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