|
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 |
|
11 "github.com/spf13/cobra" |
|
12 ) |
|
13 |
|
14 var mediaOpts struct { |
|
15 filePath string |
|
16 } |
|
17 |
|
18 // mediaCmd represents the media command |
|
19 var mediaCmd = &cobra.Command{ |
|
20 Use: "media --file FILENAME", |
|
21 Aliases: []string{"upload"}, |
|
22 Short: "Upload a media attachment", |
|
23 //Long: `TBW...`, |
|
24 RunE: mediaRunE, |
|
25 } |
|
26 |
|
27 func init() { |
|
28 RootCmd.AddCommand(mediaCmd) |
|
29 |
|
30 mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file") |
|
31 mediaCmd.MarkFlagRequired("file") |
|
32 } |
|
33 |
|
34 func mediaRunE(cmd *cobra.Command, args []string) error { |
|
35 opt := mediaOpts |
|
36 |
|
37 if opt.filePath == "" { |
|
38 return errors.New("no media file name provided") |
|
39 } |
|
40 |
|
41 if err := madonInit(true); err != nil { |
|
42 return err |
|
43 } |
|
44 |
|
45 attachment, err := gClient.UploadMedia(opt.filePath) |
|
46 if err != nil { |
|
47 errPrint("Error: %s", err.Error()) |
|
48 return nil |
|
49 } |
|
50 |
|
51 p, err := getPrinter() |
|
52 if err != nil { |
|
53 return err |
|
54 } |
|
55 return p.PrintObj(attachment, nil, "") |
|
56 } |
|
57 |
|
58 // uploadFile uploads a media file and returns the attachment ID |
|
59 func uploadFile(filePath string) (int, error) { |
|
60 attachment, err := gClient.UploadMedia(filePath) |
|
61 if err != nil { |
|
62 return 0, err |
|
63 } |
|
64 if attachment == nil { |
|
65 return 0, nil |
|
66 } |
|
67 return attachment.ID, nil |
|
68 } |