author | Mikael Berthe <mikael@lilotux.net> |
Tue, 20 Mar 2018 20:52:11 +0100 | |
changeset 201 | 5cb568653fc7 |
parent 185 | 564d92b54b00 |
child 202 | 1c058699177b |
permissions | -rw-r--r-- |
185 | 1 |
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net> |
0 | 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 { |
|
201
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
16 |
filePath string |
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
17 |
description string |
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
18 |
focus string |
0 | 19 |
} |
20 |
||
21 |
// mediaCmd represents the media command |
|
22 |
var mediaCmd = &cobra.Command{ |
|
23 |
Use: "media --file FILENAME", |
|
24 |
Aliases: []string{"upload"}, |
|
25 |
Short: "Upload a media attachment", |
|
26 |
//Long: `TBW...`, |
|
27 |
RunE: mediaRunE, |
|
28 |
} |
|
29 |
||
30 |
func init() { |
|
31 |
RootCmd.AddCommand(mediaCmd) |
|
32 |
||
33 |
mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file") |
|
34 |
mediaCmd.MarkFlagRequired("file") |
|
201
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
35 |
|
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
36 |
mediaCmd.Flags().StringVar(&mediaOpts.description, "description", "", "Plain text description") |
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
37 |
mediaCmd.Flags().StringVar(&mediaOpts.focus, "focus", "", "Focal point") |
0 | 38 |
} |
39 |
||
40 |
func mediaRunE(cmd *cobra.Command, args []string) error { |
|
41 |
opt := mediaOpts |
|
42 |
||
43 |
if opt.filePath == "" { |
|
44 |
return errors.New("no media file name provided") |
|
45 |
} |
|
46 |
||
47 |
if err := madonInit(true); err != nil { |
|
48 |
return err |
|
49 |
} |
|
50 |
||
201
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
51 |
attachment, err := gClient.UploadMedia(opt.filePath, opt.description, opt.focus) |
0 | 52 |
if err != nil { |
53 |
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
|
54 |
os.Exit(1) |
0 | 55 |
} |
56 |
||
57 |
p, err := getPrinter() |
|
58 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
59 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
60 |
os.Exit(1) |
0 | 61 |
} |
110 | 62 |
return p.printObj(attachment) |
0 | 63 |
} |
64 |
||
65 |
// 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
|
66 |
func uploadFile(filePath string) (int64, error) { |
201
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
67 |
attachment, err := gClient.UploadMedia(filePath, "", "") |
0 | 68 |
if err != nil { |
69 |
return 0, err |
|
70 |
} |
|
71 |
if attachment == nil { |
|
72 |
return 0, nil |
|
73 |
} |
|
74 |
return attachment.ID, nil |
|
75 |
} |