author | Mikael Berthe <mikael@lilotux.net> |
Mon, 01 May 2017 13:03:06 +0200 | |
changeset 51 | 300ac09051a7 |
parent 45 | b58a7ea1aeb2 |
child 55 | e9df533a1c4f |
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 ( |
|
9 |
"fmt" |
|
10 |
"io/ioutil" |
|
11 |
"os" |
|
12 |
||
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
13 |
"github.com/pkg/errors" |
0 | 14 |
"github.com/spf13/cobra" |
15 |
"github.com/spf13/viper" |
|
16 |
||
17 |
"github.com/McKael/madon" |
|
18 |
"github.com/McKael/madonctl/printer" |
|
19 |
) |
|
20 |
||
21 |
// AppName is the CLI application name |
|
22 |
const AppName = "madonctl" |
|
23 |
const defaultConfigFile = "$HOME/.config/" + AppName + "/" + AppName + ".yaml" |
|
24 |
||
25 |
var cfgFile string |
|
26 |
var safeMode bool |
|
27 |
var instanceURL, appID, appSecret string |
|
28 |
var login, password, token string |
|
29 |
var gClient *madon.Client |
|
30 |
var verbose bool |
|
31 |
var outputFormat string |
|
32 |
var outputTemplate, outputTemplateFile string |
|
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
33 |
var colorMode string |
0 | 34 |
|
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
35 |
// Shell completion functions |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
36 |
const shellComplFunc = ` |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
37 |
__madonctl_visibility() { |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
38 |
COMPREPLY=( direct private unlisted public ) |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
39 |
} |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
40 |
__madonctl_output() { |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
41 |
COMPREPLY=( plain json yaml template ) |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
42 |
} |
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
43 |
__madonctl_color() { |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
44 |
COMPREPLY=( auto on off ) |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
45 |
} |
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
46 |
` |
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
47 |
|
0 | 48 |
// RootCmd represents the base command when called without any subcommands |
49 |
var RootCmd = &cobra.Command{ |
|
50 |
Use: AppName, |
|
51 |
Short: "A CLI utility for Mastodon API", |
|
52 |
PersistentPreRunE: checkOutputFormat, |
|
53 |
Long: `madonctl is a CLI tool for the Mastodon REST API. |
|
54 |
||
55 |
You can use a configuration file to store common options. |
|
56 |
For example, create ` + defaultConfigFile + ` with the following |
|
57 |
contents: |
|
58 |
||
59 |
--- |
|
60 |
instance: "INSTANCE" |
|
61 |
login: "USERNAME" |
|
62 |
password: "USERPASSWORD" |
|
63 |
... |
|
64 |
||
65 |
The simplest way to generate a configuration file is to use the 'config dump' |
|
66 |
command. |
|
67 |
||
68 |
(Configuration files in JSON are also accepted.) |
|
69 |
||
70 |
If you want shell auto-completion (for bash or zsh), you can generate the |
|
71 |
completion scripts with "madonctl completion $SHELL". |
|
72 |
For example if you use bash: |
|
73 |
||
74 |
madonctl completion bash > _bash_madonctl |
|
75 |
source _bash_madonctl |
|
76 |
||
77 |
Now you should have tab completion for subcommands and flags. |
|
78 |
||
79 |
Note: Most examples assume the user's credentials are set in the configuration |
|
80 |
file. |
|
81 |
`, |
|
82 |
Example: ` madonctl instance |
|
83 |
madonctl toot "Hello, World" |
|
84 |
madonctl toot --visibility direct "@McKael Hello, You" |
|
85 |
madonctl toot --visibility private --spoiler CW "The answer was 42" |
|
86 |
madonctl post --file image.jpg Selfie |
|
87 |
madonctl --instance INSTANCE --login USERNAME --password PASS timeline |
|
88 |
madonctl accounts notifications --list --clear |
|
89 |
madonctl accounts blocked |
|
90 |
madonctl accounts search Gargron |
|
91 |
madonctl search --resolve https://mastodon.social/@Gargron |
|
92 |
madonctl accounts follow --remote Gargron@mastodon.social |
|
93 |
madonctl accounts --account-id 399 statuses |
|
94 |
madonctl status --status-id 416671 show |
|
95 |
madonctl status --status-id 416671 favourite |
|
96 |
madonctl status --status-id 416671 boost |
|
97 |
madonctl accounts show |
|
98 |
madonctl accounts show -o yaml |
|
99 |
madonctl accounts --account-id 1 followers --template '{{.acct}}{{"\n"}}' |
|
100 |
madonctl config whoami |
|
101 |
madonctl timeline :mastodon`, |
|
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
102 |
BashCompletionFunction: shellComplFunc, |
0 | 103 |
} |
104 |
||
105 |
// Execute adds all child commands to the root command sets flags appropriately. |
|
106 |
// This is called by main.main(). It only needs to happen once to the rootCmd. |
|
107 |
func Execute() { |
|
108 |
if err := RootCmd.Execute(); err != nil { |
|
109 |
errPrint("Error: %s", err.Error()) |
|
110 |
os.Exit(-1) |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
func init() { |
|
115 |
cobra.OnInitialize(initConfig) |
|
116 |
||
117 |
// Global flags |
|
118 |
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", |
|
119 |
"config file (default is "+defaultConfigFile+")") |
|
120 |
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose mode") |
|
121 |
RootCmd.PersistentFlags().StringVarP(&instanceURL, "instance", "i", "", "Mastodon instance") |
|
122 |
RootCmd.PersistentFlags().StringVarP(&login, "login", "L", "", "Instance user login") |
|
123 |
RootCmd.PersistentFlags().StringVarP(&password, "password", "P", "", "Instance user password") |
|
124 |
RootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "User token") |
|
125 |
RootCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "plain", |
|
126 |
"Output format (plain|json|yaml|template)") |
|
127 |
RootCmd.PersistentFlags().StringVar(&outputTemplate, "template", "", |
|
128 |
"Go template (for output=template)") |
|
129 |
RootCmd.PersistentFlags().StringVar(&outputTemplateFile, "template-file", "", |
|
130 |
"Go template file (for output=template)") |
|
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
131 |
RootCmd.PersistentFlags().StringVar(&colorMode, "color", "", |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
132 |
"Color mode (auto|on|off; for output=template)") |
0 | 133 |
|
134 |
// Configuration file bindings |
|
135 |
viper.BindPFlag("output", RootCmd.PersistentFlags().Lookup("output")) |
|
136 |
viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose")) |
|
137 |
// XXX viper.BindPFlag("apiKey", RootCmd.PersistentFlags().Lookup("api-key")) |
|
138 |
viper.BindPFlag("instance", RootCmd.PersistentFlags().Lookup("instance")) |
|
139 |
viper.BindPFlag("login", RootCmd.PersistentFlags().Lookup("login")) |
|
140 |
viper.BindPFlag("password", RootCmd.PersistentFlags().Lookup("password")) |
|
141 |
viper.BindPFlag("token", RootCmd.PersistentFlags().Lookup("token")) |
|
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
142 |
viper.BindPFlag("color", RootCmd.PersistentFlags().Lookup("color")) |
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
143 |
|
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
144 |
// Flag completion |
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
145 |
annotationOutput := make(map[string][]string) |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
146 |
annotationOutput[cobra.BashCompCustom] = []string{"__madonctl_output"} |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
147 |
annotationColor := make(map[string][]string) |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
148 |
annotationColor[cobra.BashCompCustom] = []string{"__madonctl_color"} |
35
61ed03c3f134
Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
149 |
|
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
150 |
RootCmd.PersistentFlags().Lookup("output").Annotations = annotationOutput |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
151 |
RootCmd.PersistentFlags().Lookup("color").Annotations = annotationColor |
0 | 152 |
} |
153 |
||
154 |
func checkOutputFormat(cmd *cobra.Command, args []string) error { |
|
155 |
of := viper.GetString("output") |
|
156 |
switch of { |
|
157 |
case "", "plain", "json", "yaml", "template": |
|
158 |
return nil // Accepted |
|
159 |
} |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
35
diff
changeset
|
160 |
return errors.Errorf("output format '%s' not supported", of) |
0 | 161 |
} |
162 |
||
163 |
// initConfig reads in config file and ENV variables if set. |
|
164 |
func initConfig() { |
|
165 |
if cfgFile != "" { // enable ability to specify config file via flag |
|
166 |
viper.SetConfigFile(cfgFile) |
|
167 |
} |
|
168 |
||
169 |
viper.SetConfigName(AppName) // name of config file (without extension) |
|
170 |
viper.AddConfigPath("$HOME/.config/" + AppName) |
|
171 |
viper.AddConfigPath("$HOME/." + AppName) |
|
172 |
||
173 |
// Read in environment variables that match, with a prefix |
|
174 |
viper.SetEnvPrefix(AppName) |
|
175 |
viper.AutomaticEnv() |
|
176 |
||
177 |
// If a config file is found, read it in. |
|
178 |
if err := viper.ReadInConfig(); viper.GetBool("verbose") && err == nil { |
|
179 |
errPrint("Using config file: %s", viper.ConfigFileUsed()) |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
// getOutputFormat return the requested output format, defaulting to "plain". |
|
184 |
func getOutputFormat() string { |
|
185 |
of := viper.GetString("output") |
|
186 |
if of == "" { |
|
187 |
of = "plain" |
|
188 |
} |
|
189 |
// Override format if a template is provided |
|
190 |
if of == "plain" && (outputTemplate != "" || outputTemplateFile != "") { |
|
191 |
// If the format is plain and there is a template option, |
|
192 |
// set the format to "template". |
|
193 |
of = "template" |
|
194 |
} |
|
195 |
return of |
|
196 |
} |
|
197 |
||
198 |
// getPrinter returns a resource printer for the requested output format. |
|
199 |
func getPrinter() (printer.ResourcePrinter, error) { |
|
200 |
var opt string |
|
201 |
of := getOutputFormat() |
|
202 |
||
51
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
203 |
// Initialize color mode |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
204 |
switch viper.GetString("color") { |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
205 |
case "on", "yes", "force": |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
206 |
printer.ColorMode = 1 |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
207 |
case "off", "no": |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
208 |
printer.ColorMode = 2 |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
209 |
} |
300ac09051a7
Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
210 |
|
0 | 211 |
if of == "template" { |
212 |
opt = outputTemplate |
|
213 |
if outputTemplateFile != "" { |
|
214 |
tmpl, err := ioutil.ReadFile(outputTemplateFile) |
|
215 |
if err != nil { |
|
216 |
return nil, err |
|
217 |
} |
|
218 |
opt = string(tmpl) |
|
219 |
} |
|
220 |
} |
|
221 |
return printer.NewPrinter(of, opt) |
|
222 |
} |
|
223 |
||
224 |
func errPrint(format string, a ...interface{}) (n int, err error) { |
|
225 |
return fmt.Fprintf(os.Stderr, format+"\n", a...) |
|
226 |
} |