Add default visibility setting
authorMikael Berthe <mikael@lilotux.net>
Sun, 07 May 2017 00:26:04 +0200
changeset 79 bca27c55be9f
parent 78 b40bc3a8b572
child 80 7f3e2577ed85
Add default visibility setting
README.md
cmd/status.go
cmd/toot.go
--- a/README.md	Sat May 06 20:26:12 2017 +0200
+++ b/README.md	Sun May 07 00:26:04 2017 +0200
@@ -48,6 +48,8 @@
 
 If you only provide the Mastodon instance, it will generate a configuration file with an application ID/secret for this instance and you will have to add the user credentials.
 
+Note that every variable from the configration file can also be set with an environment variable (e.g. `export MADONCTL_INSTANCE='https://mamot.fr'`).
+
 ### Usage
 
 The complete list of commands is available in the online help (`madonctl help`, `madonctl command --help`...)
@@ -66,6 +68,9 @@
 % madonctl toot --visibility private --spoiler CW "The answer was 42"
 % madonctl post --file image.jpg Selfie # Send a media file
 ```
+Note: The default toot visibility can be set in the configuration file with
+the `default_visibility` setting or with the environment variable (example
+`export MADONCTL_DEFAULT_VISIBILITY=unlisted`).
 
 Send (text) file content as new message:
 ```
--- a/cmd/status.go	Sat May 06 20:26:12 2017 +0200
+++ b/cmd/status.go	Sun May 07 00:26:04 2017 +0200
@@ -166,7 +166,10 @@
   madonctl status toot --sensitive --file image.jpg Image
   madonctl status post --media-ids ID1,ID2,ID3 Image
   madonctl status toot --text-file message.txt
-  echo "Hello from #madonctl" | madonctl status toot --stdin`,
+  echo "Hello from #madonctl" | madonctl status toot --stdin
+
+The default visibility can be set in the configuration file with the option
+'default_visibility' (or with an environmnent variable).`,
 	RunE: func(cmd *cobra.Command, args []string) error {
 		return statusSubcommandRunE(cmd.Name(), args)
 	},
--- a/cmd/toot.go	Sat May 06 20:26:12 2017 +0200
+++ b/cmd/toot.go	Sun May 07 00:26:04 2017 +0200
@@ -8,6 +8,7 @@
 import (
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
+	"github.com/spf13/viper"
 
 	"github.com/McKael/madon"
 )
@@ -42,7 +43,10 @@
   madonctl status post --media-ids ID1,ID2 "Here are the photos"
   madonctl post --sensitive --file image.jpg Image
   madonctl toot --text-file message.txt
-  echo "Hello from #madonctl" | madonctl toot --visibility unlisted --stdin`,
+  echo "Hello from #madonctl" | madonctl toot --visibility unlisted --stdin
+
+The default visibility can be set in the configuration file with the option
+'default_visibility' (or with an environmnent variable).`,
 	RunE: func(cmd *cobra.Command, args []string) error {
 		if err := madonInit(true); err != nil {
 			return err
@@ -54,11 +58,18 @@
 func toot(tootText string) (*madon.Status, error) {
 	opt := statusOpts
 
+	// Get default visibility from configuration
+	if opt.visibility == "" {
+		if v := viper.GetString("default_visibility"); v != "" {
+			opt.visibility = v
+		}
+	}
+
 	switch opt.visibility {
 	case "", "direct", "private", "unlisted", "public":
 		// OK
 	default:
-		return nil, errors.New("invalid visibility argument value")
+		return nil, errors.Errorf("invalid visibility argument value '%s'", opt.visibility)
 	}
 
 	if opt.inReplyToID < 0 {