author | Mikael Berthe <mikael@lilotux.net> |
Sun, 23 Apr 2017 18:18:51 +0200 | |
changeset 10 | e4c490882126 |
parent 0 | 5abace724584 |
child 81 | b1671f83e91b |
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 |
}, |
|
45 |
} |
|
46 |
||
47 |
const configurationTemplate = `--- |
|
48 |
instance: '{{.InstanceURL}}' |
|
49 |
app_id: '{{.ID}}' |
|
50 |
app_secret: '{{.Secret}}' |
|
51 |
||
52 |
{{if .UserToken}}token: {{.UserToken.access_token}}{{else}}#token: ''{{end}} |
|
53 |
#login: '' |
|
54 |
#password: '' |
|
55 |
safe_mode: true |
|
56 |
... |
|
57 |
` |
|
58 |
||
59 |
func configDump() error { |
|
60 |
if viper.GetBool("safe_mode") { |
|
61 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
|
62 |
return nil |
|
63 |
} |
|
64 |
||
65 |
if err := madonInitClient(); err != nil { |
|
66 |
return err |
|
67 |
} |
|
10
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
68 |
// 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
|
69 |
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
|
70 |
if err := madonLogin(); err != nil { |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
71 |
errPrint("Error: could not log in: %s", err) |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
72 |
os.Exit(-1) |
e4c490882126
Don't dump the configuration file if login failed
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
73 |
} |
0 | 74 |
} |
75 |
||
76 |
var p printer.ResourcePrinter |
|
77 |
var err error |
|
78 |
||
79 |
if getOutputFormat() == "plain" { |
|
80 |
cfile := viper.ConfigFileUsed() |
|
81 |
if cfile == "" { |
|
82 |
cfile = defaultConfigFile |
|
83 |
} |
|
84 |
errPrint("You can copy the following lines into a configuration file.") |
|
85 |
errPrint("E.g. %s -i INSTANCE -L USERNAME -P PASS config dump > %s\n", AppName, cfile) |
|
86 |
p, err = printer.NewPrinterTemplate(configurationTemplate) |
|
87 |
} else { |
|
88 |
p, err = getPrinter() |
|
89 |
} |
|
90 |
if err != nil { |
|
91 |
return err |
|
92 |
} |
|
93 |
return p.PrintObj(gClient, nil, "") |
|
94 |
} |
|
95 |
||
96 |
func configDisplayToken() error { |
|
97 |
if viper.GetBool("safe_mode") { |
|
98 |
errPrint("Cannot dump: disabled by configuration (safe_mode)") |
|
99 |
return nil |
|
100 |
} |
|
101 |
||
102 |
if err := madonInit(true); err != nil { |
|
103 |
return err |
|
104 |
} |
|
105 |
||
106 |
p, err := getPrinter() |
|
107 |
if err != nil { |
|
108 |
return err |
|
109 |
} |
|
110 |
return p.PrintObj(gClient.UserToken, nil, "") |
|
111 |
} |