Add madonctl media --update option
authorMikael Berthe <mikael@lilotux.net>
Tue, 20 Mar 2018 21:53:57 +0100
changeset 202 1c058699177b
parent 201 5cb568653fc7
child 203 71455784278b
Add madonctl media --update option
cmd/media.go
--- a/cmd/media.go	Tue Mar 20 20:52:11 2018 +0100
+++ b/cmd/media.go	Tue Mar 20 21:53:57 2018 +0100
@@ -10,9 +10,15 @@
 
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
+	flag "github.com/spf13/pflag"
+
+	"github.com/McKael/madon"
 )
 
+var mediaFlags *flag.FlagSet
+
 var mediaOpts struct {
+	mediaID     int64
 	filePath    string
 	description string
 	focus       string
@@ -22,8 +28,19 @@
 var mediaCmd = &cobra.Command{
 	Use:     "media --file FILENAME",
 	Aliases: []string{"upload"},
-	Short:   "Upload a media attachment",
-	//Long: `TBW...`,
+	Short:   "Upload or update a media attachment",
+	Long: `Upload or update a media attachment
+
+This command can be used to upload media that will be attached to
+a status later.
+
+A media description or focal point (focus) can be updated
+as long as it is not yet attached to a status, with the '--update MEDIA_ID'
+option.`,
+	Example: `  madonctl upload --file FILENAME
+  madonctl media --file FILENAME --description "My screenshot"
+  madonctl media --update 3217821 --focus "0.5,-0.7"
+  madonctl media --update 2468123 --description "Winter Snow"`,
 	RunE: mediaRunE,
 }
 
@@ -31,24 +48,46 @@
 	RootCmd.AddCommand(mediaCmd)
 
 	mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file")
-	mediaCmd.MarkFlagRequired("file")
+	mediaCmd.Flags().Int64Var(&mediaOpts.mediaID, "update", 0, "Media to update (ID)")
 
 	mediaCmd.Flags().StringVar(&mediaOpts.description, "description", "", "Plain text description")
 	mediaCmd.Flags().StringVar(&mediaOpts.focus, "focus", "", "Focal point")
+
+	// This will be used to check if the options were explicitly set or not
+	mediaFlags = mediaCmd.Flags()
 }
 
 func mediaRunE(cmd *cobra.Command, args []string) error {
 	opt := mediaOpts
 
 	if opt.filePath == "" {
-		return errors.New("no media file name provided")
+		if opt.mediaID < 1 {
+			return errors.New("no media file name provided")
+		}
+	} else if opt.mediaID > 0 {
+		return errors.New("cannot use both --file and --update")
 	}
 
 	if err := madonInit(true); err != nil {
 		return err
 	}
 
-	attachment, err := gClient.UploadMedia(opt.filePath, opt.description, opt.focus)
+	var attachment *madon.Attachment
+	var err error
+
+	if opt.filePath != "" {
+		attachment, err = gClient.UploadMedia(opt.filePath, opt.description, opt.focus)
+	} else {
+		// Update
+		var desc, foc *string
+		if mediaFlags.Lookup("description").Changed {
+			desc = &opt.description
+		}
+		if mediaFlags.Lookup("focus").Changed {
+			foc = &opt.focus
+		}
+		attachment, err = gClient.UpdateMedia(opt.mediaID, desc, foc)
+	}
 	if err != nil {
 		errPrint("Error: %s", err.Error())
 		os.Exit(1)