author | Mikael Berthe <mikael@lilotux.net> |
Sun, 07 May 2017 19:31:37 +0200 | |
changeset 91 | 02312ccc1fd3 |
parent 83 | 57afac822019 |
child 110 | 57843255fd1a |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
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 |
|
0 | 11 |
"github.com/spf13/cobra" |
12 |
"github.com/spf13/viper" |
|
13 |
||
14 |
"github.com/McKael/madonctl/printer" |
|
15 |
) |
|
16 |
||
17 |
var configCmd = &cobra.Command{ |
|
18 |
Use: "config", |
|
19 |
Short: "Display configuration", |
|
20 |
} |
|
21 |
||
22 |
func init() { |
|
23 |
RootCmd.AddCommand(configCmd) |
|
24 |
||
25 |
// Subcommands |
|
26 |
configCmd.AddCommand(configSubcommands...) |
|
27 |
} |
|
28 |
||
29 |
var configSubcommands = []*cobra.Command{ |
|
30 |
&cobra.Command{ |
|
31 |
Use: "dump", |
|
32 |
Short: "Dump the configuration", |
|
33 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
34 |
return configDump() |
|
35 |
}, |
|
36 |
}, |
|
37 |
&cobra.Command{ |
|
38 |
Use: "whoami", |
|
39 |
Aliases: []string{"token"}, |
|
40 |
Short: "Display user token", |
|
41 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
42 |
return configDisplayToken() |
|
43 |
}, |
|
44 |
}, |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
45 |
&cobra.Command{ |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
46 |
Use: "themes", |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
47 |
//Aliases: []string{}, |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
48 |
Short: "Display available themes", |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
49 |
RunE: func(cmd *cobra.Command, args []string) error { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
50 |
return configDisplayThemes() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
51 |
}, |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
52 |
}, |
0 | 53 |
} |
54 |
||
55 |
const configurationTemplate = `--- |
|
56 |
instance: '{{.InstanceURL}}' |
|
57 |
app_id: '{{.ID}}' |
|
58 |
app_secret: '{{.Secret}}' |
|
59 |
||
60 |
{{if .UserToken}}token: {{.UserToken.access_token}}{{else}}#token: ''{{end}} |
|
61 |
#login: '' |
|
62 |
#password: '' |
|
63 |
safe_mode: true |
|
64 |
... |
|
65 |
` |
|
66 |
||
67 |
func configDump() error { |
|
68 |
if viper.GetBool("safe_mode") { |
|
69 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
|
70 |
return nil |
|
71 |
} |
|
72 |
||
73 |
if err := madonInitClient(); err != nil { |
|
74 |
return err |
|
75 |
} |
|
10
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
76 |
// 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
|
77 |
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
|
78 |
if err := madonLogin(); err != nil { |
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
79 |
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
|
80 |
os.Exit(-1) |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
81 |
} |
0 | 82 |
} |
83 |
||
84 |
var p printer.ResourcePrinter |
|
85 |
var err error |
|
86 |
||
87 |
if getOutputFormat() == "plain" { |
|
88 |
cfile := viper.ConfigFileUsed() |
|
89 |
if cfile == "" { |
|
90 |
cfile = defaultConfigFile |
|
91 |
} |
|
92 |
errPrint("You can copy the following lines into a configuration file.") |
|
93 |
errPrint("E.g. %s -i INSTANCE -L USERNAME -P PASS config dump > %s\n", AppName, cfile) |
|
83
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
94 |
pOptions := printer.Options{"template": configurationTemplate} |
57afac822019
Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents:
81
diff
changeset
|
95 |
p, err = printer.NewPrinterTemplate(pOptions) |
0 | 96 |
} else { |
97 |
p, err = getPrinter() |
|
98 |
} |
|
99 |
if err != nil { |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
100 |
errPrint("Error: %v", err) |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
101 |
os.Exit(1) |
0 | 102 |
} |
103 |
return p.PrintObj(gClient, nil, "") |
|
104 |
} |
|
105 |
||
106 |
func configDisplayToken() error { |
|
107 |
if viper.GetBool("safe_mode") { |
|
108 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
|
109 |
return nil |
|
110 |
} |
|
111 |
||
112 |
if err := madonInit(true); err != nil { |
|
113 |
return err |
|
114 |
} |
|
115 |
||
116 |
p, err := getPrinter() |
|
117 |
if err != nil { |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
118 |
errPrint("Error: %v", err) |
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
119 |
os.Exit(1) |
0 | 120 |
} |
121 |
return p.PrintObj(gClient.UserToken, nil, "") |
|
122 |
} |
|
91
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
123 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
124 |
// configDisplayThemes lists the available themes |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
125 |
// It is intended for shell completion. |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
126 |
func configDisplayThemes() error { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
127 |
var p printer.ResourcePrinter |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
128 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
129 |
themes, err := getThemes() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
130 |
if err != nil { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
131 |
errPrint("Error: %v", err) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
132 |
os.Exit(1) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
133 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
134 |
|
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
135 |
if getOutputFormat() == "plain" { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
136 |
pOptions := printer.Options{"template": `{{printf "%s\n" .}}`} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
137 |
p, err = printer.NewPrinterTemplate(pOptions) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
138 |
} else { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
139 |
p, err = getPrinter() |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
140 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
141 |
if err != nil { |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
142 |
errPrint("Error: %v", err) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
143 |
os.Exit(1) |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
144 |
} |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
145 |
return p.PrintObj(themes, nil, "") |
02312ccc1fd3
Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents:
83
diff
changeset
|
146 |
} |