@@ -9,78 +9,90 @@ import (
99 "github.com/spf13/cobra"
1010)
1111
12- var (
12+ const notSetValue = "[not set]"
13+
14+ type ConfigOptions struct {
1315 host string
1416 token string
1517 email string
1618 clear bool
1719 show bool
18- )
19-
20- const notSetValue = "[not set]"
20+ }
2121
22- var configCmd = & cobra.Command {
23- Use : "config" ,
24- Short : "Configure Glean CLI credentials" ,
25- Long : heredoc .Doc (`
26- Configure credentials for the Glean CLI.
27-
28- Examples:
29- # Set Glean host (either format works)
30- glean config --host linkedin
31- glean config --host linkedin-be.glean.com
32-
33- # Set Glean API token
34- glean config --token your-token
35-
36- # Set Glean user email
37- glean config --email user@company.com
38-
39- # Show current configuration
40- glean config --show
41-
42- # Clear all stored credentials
43- glean config --clear
44- ` ),
45- RunE : func (cmd * cobra.Command , args []string ) error {
46- if show {
47- cfg , err := config .LoadConfig ()
48- if err != nil {
49- return fmt .Errorf ("failed to access keyring: %w" , err )
22+ func NewCmdConfig () * cobra.Command {
23+ opts := ConfigOptions {}
24+
25+ cmd := & cobra.Command {
26+ Use : "config" ,
27+ Short : "Manage Glean CLI configuration" ,
28+ Long : heredoc .Doc (`
29+ Configure credentials for the Glean CLI.
30+
31+ Examples:
32+ # Set Glean host (either format works)
33+ glean config --host linkedin
34+ glean config --host linkedin-be.glean.com
35+
36+ # Set Glean API token
37+ glean config --token your-token
38+
39+ # Set Glean user email
40+ glean config --email user@company.com
41+
42+ # Show current configuration
43+ glean config --show
44+
45+ # Clear all stored credentials
46+ glean config --clear
47+ ` ),
48+ RunE : func (cmd * cobra.Command , args []string ) error {
49+ if opts .show {
50+ cfg , err := config .LoadConfig ()
51+ if err != nil {
52+ return fmt .Errorf ("failed to access keyring: %w" , err )
53+ }
54+
55+ fmt .Println ("Current configuration:" )
56+ fmt .Printf (" %-10s %s\n " , "Host:" , valueOrNotSet (cfg .GleanHost ))
57+ fmt .Printf (" %-10s %s\n " , "Email:" , valueOrNotSet (cfg .GleanEmail ))
58+
59+ // Mask token if present
60+ tokenDisplay := notSetValue
61+ if cfg .GleanToken != "" {
62+ tokenDisplay = cfg .GleanToken [0 :4 ] + strings .Repeat ("*" , len (cfg .GleanToken )- 4 )
63+ }
64+ fmt .Printf (" %-10s %s\n " , "Token:" , tokenDisplay )
65+ return nil
5066 }
5167
52- fmt .Println ("Current configuration:" )
53- fmt .Printf (" %-10s %s\n " , "Host:" , valueOrNotSet (cfg .GleanHost ))
54- fmt .Printf (" %-10s %s\n " , "Email:" , valueOrNotSet (cfg .GleanEmail ))
68+ if opts .clear {
69+ if err := config .ClearConfig (); err != nil {
70+ return fmt .Errorf ("failed to clear configuration: %w" , err )
71+ }
72+ fmt .Println ("Configuration cleared successfully" )
73+ return nil
74+ }
5575
56- // Mask token if present
57- tokenDisplay := notSetValue
58- if cfg .GleanToken != "" {
59- tokenDisplay = cfg .GleanToken [0 :4 ] + strings .Repeat ("*" , len (cfg .GleanToken )- 4 )
76+ if opts .host == "" && opts .token == "" && opts .email == "" {
77+ return fmt .Errorf ("no configuration provided. Use --host, --token, or --email to set configuration" )
6078 }
61- fmt .Printf (" %-10s %s\n " , "Token:" , tokenDisplay )
62- return nil
63- }
6479
65- if clear {
66- if err := config .ClearConfig (); err != nil {
67- return fmt .Errorf ("failed to clear configuration: %w" , err )
80+ if err := config .SaveConfig (opts .host , opts .token , opts .email ); err != nil {
81+ return fmt .Errorf ("failed to save configuration: %w" , err )
6882 }
69- fmt .Println ("Configuration cleared successfully" )
70- return nil
71- }
7283
73- if host == "" && token == "" && email == "" {
74- return fmt .Errorf ("no configuration provided. Use --host, --token, or --email to set configuration" )
75- }
84+ fmt .Println ("Configuration saved successfully" )
85+ return nil
86+ },
87+ }
7688
77- if err := config .SaveConfig (host , token , email ); err != nil {
78- return fmt .Errorf ("failed to save configuration: %w" , err )
79- }
89+ cmd .Flags ().StringVar (& opts .host , "host" , "" , "Glean instance name or full hostname (e.g., 'linkedin' or 'linkedin-be.glean.com')" )
90+ cmd .Flags ().StringVar (& opts .token , "token" , "" , "Glean API token" )
91+ cmd .Flags ().StringVar (& opts .email , "email" , "" , "Email address for API requests" )
92+ cmd .Flags ().BoolVar (& opts .clear , "clear" , false , "Clear all stored credentials" )
93+ cmd .Flags ().BoolVar (& opts .show , "show" , false , "Show current configuration" )
8094
81- fmt .Println ("Configuration saved successfully" )
82- return nil
83- },
95+ return cmd
8496}
8597
8698func valueOrNotSet (value string ) string {
@@ -89,13 +101,3 @@ func valueOrNotSet(value string) string {
89101 }
90102 return value
91103}
92-
93- func init () {
94- rootCmd .AddCommand (configCmd )
95-
96- configCmd .Flags ().StringVar (& host , "host" , "" , "Glean instance name or full hostname (e.g., 'linkedin' or 'linkedin-be.glean.com')" )
97- configCmd .Flags ().StringVar (& token , "token" , "" , "Glean API token" )
98- configCmd .Flags ().StringVar (& email , "email" , "" , "Email address for API requests" )
99- configCmd .Flags ().BoolVar (& clear , "clear" , false , "Clear all stored credentials" )
100- configCmd .Flags ().BoolVar (& show , "show" , false , "Show current configuration" )
101- }
0 commit comments