author | Mikael Berthe <mikael@lilotux.net> |
Sat, 04 Feb 2023 12:58:35 +0100 | |
changeset 265 | 05c40b36d3b2 |
parent 245 | 910f00ab2799 |
child 282 | 46b37f928ab9 |
permissions | -rw-r--r-- |
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
245
diff
changeset
|
1 |
// Copyright © 2017-2023 Mikael Berthe <mikael@lilotux.net> |
0 | 2 |
// |
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
10
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
9 |
"os" |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
10 |
|
114
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
11 |
"github.com/mattn/go-isatty" |
0 | 12 |
"github.com/spf13/cobra" |
13 |
"github.com/spf13/viper" |
|
14 |
||
15 |
"github.com/McKael/madonctl/printer" |
|
16 |
) |
|
17 |
||
18 |
var configCmd = &cobra.Command{ |
|
19 |
Use: "config", |
|
20 |
Short: "Display configuration", |
|
127
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
21 |
Long: `Display configuration |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
22 |
|
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
23 |
Display current configuration. You can use this command to generate an |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
24 |
initial configuration file (see the examples below). |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
25 |
|
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
26 |
This command is disabled if the safe_mode setting is set to true in the |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
27 |
configuration file.`, |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
28 |
Example: ` madonctl config dump -i INSTANCE -L USERNAME -P PASS > config.yaml |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
29 |
madonctl whoami |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
30 |
madonctl whoami --template '{{.access_token}}'`, |
0 | 31 |
} |
32 |
||
33 |
func init() { |
|
34 |
RootCmd.AddCommand(configCmd) |
|
35 |
||
36 |
// Subcommands |
|
37 |
configCmd.AddCommand(configSubcommands...) |
|
38 |
} |
|
39 |
||
40 |
var configSubcommands = []*cobra.Command{ |
|
41 |
&cobra.Command{ |
|
127
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
42 |
Use: "dump", |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
43 |
Short: "Dump the configuration", |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
114
diff
changeset
|
44 |
Example: ` madonctl config dump -i INSTANCE -L USERNAME -P PASS > config.yaml`, |
0 | 45 |
RunE: func(cmd *cobra.Command, args []string) error { |
244
a01bc98ae01a
Display config details after oauth2
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
46 |
return configDump(false) |
0 | 47 |
}, |
48 |
}, |
|
49 |
&cobra.Command{ |
|
50 |
Use: "whoami", |
|
51 |
Aliases: []string{"token"}, |
|
52 |
Short: "Display user token", |
|
53 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
54 |
return configDisplayToken() |
|
55 |
}, |
|
56 |
}, |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
57 |
&cobra.Command{ |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
58 |
Use: "themes", |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
59 |
//Aliases: []string{}, |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
60 |
Short: "Display available themes", |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
61 |
RunE: func(cmd *cobra.Command, args []string) error { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
62 |
return configDisplayThemes() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
63 |
}, |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
64 |
}, |
0 | 65 |
} |
66 |
||
67 |
const configurationTemplate = `--- |
|
68 |
instance: '{{.InstanceURL}}' |
|
69 |
app_id: '{{.ID}}' |
|
70 |
app_secret: '{{.Secret}}' |
|
71 |
||
72 |
{{if .UserToken}}token: {{.UserToken.access_token}}{{else}}#token: ''{{end}} |
|
73 |
#login: '' |
|
74 |
#password: '' |
|
75 |
safe_mode: true |
|
136
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
76 |
|
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
77 |
#default_visibility: unlisted |
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
78 |
|
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
79 |
#template_directory: '' |
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
80 |
#default_output: theme |
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
81 |
#default_theme: ansi |
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
82 |
#color: auto |
32430612b929
config dump: Display all available settings
Mikael Berthe <mikael@lilotux.net>
parents:
127
diff
changeset
|
83 |
#verbose: false |
0 | 84 |
... |
85 |
` |
|
86 |
||
245
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
87 |
func configDump(oauth2 bool) error { |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
88 |
if !oauth2 { |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
89 |
if viper.GetBool("safe_mode") { |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
90 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
91 |
return nil |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
92 |
} |
0 | 93 |
|
245
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
94 |
if err := madonInitClient(); err != nil { |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
95 |
return err |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
96 |
} |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
97 |
|
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
98 |
// Try to sign in if a login was provided |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
99 |
if viper.GetString("token") != "" || viper.GetString("login") != "" { |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
100 |
if err := madonLogin(); err != nil { |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
101 |
errPrint("Error: could not log in: %v", err) |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
102 |
os.Exit(-1) |
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
103 |
} |
10
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
104 |
} |
0 | 105 |
} |
106 |
||
107 |
var p printer.ResourcePrinter |
|
108 |
var err error |
|
109 |
||
110 |
if getOutputFormat() == "plain" { |
|
111 |
cfile := viper.ConfigFileUsed() |
|
112 |
if cfile == "" { |
|
113 |
cfile = defaultConfigFile |
|
114 |
} |
|
114
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
115 |
if isatty.IsTerminal(os.Stdout.Fd()) { |
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
116 |
errPrint("You can copy the following lines into a configuration file.") |
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
117 |
errPrint("E.g. %s -i INSTANCE -L USERNAME -P PASS config dump > %s", AppName, cfile) |
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
118 |
errPrint(" or %s -i INSTANCE oauth2 > %s\n", AppName, cfile) |
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
119 |
} |
83
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
120 |
pOptions := printer.Options{"template": configurationTemplate} |
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
121 |
p, err = printer.NewPrinterTemplate(pOptions) |
0 | 122 |
} else { |
123 |
p, err = getPrinter() |
|
124 |
} |
|
125 |
if err != nil { |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
126 |
errPrint("Error: %v", err) |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
127 |
os.Exit(1) |
0 | 128 |
} |
129 |
return p.PrintObj(gClient, nil, "") |
|
130 |
} |
|
131 |
||
132 |
func configDisplayToken() error { |
|
133 |
if viper.GetBool("safe_mode") { |
|
134 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
|
135 |
return nil |
|
136 |
} |
|
137 |
||
138 |
if err := madonInit(true); err != nil { |
|
139 |
return err |
|
140 |
} |
|
141 |
||
142 |
p, err := getPrinter() |
|
143 |
if err != nil { |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
144 |
errPrint("Error: %v", err) |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
145 |
os.Exit(1) |
0 | 146 |
} |
110 | 147 |
return p.printObj(gClient.UserToken) |
0 | 148 |
} |
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
149 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
150 |
// configDisplayThemes lists the available themes |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
151 |
// It is intended for shell completion. |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
152 |
func configDisplayThemes() error { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
153 |
var p printer.ResourcePrinter |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
154 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
155 |
themes, err := getThemes() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
156 |
if err != nil { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
157 |
errPrint("Error: %v", err) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
158 |
os.Exit(1) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
159 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
160 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
161 |
if getOutputFormat() == "plain" { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
162 |
pOptions := printer.Options{"template": `{{printf "%s\n" .}}`} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
163 |
p, err = printer.NewPrinterTemplate(pOptions) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
164 |
} else { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
165 |
p, err = getPrinter() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
166 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
167 |
if err != nil { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
168 |
errPrint("Error: %v", err) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
169 |
os.Exit(1) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
170 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
171 |
return p.PrintObj(themes, nil, "") |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
172 |
} |