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