Skip to content

Commit 656ae77

Browse files
authored
fix: credentials: hide unneeded global flags (#247)
Signed-off-by: Grant Linville <[email protected]>
1 parent d51cad8 commit 656ae77

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/samber/lo v1.38.1
2323
github.com/sirupsen/logrus v1.9.3
2424
github.com/spf13/cobra v1.8.0
25+
github.com/spf13/pflag v1.0.5
2526
github.com/stretchr/testify v1.8.4
2627
github.com/tidwall/gjson v1.17.1
2728
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
@@ -68,7 +69,6 @@ require (
6869
github.com/pkg/errors v0.9.1 // indirect
6970
github.com/pmezard/go-difflib v1.0.0 // indirect
7071
github.com/rivo/uniseg v0.1.0 // indirect
71-
github.com/spf13/pflag v1.0.5 // indirect
7272
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
7373
github.com/therootcompany/xz v1.0.1 // indirect
7474
github.com/tidwall/match v1.1.1 // indirect

pkg/cli/gptscript.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/gptscript-ai/gptscript/pkg/types"
2626
"github.com/gptscript-ai/gptscript/pkg/version"
2727
"github.com/spf13/cobra"
28+
"github.com/spf13/pflag"
2829
"golang.org/x/term"
2930
)
3031

@@ -58,9 +59,44 @@ type GPTScript struct {
5859

5960
func New() *cobra.Command {
6061
root := &GPTScript{}
61-
return cmd.Command(root, &Eval{
62+
command := cmd.Command(root, &Eval{
6263
gptscript: root,
6364
}, &Credential{root: root})
65+
66+
// Hide all the global flags for the credential subcommand.
67+
for _, child := range command.Commands() {
68+
if strings.HasPrefix(child.Name(), "credential") {
69+
command.PersistentFlags().VisitAll(func(f *pflag.Flag) {
70+
newFlag := pflag.Flag{
71+
Name: f.Name,
72+
Usage: f.Usage,
73+
}
74+
75+
if f.Name != "credential-context" { // We want to keep credential-context
76+
child.Flags().AddFlag(&newFlag)
77+
child.Flags().Lookup(newFlag.Name).Hidden = true
78+
}
79+
})
80+
81+
for _, grandchild := range child.Commands() {
82+
command.PersistentFlags().VisitAll(func(f *pflag.Flag) {
83+
newFlag := pflag.Flag{
84+
Name: f.Name,
85+
Usage: f.Usage,
86+
}
87+
88+
if f.Name != "credential-context" {
89+
grandchild.Flags().AddFlag(&newFlag)
90+
grandchild.Flags().Lookup(newFlag.Name).Hidden = true
91+
}
92+
})
93+
}
94+
95+
break
96+
}
97+
}
98+
99+
return command
64100
}
65101

66102
func (r *GPTScript) NewRunContext(cmd *cobra.Command) context.Context {

0 commit comments

Comments
 (0)