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