cmd/status.go
changeset 0 5abace724584
child 13 f862af8faf17
equal deleted inserted replaced
-1:000000000000 0:5abace724584
       
     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 	"errors"
       
    10 	"strings"
       
    11 
       
    12 	"github.com/spf13/cobra"
       
    13 
       
    14 	"github.com/McKael/madon"
       
    15 )
       
    16 
       
    17 var statusOpts struct {
       
    18 	statusID int
       
    19 	unset    bool
       
    20 
       
    21 	// The following fields are used for the post/toot command
       
    22 	visibility  string
       
    23 	sensitive   bool
       
    24 	spoiler     string
       
    25 	inReplyToID int
       
    26 	filePath    string
       
    27 	mediaIDs    string
       
    28 
       
    29 	// TODO
       
    30 	limit int
       
    31 }
       
    32 
       
    33 func init() {
       
    34 	RootCmd.AddCommand(statusCmd)
       
    35 
       
    36 	// Subcommands
       
    37 	statusCmd.AddCommand(statusSubcommands...)
       
    38 
       
    39 	// Global flags
       
    40 	statusCmd.PersistentFlags().IntVarP(&statusOpts.statusID, "status-id", "s", 0, "Status ID number")
       
    41 	//statusCmd.PersistentFlags().IntVarP(&statusOpts.limit, "limit", "l", 0, "Limit number of results")
       
    42 
       
    43 	statusCmd.MarkPersistentFlagRequired("status-id")
       
    44 
       
    45 	// Subcommand flags
       
    46 	statusReblogSubcommand.Flags().BoolVar(&statusOpts.unset, "unset", false, "Unreblog the status")
       
    47 	statusFavouriteSubcommand.Flags().BoolVar(&statusOpts.unset, "unset", false, "Remove the status from the favourites")
       
    48 	statusPostSubcommand.Flags().BoolVar(&statusOpts.sensitive, "sensitive", false, "Mark post as sensitive (NSFW)")
       
    49 	statusPostSubcommand.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)")
       
    50 	statusPostSubcommand.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)")
       
    51 	statusPostSubcommand.Flags().StringVar(&statusOpts.mediaIDs, "media-ids", "", "Comma-separated list of media IDs")
       
    52 	statusPostSubcommand.Flags().StringVarP(&statusOpts.filePath, "file", "f", "", "File name")
       
    53 	statusPostSubcommand.Flags().IntVarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
       
    54 }
       
    55 
       
    56 // statusCmd represents the status command
       
    57 // This command does nothing without a subcommand
       
    58 var statusCmd = &cobra.Command{
       
    59 	Use:     "status --status-id ID subcommand",
       
    60 	Aliases: []string{"st"},
       
    61 	Short:   "Get status details",
       
    62 	//Long:    `TBW...`, // TODO
       
    63 	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
       
    64 		// This is common to status and all status subcommands but "post"
       
    65 		if statusOpts.statusID < 1 && cmd.Name() != "post" {
       
    66 			return errors.New("missing status ID")
       
    67 		}
       
    68 		if err := madonInit(true); err != nil {
       
    69 			return err
       
    70 		}
       
    71 		return nil
       
    72 	},
       
    73 }
       
    74 
       
    75 var statusSubcommands = []*cobra.Command{
       
    76 	&cobra.Command{
       
    77 		Use:     "show",
       
    78 		Aliases: []string{"display"},
       
    79 		Short:   "Get the status",
       
    80 		RunE: func(cmd *cobra.Command, args []string) error {
       
    81 			return statusSubcommandRunE(cmd.Name(), args)
       
    82 		},
       
    83 	},
       
    84 	&cobra.Command{
       
    85 		Use:   "context",
       
    86 		Short: "Get the status context",
       
    87 		RunE: func(cmd *cobra.Command, args []string) error {
       
    88 			return statusSubcommandRunE(cmd.Name(), args)
       
    89 		},
       
    90 	},
       
    91 	&cobra.Command{
       
    92 		Use:   "card",
       
    93 		Short: "Get the status card",
       
    94 		RunE: func(cmd *cobra.Command, args []string) error {
       
    95 			return statusSubcommandRunE(cmd.Name(), args)
       
    96 		},
       
    97 	},
       
    98 	&cobra.Command{
       
    99 		Use:   "reblogged-by",
       
   100 		Short: "Display accounts which reblogged the status",
       
   101 		RunE: func(cmd *cobra.Command, args []string) error {
       
   102 			return statusSubcommandRunE(cmd.Name(), args)
       
   103 		},
       
   104 	},
       
   105 	&cobra.Command{
       
   106 		Use:     "favourited-by",
       
   107 		Aliases: []string{"favorited-by"},
       
   108 		Short:   "Display accounts which favourited the status",
       
   109 		RunE: func(cmd *cobra.Command, args []string) error {
       
   110 			return statusSubcommandRunE(cmd.Name(), args)
       
   111 		},
       
   112 	},
       
   113 	&cobra.Command{
       
   114 		Use:     "delete",
       
   115 		Aliases: []string{"rm"},
       
   116 		Short:   "Delete the status",
       
   117 		RunE: func(cmd *cobra.Command, args []string) error {
       
   118 			return statusSubcommandRunE(cmd.Name(), args)
       
   119 		},
       
   120 	},
       
   121 	statusReblogSubcommand,
       
   122 	statusFavouriteSubcommand,
       
   123 	statusPostSubcommand,
       
   124 }
       
   125 
       
   126 var statusReblogSubcommand = &cobra.Command{
       
   127 	Use:     "boost",
       
   128 	Aliases: []string{"reblog"},
       
   129 	Short:   "Boost (reblog) or unreblog the status",
       
   130 	RunE: func(cmd *cobra.Command, args []string) error {
       
   131 		return statusSubcommandRunE(cmd.Name(), args)
       
   132 	},
       
   133 }
       
   134 
       
   135 var statusFavouriteSubcommand = &cobra.Command{
       
   136 	Use:     "favourite",
       
   137 	Aliases: []string{"favorite", "fave"},
       
   138 	Short:   "Mark/unmark the status as favourite",
       
   139 	RunE: func(cmd *cobra.Command, args []string) error {
       
   140 		return statusSubcommandRunE(cmd.Name(), args)
       
   141 	},
       
   142 }
       
   143 
       
   144 var statusPostSubcommand = &cobra.Command{
       
   145 	Use:     "post",
       
   146 	Aliases: []string{"toot", "pouet"},
       
   147 	Short:   "Post a message (same as 'madonctl toot')",
       
   148 	Example: `  madonctl status post --spoiler Warning "Hello, World"
       
   149   madonctl status toot --sensitive --file image.jpg Image
       
   150   madonctl status post --media-ids ID1,ID2,ID3 Image`,
       
   151 	RunE: func(cmd *cobra.Command, args []string) error {
       
   152 		return statusSubcommandRunE(cmd.Name(), args)
       
   153 	},
       
   154 }
       
   155 
       
   156 func statusSubcommandRunE(subcmd string, args []string) error {
       
   157 	opt := statusOpts
       
   158 
       
   159 	var obj interface{}
       
   160 	var err error
       
   161 
       
   162 	switch subcmd {
       
   163 	case "show":
       
   164 		var status *madon.Status
       
   165 		status, err = gClient.GetStatus(opt.statusID)
       
   166 		obj = status
       
   167 	case "context":
       
   168 		var context *madon.Context
       
   169 		context, err = gClient.GetStatusContext(opt.statusID)
       
   170 		obj = context
       
   171 	case "card":
       
   172 		var context *madon.Card
       
   173 		context, err = gClient.GetStatusCard(opt.statusID)
       
   174 		obj = context
       
   175 	case "reblogged-by":
       
   176 		var accountList []madon.Account
       
   177 		accountList, err = gClient.GetStatusRebloggedBy(opt.statusID)
       
   178 		obj = accountList
       
   179 	case "favourited-by":
       
   180 		var accountList []madon.Account
       
   181 		accountList, err = gClient.GetStatusFavouritedBy(opt.statusID)
       
   182 		obj = accountList
       
   183 	case "delete":
       
   184 		err = gClient.DeleteStatus(opt.statusID)
       
   185 	case "boost":
       
   186 		if opt.unset {
       
   187 			err = gClient.UnreblogStatus(opt.statusID)
       
   188 		} else {
       
   189 			err = gClient.ReblogStatus(opt.statusID)
       
   190 		}
       
   191 	case "favourite":
       
   192 		if opt.unset {
       
   193 			err = gClient.UnfavouriteStatus(opt.statusID)
       
   194 		} else {
       
   195 			err = gClient.FavouriteStatus(opt.statusID)
       
   196 		}
       
   197 	case "post": // toot
       
   198 		var s *madon.Status
       
   199 		s, err = toot(strings.Join(args, " "))
       
   200 		obj = s
       
   201 	default:
       
   202 		return errors.New("statusSubcommand: internal error")
       
   203 	}
       
   204 
       
   205 	if err != nil {
       
   206 		errPrint("Error: %s", err.Error())
       
   207 		return nil
       
   208 	}
       
   209 	if obj == nil {
       
   210 		return nil
       
   211 	}
       
   212 
       
   213 	p, err := getPrinter()
       
   214 	if err != nil {
       
   215 		return err
       
   216 	}
       
   217 	return p.PrintObj(obj, nil, "")
       
   218 }