Skip to content

enhance: add credential show command #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/cli/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (c *Credential) Customize(cmd *cobra.Command) {
cmd.Short = "List stored credentials"
cmd.Args = cobra.NoArgs
cmd.AddCommand(cmd2.Command(&Delete{root: c.root}))
cmd.AddCommand(cmd2.Command(&Show{root: c.root}))
}

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

if c.ShowEnvVars {
_, _ = w.Write([]byte("CONTEXT\tTOOL\tENVIRONMENT VARIABLES\n"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We previously referred to a credential name simply by the tool that created it, which is still the default, but now that aliases are supported as well, I changed the output format here and in other places.

_, _ = w.Write([]byte("CONTEXT\tCREDENTIAL\tENVIRONMENT VARIABLES\n"))

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

for _, cred := range creds {
envVars := make([]string, 0, len(cred.Env))
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/credential_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Delete struct {
}

func (c *Delete) Customize(cmd *cobra.Command) {
cmd.Use = "delete <tool name>"
cmd.Use = "delete <credential name>"
cmd.Aliases = []string{"rm", "del"}
cmd.SilenceUsage = true
cmd.Short = "Delete a stored credential"
Expand Down
61 changes: 61 additions & 0 deletions pkg/cli/credential_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cli

import (
"fmt"
"os"
"text/tabwriter"

"github.com/gptscript-ai/gptscript/pkg/cache"
"github.com/gptscript-ai/gptscript/pkg/config"
"github.com/gptscript-ai/gptscript/pkg/credentials"
"github.com/spf13/cobra"
)

type Show struct {
root *GPTScript
}

func (c *Show) Customize(cmd *cobra.Command) {
cmd.Use = "show <credential name>"
cmd.Aliases = []string{"reveal"}
cmd.SilenceUsage = true
cmd.Short = "Show the secret value of a stored credential"
cmd.Args = cobra.ExactArgs(1)
}

func (c *Show) Run(_ *cobra.Command, args []string) error {
opts, err := c.root.NewGPTScriptOpts()
if err != nil {
return err
}
opts.Cache = cache.Complete(opts.Cache)

cfg, err := config.ReadCLIConfig(c.root.ConfigFile)
if err != nil {
return fmt.Errorf("failed to read CLI config: %w", err)
}

store, err := credentials.NewStore(cfg, c.root.CredentialContext, opts.Cache.CacheDir)
if err != nil {
return fmt.Errorf("failed to get credentials store: %w", err)
}

cred, exists, err := store.Get(args[0])
if err != nil {
return fmt.Errorf("failed to get credential: %w", err)
}

if !exists {
return fmt.Errorf("credential %q not found", args[0])
}

w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
defer w.Flush()

_, _ = w.Write([]byte("ENV\tVALUE\n"))
for env, val := range cred.Env {
_, _ = fmt.Fprintf(w, "%s\t%s\n", env, val)
}

return nil
}