<commit_before>package commands

import (
	"fmt"

	"github.com/bndw/pick/errors"
	"github.com/bndw/pick/utils"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

func init() {
	rootCmd.AddCommand(&cobra.Command{
		Use:   "cat [name]",
		Short: "Cat a credential",
		Long:  "The cat command is used to print a credential to stdout.",
		Run: func(cmd *cobra.Command, args []string) {
			runCommand(Cat, cmd, args)
		},
	})
}

func Cat(args []string, flags *pflag.FlagSet) error {
	if len(args) != 1 {
		return &errors.InvalidCommandUsage{}
	}
	name := args[0]

	safe, err := loadSafe()
	if err != nil {
		return err
	}

	account, err := safe.Get(name)
	if err != nil {
		return err
	}

	fmt.Printf(`account:  %s
username: %s
password: %s
created:  %s
modified: %s
`, name, account.Username, account.Password,
		utils.FormatUnixTime(account.CreatedOn),
		utils.FormatUnixTime(account.ModifiedOn))
	return nil
}<commit_msg>Add --history flag for 'cat' command to show an account's full history<commit_after>package commands

import (
	"fmt"

	"github.com/bndw/pick/errors"
	"github.com/bndw/pick/safe"
	"github.com/bndw/pick/utils"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

func init() {
	cmd := &cobra.Command{
		Use:   "cat [name]",
		Short: "Cat a credential",
		Long:  "The cat command is used to print a credential to stdout.",
		Run: func(cmd *cobra.Command, args []string) {
			runCommand(Cat, cmd, args)
		},
	}
	cmd.Flags().Bool("history", false, "show credential history")
	rootCmd.AddCommand(cmd)
}

func printAccount(name string, account *safe.Account, showHistory bool) {
	// Print header
	if showHistory && account.History != nil {
		history := account.History
		account.History = nil
		history = append(history, *account)
		for i, l := 0, len(history); i < l; i++ {
			printCredential(&history[i], "  ", i == 0)
		}
	} else {
		// Print a credential
		printCredential(account, "", true)
	}
}

func printCredential(account *safe.Account, printPrefix string, isInitialAccount bool) {
	// Print a credential
	var createdOrModified string
	if isInitialAccount {
		createdOrModified = "created"
	} else {
		createdOrModified = "modified"
	}
	fmt.Printf("%s: %s\n%susername: %s\n%spassword: %<PASSWORD>",
		createdOrModified,
		utils.FormatUnixTime(account.ModifiedOn),
		printPrefix, account.Username,
		printPrefix, account.Password)
}

func Cat(args []string, flags *pflag.FlagSet) error {
	if len(args) != 1 {
		return &errors.InvalidCommandUsage{}
	}
	name := args[0]

	showHistory, err := parseCatFlags(flags)
	if err != nil {
		return err
	}

	s, err := loadSafe()
	if err != nil {
		return err
	}

	account, err := s.Get(name)
	if err != nil {
		return err
	}

	printAccount(name, account, showHistory)
	return nil
}

func parseCatFlags(flags *pflag.FlagSet) (showHistory bool, err error) {
	showHistory, err = flags.GetBool("history")
	return
}
{'fraction_non_alphanumeric': 0.14573913043478262, 'fraction_numerical': 0.004869565217391304, 'mean_word_length': 4.027972027972028, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '4774596', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1046, 'n_words': 261}