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