cmd/media.go
changeset 202 1c058699177b
parent 201 5cb568653fc7
child 239 605a00e9d1ab
equal deleted inserted replaced
201:5cb568653fc7 202:1c058699177b
     8 import (
     8 import (
     9 	"os"
     9 	"os"
    10 
    10 
    11 	"github.com/pkg/errors"
    11 	"github.com/pkg/errors"
    12 	"github.com/spf13/cobra"
    12 	"github.com/spf13/cobra"
       
    13 	flag "github.com/spf13/pflag"
       
    14 
       
    15 	"github.com/McKael/madon"
    13 )
    16 )
    14 
    17 
       
    18 var mediaFlags *flag.FlagSet
       
    19 
    15 var mediaOpts struct {
    20 var mediaOpts struct {
       
    21 	mediaID     int64
    16 	filePath    string
    22 	filePath    string
    17 	description string
    23 	description string
    18 	focus       string
    24 	focus       string
    19 }
    25 }
    20 
    26 
    21 // mediaCmd represents the media command
    27 // mediaCmd represents the media command
    22 var mediaCmd = &cobra.Command{
    28 var mediaCmd = &cobra.Command{
    23 	Use:     "media --file FILENAME",
    29 	Use:     "media --file FILENAME",
    24 	Aliases: []string{"upload"},
    30 	Aliases: []string{"upload"},
    25 	Short:   "Upload a media attachment",
    31 	Short:   "Upload or update a media attachment",
    26 	//Long: `TBW...`,
    32 	Long: `Upload or update a media attachment
       
    33 
       
    34 This command can be used to upload media that will be attached to
       
    35 a status later.
       
    36 
       
    37 A media description or focal point (focus) can be updated
       
    38 as long as it is not yet attached to a status, with the '--update MEDIA_ID'
       
    39 option.`,
       
    40 	Example: `  madonctl upload --file FILENAME
       
    41   madonctl media --file FILENAME --description "My screenshot"
       
    42   madonctl media --update 3217821 --focus "0.5,-0.7"
       
    43   madonctl media --update 2468123 --description "Winter Snow"`,
    27 	RunE: mediaRunE,
    44 	RunE: mediaRunE,
    28 }
    45 }
    29 
    46 
    30 func init() {
    47 func init() {
    31 	RootCmd.AddCommand(mediaCmd)
    48 	RootCmd.AddCommand(mediaCmd)
    32 
    49 
    33 	mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file")
    50 	mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file")
    34 	mediaCmd.MarkFlagRequired("file")
    51 	mediaCmd.Flags().Int64Var(&mediaOpts.mediaID, "update", 0, "Media to update (ID)")
    35 
    52 
    36 	mediaCmd.Flags().StringVar(&mediaOpts.description, "description", "", "Plain text description")
    53 	mediaCmd.Flags().StringVar(&mediaOpts.description, "description", "", "Plain text description")
    37 	mediaCmd.Flags().StringVar(&mediaOpts.focus, "focus", "", "Focal point")
    54 	mediaCmd.Flags().StringVar(&mediaOpts.focus, "focus", "", "Focal point")
       
    55 
       
    56 	// This will be used to check if the options were explicitly set or not
       
    57 	mediaFlags = mediaCmd.Flags()
    38 }
    58 }
    39 
    59 
    40 func mediaRunE(cmd *cobra.Command, args []string) error {
    60 func mediaRunE(cmd *cobra.Command, args []string) error {
    41 	opt := mediaOpts
    61 	opt := mediaOpts
    42 
    62 
    43 	if opt.filePath == "" {
    63 	if opt.filePath == "" {
    44 		return errors.New("no media file name provided")
    64 		if opt.mediaID < 1 {
       
    65 			return errors.New("no media file name provided")
       
    66 		}
       
    67 	} else if opt.mediaID > 0 {
       
    68 		return errors.New("cannot use both --file and --update")
    45 	}
    69 	}
    46 
    70 
    47 	if err := madonInit(true); err != nil {
    71 	if err := madonInit(true); err != nil {
    48 		return err
    72 		return err
    49 	}
    73 	}
    50 
    74 
    51 	attachment, err := gClient.UploadMedia(opt.filePath, opt.description, opt.focus)
    75 	var attachment *madon.Attachment
       
    76 	var err error
       
    77 
       
    78 	if opt.filePath != "" {
       
    79 		attachment, err = gClient.UploadMedia(opt.filePath, opt.description, opt.focus)
       
    80 	} else {
       
    81 		// Update
       
    82 		var desc, foc *string
       
    83 		if mediaFlags.Lookup("description").Changed {
       
    84 			desc = &opt.description
       
    85 		}
       
    86 		if mediaFlags.Lookup("focus").Changed {
       
    87 			foc = &opt.focus
       
    88 		}
       
    89 		attachment, err = gClient.UpdateMedia(opt.mediaID, desc, foc)
       
    90 	}
    52 	if err != nil {
    91 	if err != nil {
    53 		errPrint("Error: %s", err.Error())
    92 		errPrint("Error: %s", err.Error())
    54 		os.Exit(1)
    93 		os.Exit(1)
    55 	}
    94 	}
    56 
    95