author | Mikael Berthe <mikael@lilotux.net> |
Tue, 09 May 2017 20:14:28 +0200 | |
changeset 110 | 57843255fd1a |
parent 81 | b1671f83e91b |
child 185 | 564d92b54b00 |
permissions | -rw-r--r-- |
0 | 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 ( |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
9 |
"os" |
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
10 |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
11 |
"github.com/pkg/errors" |
0 | 12 |
"github.com/spf13/cobra" |
13 |
) |
|
14 |
||
15 |
var mediaOpts struct { |
|
16 |
filePath string |
|
17 |
} |
|
18 |
||
19 |
// mediaCmd represents the media command |
|
20 |
var mediaCmd = &cobra.Command{ |
|
21 |
Use: "media --file FILENAME", |
|
22 |
Aliases: []string{"upload"}, |
|
23 |
Short: "Upload a media attachment", |
|
24 |
//Long: `TBW...`, |
|
25 |
RunE: mediaRunE, |
|
26 |
} |
|
27 |
||
28 |
func init() { |
|
29 |
RootCmd.AddCommand(mediaCmd) |
|
30 |
||
31 |
mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file") |
|
32 |
mediaCmd.MarkFlagRequired("file") |
|
33 |
} |
|
34 |
||
35 |
func mediaRunE(cmd *cobra.Command, args []string) error { |
|
36 |
opt := mediaOpts |
|
37 |
||
38 |
if opt.filePath == "" { |
|
39 |
return errors.New("no media file name provided") |
|
40 |
} |
|
41 |
||
42 |
if err := madonInit(true); err != nil { |
|
43 |
return err |
|
44 |
} |
|
45 |
||
46 |
attachment, err := gClient.UploadMedia(opt.filePath) |
|
47 |
if err != nil { |
|
48 |
errPrint("Error: %s", err.Error()) |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
49 |
os.Exit(1) |
0 | 50 |
} |
51 |
||
52 |
p, err := getPrinter() |
|
53 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
54 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
55 |
os.Exit(1) |
0 | 56 |
} |
110 | 57 |
return p.printObj(attachment) |
0 | 58 |
} |
59 |
||
60 |
// uploadFile uploads a media file and returns the attachment ID |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
61 |
func uploadFile(filePath string) (int64, error) { |
0 | 62 |
attachment, err := gClient.UploadMedia(filePath) |
63 |
if err != nil { |
|
64 |
return 0, err |
|
65 |
} |
|
66 |
if attachment == nil { |
|
67 |
return 0, nil |
|
68 |
} |
|
69 |
return attachment.ID, nil |
|
70 |
} |