author | Mikael Berthe <mikael@lilotux.net> |
Wed, 21 Mar 2018 23:55:25 +0100 | |
changeset 216 | 3e017cfad3cc |
parent 202 | 1c058699177b |
child 239 | 605a00e9d1ab |
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" |
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
13 |
flag "github.com/spf13/pflag" |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
14 |
|
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
15 |
"github.com/McKael/madon" |
0 | 16 |
) |
17 |
||
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
18 |
var mediaFlags *flag.FlagSet |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
19 |
|
0 | 20 |
var mediaOpts struct { |
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
21 |
mediaID int64 |
201
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
22 |
filePath string |
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
23 |
description string |
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
24 |
focus string |
0 | 25 |
} |
26 |
||
27 |
// mediaCmd represents the media command |
|
28 |
var mediaCmd = &cobra.Command{ |
|
29 |
Use: "media --file FILENAME", |
|
30 |
Aliases: []string{"upload"}, |
|
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
31 |
Short: "Upload or update a media attachment", |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
32 |
Long: `Upload or update a media attachment |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
33 |
|
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
34 |
This command can be used to upload media that will be attached to |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
35 |
a status later. |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
36 |
|
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
37 |
A media description or focal point (focus) can be updated |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
38 |
as long as it is not yet attached to a status, with the '--update MEDIA_ID' |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
39 |
option.`, |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
40 |
Example: ` madonctl upload --file FILENAME |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
41 |
madonctl media --file FILENAME --description "My screenshot" |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
42 |
madonctl media --update 3217821 --focus "0.5,-0.7" |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
43 |
madonctl media --update 2468123 --description "Winter Snow"`, |
0 | 44 |
RunE: mediaRunE, |
45 |
} |
|
46 |
||
47 |
func init() { |
|
48 |
RootCmd.AddCommand(mediaCmd) |
|
49 |
||
50 |
mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file") |
|
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
51 |
mediaCmd.Flags().Int64Var(&mediaOpts.mediaID, "update", 0, "Media to update (ID)") |
201
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
52 |
|
5cb568653fc7
media: add --description and --focus flags for attachments
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
53 |
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
|
54 |
mediaCmd.Flags().StringVar(&mediaOpts.focus, "focus", "", "Focal point") |
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
55 |
|
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
56 |
// This will be used to check if the options were explicitly set or not |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
57 |
mediaFlags = mediaCmd.Flags() |
0 | 58 |
} |
59 |
||
60 |
func mediaRunE(cmd *cobra.Command, args []string) error { |
|
61 |
opt := mediaOpts |
|
62 |
||
63 |
if opt.filePath == "" { |
|
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
64 |
if opt.mediaID < 1 { |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
65 |
return errors.New("no media file name provided") |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
66 |
} |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
67 |
} else if opt.mediaID > 0 { |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
68 |
return errors.New("cannot use both --file and --update") |
0 | 69 |
} |
70 |
||
71 |
if err := madonInit(true); err != nil { |
|
72 |
return err |
|
73 |
} |
|
74 |
||
202
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
75 |
var attachment *madon.Attachment |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
76 |
var err error |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
77 |
|
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
78 |
if opt.filePath != "" { |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
79 |
attachment, err = gClient.UploadMedia(opt.filePath, opt.description, opt.focus) |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
80 |
} else { |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
81 |
// Update |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
82 |
var desc, foc *string |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
83 |
if mediaFlags.Lookup("description").Changed { |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
84 |
desc = &opt.description |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
85 |
} |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
86 |
if mediaFlags.Lookup("focus").Changed { |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
87 |
foc = &opt.focus |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
88 |
} |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
89 |
attachment, err = gClient.UpdateMedia(opt.mediaID, desc, foc) |
1c058699177b
Add madonctl media --update option
Mikael Berthe <mikael@lilotux.net>
parents:
201
diff
changeset
|
90 |
} |
0 | 91 |
if err != nil { |
92 |
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
|
93 |
os.Exit(1) |
0 | 94 |
} |
95 |
||
96 |
p, err := getPrinter() |
|
97 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
98 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
99 |
os.Exit(1) |
0 | 100 |
} |
110 | 101 |
return p.printObj(attachment) |
0 | 102 |
} |
103 |
||
104 |
// 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
|
105 |
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
|
106 |
attachment, err := gClient.UploadMedia(filePath, "", "") |
0 | 107 |
if err != nil { |
108 |
return 0, err |
|
109 |
} |
|
110 |
if attachment == nil { |
|
111 |
return 0, nil |
|
112 |
} |
|
113 |
return attachment.ID, nil |
|
114 |
} |