cmd/toot.go
changeset 79 bca27c55be9f
parent 71 67e3234c26a9
child 129 11966471aac3
equal deleted inserted replaced
78:b40bc3a8b572 79:bca27c55be9f
     6 package cmd
     6 package cmd
     7 
     7 
     8 import (
     8 import (
     9 	"github.com/pkg/errors"
     9 	"github.com/pkg/errors"
    10 	"github.com/spf13/cobra"
    10 	"github.com/spf13/cobra"
       
    11 	"github.com/spf13/viper"
    11 
    12 
    12 	"github.com/McKael/madon"
    13 	"github.com/McKael/madon"
    13 )
    14 )
    14 
    15 
    15 // toot is a kind of alias for status post
    16 // toot is a kind of alias for status post
    40 	Example: `  madonctl toot message
    41 	Example: `  madonctl toot message
    41   madonctl toot --spoiler Warning "Hello, World"
    42   madonctl toot --spoiler Warning "Hello, World"
    42   madonctl status post --media-ids ID1,ID2 "Here are the photos"
    43   madonctl status post --media-ids ID1,ID2 "Here are the photos"
    43   madonctl post --sensitive --file image.jpg Image
    44   madonctl post --sensitive --file image.jpg Image
    44   madonctl toot --text-file message.txt
    45   madonctl toot --text-file message.txt
    45   echo "Hello from #madonctl" | madonctl toot --visibility unlisted --stdin`,
    46   echo "Hello from #madonctl" | madonctl toot --visibility unlisted --stdin
       
    47 
       
    48 The default visibility can be set in the configuration file with the option
       
    49 'default_visibility' (or with an environmnent variable).`,
    46 	RunE: func(cmd *cobra.Command, args []string) error {
    50 	RunE: func(cmd *cobra.Command, args []string) error {
    47 		if err := madonInit(true); err != nil {
    51 		if err := madonInit(true); err != nil {
    48 			return err
    52 			return err
    49 		}
    53 		}
    50 		return statusSubcommandRunE("post", args)
    54 		return statusSubcommandRunE("post", args)
    52 }
    56 }
    53 
    57 
    54 func toot(tootText string) (*madon.Status, error) {
    58 func toot(tootText string) (*madon.Status, error) {
    55 	opt := statusOpts
    59 	opt := statusOpts
    56 
    60 
       
    61 	// Get default visibility from configuration
       
    62 	if opt.visibility == "" {
       
    63 		if v := viper.GetString("default_visibility"); v != "" {
       
    64 			opt.visibility = v
       
    65 		}
       
    66 	}
       
    67 
    57 	switch opt.visibility {
    68 	switch opt.visibility {
    58 	case "", "direct", "private", "unlisted", "public":
    69 	case "", "direct", "private", "unlisted", "public":
    59 		// OK
    70 		// OK
    60 	default:
    71 	default:
    61 		return nil, errors.New("invalid visibility argument value")
    72 		return nil, errors.Errorf("invalid visibility argument value '%s'", opt.visibility)
    62 	}
    73 	}
    63 
    74 
    64 	if opt.inReplyToID < 0 {
    75 	if opt.inReplyToID < 0 {
    65 		return nil, errors.New("invalid in-reply-to argument value")
    76 		return nil, errors.New("invalid in-reply-to argument value")
    66 	}
    77 	}