author | convert-repo |
Fri, 05 May 2017 21:55:09 +0000 | |
changeset 69 | 7217174c217a |
parent 47 | 82d8b6074309 |
child 81 | b1671f83e91b |
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 { |
|
54 |
return err |
|
55 |
} |
|
56 |
return p.PrintObj(attachment, nil, "") |
|
57 |
} |
|
58 |
||
59 |
// 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
|
60 |
func uploadFile(filePath string) (int64, error) { |
0 | 61 |
attachment, err := gClient.UploadMedia(filePath) |
62 |
if err != nil { |
|
63 |
return 0, err |
|
64 |
} |
|
65 |
if attachment == nil { |
|
66 |
return 0, nil |
|
67 |
} |
|
68 |
return attachment.ID, nil |
|
69 |
} |