cmd/root.go
author rjp <zimpenfish@gmail.com>
Mon, 23 Jan 2023 16:39:02 +0000
changeset 267 5b91a65ba95a
parent 239 605a00e9d1ab
child 265 05c40b36d3b2
permissions -rw-r--r--
Update to handle non-int64 IDs Pleroma/Akkoma and GotoSocial use opaque IDs rather than `int64`s like Mastodon which means that `madon` can't talk to either of those. This commit updates everything that can be an ID to `madon.ActivityID` which is an alias for `string` - can't create a specific type for it since there's more than a few places where they're concatenated directly to strings for URLs, etc. Which means it could just as easily be a direct `string` type itself but I find that having distinct types can often make the code more readable and understandable. One extra bit is that `statusOpts` has grown a `_hasReplyTo` boolean to indicate whether the `--in-reply-to` flag was given or not because we can't distinguish because "empty because default" or "empty because given and empty". Another way around this would be to set the default to some theoretically impossible or unlikely string but you never know when someone might spin up an instance where, e.g., admin posts have negative integer IDs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
185
564d92b54b00 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 171
diff changeset
     1
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net>
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package cmd
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
import (
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	"os"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"github.com/spf13/cobra"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"github.com/spf13/viper"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
239
605a00e9d1ab Switch to Go modules (and bump Go version requirement)
Mikael Berthe <mikael@lilotux.net>
parents: 213
diff changeset
    14
	"github.com/McKael/madon/v2"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
// AppName is the CLI application name
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
const defaultConfigFile = "$HOME/.config/" + AppName + "/" + AppName + ".yaml"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
var cfgFile string
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
var safeMode bool
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
var instanceURL, appID, appSecret string
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
var login, password, token string
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
var verbose bool
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
var outputFormat string
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents: 80
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents: 80
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
// RootCmd represents the base command when called without any subcommands
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
var RootCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
	Use:               AppName,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	Short:             "A CLI utility for Mastodon API",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	PersistentPreRunE: checkOutputFormat,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
	Long: `madonctl is a CLI tool for the Mastodon REST API.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
You can use a configuration file to store common options.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
For example, create ` + defaultConfigFile + ` with the following
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
contents:
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
	---
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
	instance: "INSTANCE"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	login: "USERNAME"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	password: "USERPASSWORD"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
	...
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
The simplest way to generate a configuration file is to use the 'config dump'
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
command.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
(Configuration files in JSON are also accepted.)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
If you want shell auto-completion (for bash or zsh), you can generate the
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
completion scripts with "madonctl completion $SHELL".
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
For example if you use bash:
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
	madonctl completion bash > _bash_madonctl
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	source _bash_madonctl
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
Now you should have tab completion for subcommands and flags.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
Note: Most examples assume the user's credentials are set in the configuration
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
file.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
`,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
	Example: `  madonctl instance
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
  madonctl toot "Hello, World"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
  madonctl toot --visibility direct "@McKael Hello, You"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
  madonctl toot --visibility private --spoiler CW "The answer was 42"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
  madonctl post --file image.jpg Selfie
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
  madonctl --instance INSTANCE --login USERNAME --password PASS timeline
213
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
    99
  madonctl account notifications --list --clear
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   100
  madonctl account blocked
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   101
  madonctl account search Gargron
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
  madonctl search --resolve https://mastodon.social/@Gargron
213
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   103
  madonctl account follow 37
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   104
  madonctl account follow Gargron@mastodon.social
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   105
  madonctl account follow https://mastodon.social/@Gargron
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   106
  madonctl account --account-id 399 statuses
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
  madonctl status --status-id 416671 show
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
  madonctl status --status-id 416671 favourite
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
  madonctl status --status-id 416671 boost
213
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   110
  madonctl account show
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   111
  madonctl account show Gargron@mastodon.social
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   112
  madonctl account show -o yaml
3569475f86ec Update help & documentation
Mikael Berthe <mikael@lilotux.net>
parents: 185
diff changeset
   113
  madonctl account --account-id 1 followers --template '{{.acct}}{{"\n"}}'
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
  madonctl config whoami
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
  madonctl timeline :mastodon`,
35
61ed03c3f134 Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   116
	BashCompletionFunction: shellComplFunc,
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
// Execute adds all child commands to the root command sets flags appropriately.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
// This is called by main.main(). It only needs to happen once to the rootCmd.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
func Execute() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
	if err := RootCmd.Execute(); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
		errPrint("Error: %s", err.Error())
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
		os.Exit(-1)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
	cobra.OnInitialize(initConfig)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
	// Global flags
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
		"config file (default is "+defaultConfigFile+")")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
	RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose mode")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
	RootCmd.PersistentFlags().StringVarP(&instanceURL, "instance", "i", "", "Mastodon instance")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
	RootCmd.PersistentFlags().StringVarP(&login, "login", "L", "", "Instance user login")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
	RootCmd.PersistentFlags().StringVarP(&password, "password", "P", "", "Instance user password")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
	RootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "User token")
