MCPcopy Create free account
hub / github.com/docker/secrets-engine / newCommand

Function newCommand

store/keychain/cmd/main.go:33–133  ·  view source on GitHub ↗

newCommand creates an example CLI that uses the keychain library It supports windows, linux and macOS.

(ctx context.Context)

Source from the content-addressed store, hash-verified

31// newCommand creates an example CLI that uses the keychain library
32// It supports windows, linux and macOS.
33func newCommand(ctx context.Context) (*cobra.Command, error) {
34 kc, err := keychain.New(
35 ctx,
36 "io.docker.Secrets",
37 "docker-example-cli",
38 func(_ context.Context, _ store.ID) *mocks.MockCredential {
39 return &mocks.MockCredential{}
40 },
41 )
42 if errors.Is(err, keychain.ErrKeychainUnavailable) {
43 // The keychain backend is unreachable on this host (for example WSL
44 // with no D-Bus session bus, or no gnome-keyring/kwallet running). A
45 // real application would fall back to another store here; this example
46 // simply reports it.
47 return nil, fmt.Errorf("keychain backend unavailable, no fallback configured: %w", err)
48 }
49 if err != nil {
50 return nil, err
51 }
52 list := &cobra.Command{
53 Use: "list",
54 Aliases: []string{"ls"},
55 RunE: func(cmd *cobra.Command, _ []string) error {
56 secrets, err := kc.GetAllMetadata(cmd.Context())
57 if errors.Is(err, store.ErrCredentialNotFound) {
58 fmt.Println("No Secrets found")
59 return nil
60 }
61 if err != nil {
62 return err
63 }
64
65 for id, v := range secrets {
66 fmt.Printf("\nID: %s\n", id)
67 fmt.Printf("\nMetadata: %+v", v.Metadata())
68 }
69 return nil
70 },
71 }
72
73 var (
74 username string
75 password string
76 )
77 save := &cobra.Command{
78 Use: "store",
79 Aliases: []string{"set", "save"},
80 RunE: func(cmd *cobra.Command, _ []string) error {
81 id, err := store.ParseID(path.Join("keystore-cli", username))
82 if err != nil {
83 return err
84 }
85 creds := &mocks.MockCredential{
86 Username: username,
87 Password: password,
88 }
89 return kc.Save(cmd.Context(), id, creds)
90 },

Callers 1

mainFunction · 0.85

Calls 10

PrintlnMethod · 0.80
ErrorfMethod · 0.65
GetAllMetadataMethod · 0.65
PrintfMethod · 0.65
MetadataMethod · 0.65
SaveMethod · 0.65
GetMethod · 0.65
MarshalMethod · 0.65
StringMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected