<commit_before>package trust

import (
	"fmt"
	"path"
	"sort"
	"strings"

	"github.com/docker/cli/cli"
	"github.com/docker/cli/cli/command"
	"github.com/docker/cli/cli/command/image"
	"github.com/docker/cli/cli/trust"
	"github.com/docker/notary/client"
	"github.com/docker/notary/tuf/data"
	"github.com/spf13/cobra"
)

func newSignCommand(dockerCli command.Cli) *cobra.Command {
	cmd := &cobra.Command{
		Use:   "sign [OPTIONS] IMAGE:TAG",
		Short: "Sign an image",
		Args:  cli.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return signImage(dockerCli, args[0])
		},
	}
	return cmd
}

func signImage(cli command.Cli, imageName string) error {
	ctx, ref, repoInfo, authConfig, err := getImageReferencesAndAuth(cli, imageName)
	if err != nil {
		return err
	}

	notaryRepo, err := trust.GetNotaryRepository(cli, repoInfo, *authConfig, "push", "pull")
	if err != nil {
		return trust.NotaryError(ref.Name(), err)
	}
	if err = clearChangeList(notaryRepo); err != nil {
		return err
	}
	defer clearChangeList(notaryRepo)
	tag, err := getTag(ref)
	if err != nil {
		return err
	}
	if tag == "" {
		return fmt.Errorf("No tag specified for %s", imageName)
	}

	// get the latest repository metadata so we can figure out which roles to sign
	if err = notaryRepo.Update(false); err != nil {
		switch err.(type) {
		case client.ErrRepoNotInitialized, client.ErrRepositoryNotExist:
			// before initializing a new repo, check that the image exists locally:
			if err := checkLocalImageExistence(ctx, cli, imageName); err != nil {
				return err
			}

			userRole := data.RoleName(path.Join(data.CanonicalTargetsRole.String(), authConfig.Username))
			if err := initNotaryRepoWithSigners(notaryRepo, userRole); err != nil {
				return trust.NotaryError(ref.Name(), err)
			}

			fmt.Fprintf(cli.Out(), "Created signer: %s\n", authConfig.Username)
			fmt.Fprintf(cli.Out(), "Finished initializing %q\n", notaryRepo.GetGUN().String())
		default:
			return trust.NotaryError(repoInfo.Name.Name(), err)
		}
	}
	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(cli, repoInfo.Index, "push")
	target, err := createTarget(notaryRepo, tag)
	if err != nil {
		switch err := err.(type) {
		case client.ErrNoSuchTarget, client.ErrRepositoryNotExist:
			// Fail fast if the image doesn't exist locally
			if err := checkLocalImageExistence(ctx, cli, imageName); err != nil {
				return err
			}
			return image.TrustedPush(ctx, cli, repoInfo, ref, *authConfig, requestPrivilege)
		default:
			return err
		}
	}
<commit_msg>Update success messaging on initialization<commit_after>package trust

import (
	"fmt"
	"path"
	"sort"
	"strings"

	"github.com/docker/cli/cli"
	"github.com/docker/cli/cli/command"
	"github.com/docker/cli/cli/command/image"
	"github.com/docker/cli/cli/trust"
	"github.com/docker/notary/client"
	"github.com/docker/notary/tuf/data"
	"github.com/spf13/cobra"
)

func newSignCommand(dockerCli command.Cli) *cobra.Command {
	cmd := &cobra.Command{
		Use:   "sign [OPTIONS] IMAGE:TAG",
		Short: "Sign an image",
		Args:  cli.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return signImage(dockerCli, args[0])
		},
	}
	return cmd
}

func signImage(cli command.Cli, imageName string) error {
	ctx, ref, repoInfo, authConfig, err := getImageReferencesAndAuth(cli, imageName)
	if err != nil {
		return err
	}

	notaryRepo, err := trust.GetNotaryRepository(cli, repoInfo, *authConfig, "push", "pull")
	if err != nil {
		return trust.NotaryError(ref.Name(), err)
	}
	if err = clearChangeList(notaryRepo); err != nil {
		return err
	}
	defer clearChangeList(notaryRepo)
	tag, err := getTag(ref)
	if err != nil {
		return err
	}
	if tag == "" {
		return fmt.Errorf("No tag specified for %s", imageName)
	}

	// get the latest repository metadata so we can figure out which roles to sign
	if err = notaryRepo.Update(false); err != nil {
		switch err.(type) {
		case client.ErrRepoNotInitialized, client.ErrRepositoryNotExist:
			// before initializing a new repo, check that the image exists locally:
			if err := checkLocalImageExistence(ctx, cli, imageName); err != nil {
				return err
			}

			userRole := data.RoleName(path.Join(data.CanonicalTargetsRole.String(), authConfig.Username))
			if err := initNotaryRepoWithSigners(notaryRepo, userRole); err != nil {
				return trust.NotaryError(ref.Name(), err)
			}

			fmt.Fprintf(cli.Out(), "Created signer: %s\n", authConfig.Username)
			fmt.Fprintf(cli.Out(), "Finished initializing signed repository for %s\n", imageName)
		default:
			return trust.NotaryError(repoInfo.Name.Name(), err)
		}
	}
	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(cli, repoInfo.Index, "push")
	target, err := createTarget(notaryRepo, tag)
	if err != nil {
		switch err := err.(type) {
		case client.ErrNoSuchTarget, client.ErrRepositoryNotExist:
			// Fail fast if the image doesn't exist locally
			if err := checkLocalImageExistence(ctx, cli, imageName); err != nil {
				return err
			}
			return image.TrustedPush(ctx, cli, repoInfo, ref, *authConfig, requestPrivilege)
		default:
			return err
		}
	}

{'fraction_non_alphanumeric': 0.13262910798122066, 'fraction_numerical': 0.001564945226917058, 'mean_word_length': 4.21734693877551, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '207242', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1779, 'n_words': 431}