135
55b4a119c7c6 Update settings
Mikael Berthe <mikael@lilotux.net>
parents: 125
diff changeset
   139
	RootCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "",
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents: 80
diff changeset
   140
		"Output format (plain|json|yaml|template|theme)")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
	RootCmd.PersistentFlags().StringVar(&outputTemplate, "template", "",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
		"Go template (for output=template)")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
	RootCmd.PersistentFlags().StringVar(&outputTemplateFile, "template-file", "",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
		"Go template file (for output=template)")
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents: 80
diff changeset
   145
	RootCmd.PersistentFlags().StringVar(&outputTheme, "theme", "",
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents: 80
diff changeset
   146
		"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
   147
	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
   148
		"Color mode (auto|on|off; for output=template)")
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
	// Configuration file bindings
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   151
	viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
	viper.BindPFlag("instance", RootCmd.PersistentFlags().Lookup("instance"))
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
	viper.BindPFlag("login", RootCmd.PersistentFlags().Lookup("login"))
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
	viper.BindPFlag("password", RootCmd.PersistentFlags().Lookup("password"))
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
	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
   156
	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
   157
61ed03c3f134 Add flag shell completion (values for --output and --visibility)
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
   158
	// Flag completion
51
300ac09051a7 Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents: 45
diff changeset
   159
	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
   160
	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
   161
	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
   162
	annotationColor[cobra.BashCompCustom] = []string{"__madonctl_color"}
91
02312ccc1fd3 Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
   163
	annotationTheme := make(map[string][]string)
02312ccc1fd3 Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
   164
	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
   165
51
300ac09051a7 Add ability to force colors: option --color=auto|on|off
Mikael Berthe <mikael@lilotux.net>
parents: 45
diff changeset
   166
	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
   167
	RootCmd.PersistentFlags().Lookup("color").Annotations = annotationColor
91
02312ccc1fd3 Add shell completion for theme flag
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
   168
	RootCmd.PersistentFlags().Lookup("theme").Annotations = annotationTheme
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   169
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   170
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
// initConfig reads in config file and ENV variables if set.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
func initConfig() {
125
d436b88d137b Allow special configuration file "/dev/null"
Mikael Berthe <mikael@lilotux.net>
parents: 91
diff changeset
   173
	if cfgFile == "/dev/null" {
d436b88d137b Allow special configuration file "/dev/null"
Mikael Berthe <mikael@lilotux.net>
parents: 91
diff changeset
   174
		return
d436b88d137b Allow special configuration file "/dev/null"
Mikael Berthe <mikael@lilotux.net>
parents: 91
diff changeset
   175
	}
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   176
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   177
	viper.SetConfigName(AppName) // name of config file (without extension)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   178
	viper.AddConfigPath("$HOME/.config/" + AppName)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   179
	viper.AddConfigPath("$HOME/." + AppName)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
	// Read in environment variables that match, with a prefix
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   182
	viper.SetEnvPrefix(AppName)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   183
	viper.AutomaticEnv()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   184
171
611c211534d0 Fix --config flag
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
   185
	// Enable ability to specify config file via flag
611c211534d0 Fix --config flag
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
   186
	viper.SetConfigFile(cfgFile)
611c211534d0 Fix --config flag
Mikael Berthe <mikael@lilotux.net>
parents: 135
diff changeset
   187
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   188
	// 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
   189
	err := viper.ReadInConfig()
0491cc43911e Exit with error if the requested configuration file does not exist
Mikael Berthe <mikael@lilotux.net>
parents: 55
diff changeset
   190
	if err != nil {
0491cc43911e Exit with error if the requested configuration file does not exist
Mikael Berthe <mikael@lilotux.net>
parents: 55
diff changeset
   191
		if cfgFile != "" {
66
43d43c8b53aa Be more verbose when configuration file cannot be read
Mikael Berthe <mikael@lilotux.net>
parents: 65
diff changeset
   192
			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
   193
			os.Exit(-1)
0491cc43911e Exit with error if the requested configuration file does not exist
Mikael Berthe <mikael@lilotux.net>
parents: 55
diff changeset
   194
		}
0491cc43911e Exit with error if the requested configuration file does not exist
Mikael Berthe <mikael@lilotux.net>
parents: 55
diff changeset
   195
	} else if viper.GetBool("verbose") {
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   196
		errPrint("Using config file: %s", viper.ConfigFileUsed())
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   197
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   198
}