Skip to content

Commit 3277a3c

Browse files
authored
enhance: add credential show command (#491)
Signed-off-by: Grant Linville <[email protected]>
1 parent fa70e26 commit 3277a3c

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

pkg/cli/credential.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (c *Credential) Customize(cmd *cobra.Command) {
2626
cmd.Short = "List stored credentials"
2727
cmd.Args = cobra.NoArgs
2828
cmd.AddCommand(cmd2.Command(&Delete{root: c.root}))
29+
cmd.AddCommand(cmd2.Command(&Show{root: c.root}))
2930
}
3031

3132
func (c *Credential) Run(_ *cobra.Command, _ []string) error {
@@ -68,7 +69,7 @@ func (c *Credential) Run(_ *cobra.Command, _ []string) error {
6869
defer w.Flush()
6970

7071
if c.ShowEnvVars {
71-
_, _ = w.Write([]byte("CONTEXT\tTOOL\tENVIRONMENT VARIABLES\n"))
72+
_, _ = w.Write([]byte("CONTEXT\tCREDENTIAL\tENVIRONMENT VARIABLES\n"))
7273

7374
for _, cred := range creds {
7475
envVars := make([]string, 0, len(cred.Env))
@@ -79,7 +80,7 @@ func (c *Credential) Run(_ *cobra.Command, _ []string) error {
7980
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", cred.Context, cred.ToolName, strings.Join(envVars, ", "))
8081
}
8182
} else {
82-
_, _ = w.Write([]byte("CONTEXT\tTOOL\n"))
83+
_, _ = w.Write([]byte("CONTEXT\tCREDENTIAL\n"))
8384
for _, cred := range creds {
8485
_, _ = fmt.Fprintf(w, "%s\t%s\n", cred.Context, cred.ToolName)
8586
}
@@ -93,7 +94,7 @@ func (c *Credential) Run(_ *cobra.Command, _ []string) error {
9394
if c.ShowEnvVars {
9495
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
9596
defer w.Flush()
96-
_, _ = w.Write([]byte("TOOL\tENVIRONMENT VARIABLES\n"))
97+
_, _ = w.Write([]byte("CREDENTIAL\tENVIRONMENT VARIABLES\n"))
9798

9899
for _, cred := range creds {
99100
envVars := make([]string, 0, len(cred.Env))

pkg/cli/credential_delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Delete struct {
1414
}
1515

1616
func (c *Delete) Customize(cmd *cobra.Command) {
17-
cmd.Use = "delete <tool name>"
17+
cmd.Use = "delete <credential name>"
1818
cmd.Aliases = []string{"rm", "del"}
1919
cmd.SilenceUsage = true
2020
cmd.Short = "Delete a stored credential"

pkg/cli/credential_show.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"text/tabwriter"
7+
8+
"github.com/gptscript-ai/gptscript/pkg/cache"
9+
"github.com/gptscript-ai/gptscript/pkg/config"
10+
"github.com/gptscript-ai/gptscript/pkg/credentials"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
type Show struct {
15+
root *GPTScript
16+
}
17+
18+
func (c *Show) Customize(cmd *cobra.Command) {
19+
cmd.Use = "show <credential name>"
20+
cmd.Aliases = []string{"reveal"}
21+
cmd.SilenceUsage = true
22+
cmd.Short = "Show the secret value of a stored credential"
23+
cmd.Args = cobra.ExactArgs(1)
24+
}
25+
26+
func (c *Show) Run(_ *cobra.Command, args []string) error {
27+
opts, err := c.root.NewGPTScriptOpts()
28+
if err != nil {
29+
return err
30+
}
31+
opts.Cache = cache.Complete(opts.Cache)
32+
33+
cfg, err := config.ReadCLIConfig(c.root.ConfigFile)
34+
if err != nil {
35+
return fmt.Errorf("failed to read CLI config: %w", err)
36+
}
37+
38+
store, err := credentials.NewStore(cfg, c.root.CredentialContext, opts.Cache.CacheDir)
39+
if err != nil {
40+
return fmt.Errorf("failed to get credentials store: %w", err)
41+
}
42+
43+
cred, exists, err := store.Get(args[0])
44+
if err != nil {
45+
return fmt.Errorf("failed to get credential: %w", err)
46+
}
47+
48+
if !exists {
49+
return fmt.Errorf("credential %q not found", args[0])
50+
}
51+
52+
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
53+
defer w.Flush()
54+
55+
_, _ = w.Write([]byte("ENV\tVALUE\n"))
56+
for env, val := range cred.Env {
57+
_, _ = fmt.Fprintf(w, "%s\t%s\n", env, val)
58+
}
59+
60+
return nil
61+
}

0 commit comments

Comments
 (0)