author | Mikael Berthe <mikael@lilotux.net> |
Wed, 21 Mar 2018 23:55:25 +0100 | |
changeset 216 | 3e017cfad3cc |
parent 185 | 564d92b54b00 |
child 244 | a01bc98ae01a |
permissions | -rw-r--r-- |
185 | 1 |
// Copyright © 2017-2018 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 { |
46 |
return configDump() |
|
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 |
||
87 |
func configDump() error { |
|
88 |
if viper.GetBool("safe_mode") { |
|
89 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
|
90 |
return nil |
|
91 |
} |
|
92 |
||
93 |
if err := madonInitClient(); err != nil { |
|
94 |
return err |
|
95 |
} |
|
10
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
96 |
// Try to sign in if a login was provided |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
97 |
if viper.GetString("token") != "" || viper.GetString("login") != "" { |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
98 |
if err := madonLogin(); err != nil { |
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
99 |
errPrint("Error: could not log in: %v", err) |
10
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
100 |
os.Exit(-1) |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
101 |
} |
0 | 102 |
} |
103 |
||
104 |
var p printer.ResourcePrinter |
|
105 |
var err error |
|
106 |
||
107 |
if getOutputFormat() == "plain" { |
|
108 |
cfile := viper.ConfigFileUsed() |
|
109 |
if cfile == "" { |
|
110 |
cfile = defaultConfigFile |
|
111 |
} |
|
114
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
112 |
if isatty.IsTerminal(os.Stdout.Fd()) { |
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
113 |
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
|
114 |
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
|
115 |
errPrint(" or %s -i INSTANCE oauth2 > %s\n", AppName, cfile) |
590c9dd7328e
Improve config dump output
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
116 |
} |
83
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
117 |
pOptions := printer.Options{"template": configurationTemplate} |
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
118 |
p, err = printer.NewPrinterTemplate(pOptions) |
0 | 119 |
} else { |
120 |
p, err = getPrinter() |
|
121 |
} |
|
122 |
if err != nil { |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
123 |
errPrint("Error: %v", err) |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
124 |
os.Exit(1) |
0 | 125 |
} |
126 |
return p.PrintObj(gClient, nil, "") |
|
127 |
} |
|
128 |
||
129 |
func configDisplayToken() error { |
|
130 |
if viper.GetBool("safe_mode") { |
|
131 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
|
132 |
return nil |
|
133 |
} |
|
134 |
||
135 |
if err := madonInit(true); err != nil { |
|
136 |
return err |
|
137 |
} |
|
138 |
||
139 |
p, err := getPrinter() |
|
140 |
if err != nil { |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
141 |
errPrint("Error: %v", err) |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
142 |
os.Exit(1) |
0 | 143 |
} |
110 | 144 |
return p.printObj(gClient.UserToken) |
0 | 145 |
} |
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
146 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
147 |
// configDisplayThemes lists the available themes |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
148 |
// It is intended for shell completion. |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
149 |
func configDisplayThemes() error { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
150 |
var p printer.ResourcePrinter |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
151 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
152 |
themes, err := getThemes() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
153 |
if err != nil { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
154 |
errPrint("Error: %v", err) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
155 |
os.Exit(1) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
156 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
157 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
158 |
if getOutputFormat() == "plain" { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
159 |
pOptions := printer.Options{"template": `{{printf "%s\n" .}}`} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
160 |
p, err = printer.NewPrinterTemplate(pOptions) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
161 |
} else { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
162 |
p, err = getPrinter() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
163 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
164 |
if err != nil { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
165 |
errPrint("Error: %v", err) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
166 |
os.Exit(1) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
167 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
168 |
return p.PrintObj(themes, nil, "") |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
169 |
} |