cmd/toot.go
changeset 267 5b91a65ba95a
parent 239 605a00e9d1ab
child 268 4dd196a4ee7c
equal deleted inserted replaced
264:8f478162d991 267:5b91a65ba95a
    27 	tootAliasCmd.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)")
    27 	tootAliasCmd.Flags().StringVar(&statusOpts.visibility, "visibility", "", "Visibility (direct|private|unlisted|public)")
    28 	tootAliasCmd.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)")
    28 	tootAliasCmd.Flags().StringVar(&statusOpts.spoiler, "spoiler", "", "Spoiler warning (CW)")
    29 	tootAliasCmd.Flags().StringVar(&statusOpts.mediaIDs, "media-ids", "", "Comma-separated list of media IDs")
    29 	tootAliasCmd.Flags().StringVar(&statusOpts.mediaIDs, "media-ids", "", "Comma-separated list of media IDs")
    30 	tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name")
    30 	tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name")
    31 	tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
    31 	tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
    32 	tootAliasCmd.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
    32 	tootAliasCmd.Flags().StringVarP(&statusOpts.inReplyToID, "in-reply-to", "r", "", "Status ID to reply to")
    33 	tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
    33 	tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
    34 	tootAliasCmd.Flags().BoolVar(&statusOpts.addMentions, "add-mentions", false, "Add mentions when replying")
    34 	tootAliasCmd.Flags().BoolVar(&statusOpts.addMentions, "add-mentions", false, "Add mentions when replying")
    35 	tootAliasCmd.Flags().BoolVar(&statusOpts.sameVisibility, "same-visibility", false, "Use same visibility as original message (for replies)")
    35 	tootAliasCmd.Flags().BoolVar(&statusOpts.sameVisibility, "same-visibility", false, "Use same visibility as original message (for replies)")
    36 
    36 
    37 	// Flag completion
    37 	// Flag completion
    62 'default_visibility' (or with an environmnent variable).`,
    62 'default_visibility' (or with an environmnent variable).`,
    63 	RunE: func(cmd *cobra.Command, args []string) error {
    63 	RunE: func(cmd *cobra.Command, args []string) error {
    64 		if err := madonInit(true); err != nil {
    64 		if err := madonInit(true); err != nil {
    65 			return err
    65 			return err
    66 		}
    66 		}
       
    67 		// Update the extra flag to reflect if `in-reply-to` was present or not
       
    68 		statusOpts._hasReplyTo = cmd.Flags().Lookup("in-reply-to").Changed
    67 		return statusSubcommandRunE("post", args)
    69 		return statusSubcommandRunE("post", args)
    68 	},
    70 	},
    69 }
    71 }
    70 
    72 
    71 func toot(tootText string) (*madon.Status, error) {
    73 func toot(tootText string) (*madon.Status, error) {
    83 		// OK
    85 		// OK
    84 	default:
    86 	default:
    85 		return nil, errors.Errorf("invalid visibility argument value '%s'", opt.visibility)
    87 		return nil, errors.Errorf("invalid visibility argument value '%s'", opt.visibility)
    86 	}
    88 	}
    87 
    89 
    88 	if opt.inReplyToID < 0 {
    90 	// Bit of a fudge but there's no easy way to tell if a string flag
       
    91 	// is empty by default or empty by assignment.  Can't use a pointer
       
    92 	// and have `nil` be "unset" because Cobra will crash if we send it
       
    93 	// a `nil` as the recepient for a flag variable.  Hence using an
       
    94 	// extra struct member as a flag to indicate set/unset.
       
    95 	if opt._hasReplyTo && opt.inReplyToID == "" {
    89 		return nil, errors.New("invalid in-reply-to argument value")
    96 		return nil, errors.New("invalid in-reply-to argument value")
    90 	}
    97 	}
    91 
    98 
    92 	ids, err := splitIDs(opt.mediaIDs)
    99 	ids, err := splitIDs(opt.mediaIDs)
    93 	if err != nil {
   100 	if err != nil {
    96 
   103 
    97 	if tootText == "" && len(ids) == 0 && opt.spoiler == "" && opt.mediaFilePath == "" {
   104 	if tootText == "" && len(ids) == 0 && opt.spoiler == "" && opt.mediaFilePath == "" {
    98 		return nil, errors.New("toot is empty")
   105 		return nil, errors.New("toot is empty")
    99 	}
   106 	}
   100 
   107 
   101 	if opt.inReplyToID > 0 {
   108 	if opt.inReplyToID != "" {
   102 		var initialStatus *madon.Status
   109 		var initialStatus *madon.Status
   103 		var preserveVis bool
   110 		var preserveVis bool
   104 		if opt.sameVisibility &&
   111 		if opt.sameVisibility &&
   105 			!tootAliasFlags.Lookup("visibility").Changed &&
   112 			!tootAliasFlags.Lookup("visibility").Changed &&
   106 			!statusPostFlags.Lookup("visibility").Changed {
   113 			!statusPostFlags.Lookup("visibility").Changed {
   141 
   148 
   142 		fileMediaID, err := uploadFile(opt.mediaFilePath)
   149 		fileMediaID, err := uploadFile(opt.mediaFilePath)
   143 		if err != nil {
   150 		if err != nil {
   144 			return nil, errors.Wrap(err, "cannot attach media file")
   151 			return nil, errors.Wrap(err, "cannot attach media file")
   145 		}
   152 		}
   146 		if fileMediaID > 0 {
   153 		if fileMediaID != "" {
   147 			ids = append(ids, fileMediaID)
   154 			ids = append(ids, fileMediaID)
   148 		}
   155 		}
   149 	}
   156 	}
   150 
   157 
   151 	postParam := madon.PostStatusParams{
   158 	postParam := madon.PostStatusParams{