cmd/media.go
author rjp <zimpenfish@gmail.com>
Mon, 23 Jan 2023 16:39:02 +0000
changeset 267 5b91a65ba95a
parent 239 605a00e9d1ab
child 268 4dd196a4ee7c
permissions -rw-r--r--
Update to handle non-int64 IDs Pleroma/Akkoma and GotoSocial use opaque IDs rather than `int64`s like Mastodon which means that `madon` can't talk to either of those. This commit updates everything that can be an ID to `madon.ActivityID` which is an alias for `string` - can't create a specific type for it since there's more than a few places where they're concatenated directly to strings for URLs, etc. Which means it could just as easily be a direct `string` type itself but I find that having distinct types can often make the code more readable and understandable. One extra bit is that `statusOpts` has grown a `_hasReplyTo` boolean to indicate whether the `--in-reply-to` flag was given or not because we can't distinguish because "empty because default" or "empty because given and empty". Another way around this would be to set the default to some theoretically impossible or unlikely string but you never know when someone might spin up an instance where, e.g., admin posts have negative integer IDs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
185
564d92b54b00 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 110
diff changeset
     1
// Copyright © 2017-2018 Mikael Berthe <mikael@lilotux.net>
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package cmd
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
239
605a00e9d1ab Switch to Go modules (and bump Go version requirement)
Mikael Berthe <mikael@lilotux.net>
parents: 202
diff changeset
    15
	"github.com/McKael/madon/v2"
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
var mediaOpts struct {
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    21
	mediaID     madon.ActivityID
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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
// mediaCmd represents the media command
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
var mediaCmd = &cobra.Command{
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	Use:     "media --file FILENAME",
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	RunE: mediaRunE,
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
func init() {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	RootCmd.AddCommand(mediaCmd)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	mediaCmd.Flags().StringVar(&mediaOpts.filePath, "file", "", "Path of the media file")
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    51
	mediaCmd.Flags().StringVar(&mediaOpts.mediaID, "update", "", "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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
func mediaRunE(cmd *cobra.Command, args []string) error {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
	opt := mediaOpts
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	if opt.filePath == "" {
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    64
		if opt.mediaID == "" {
202
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
		}
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
    67
	} else if opt.mediaID != "" {
202
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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
	if err := madonInit(true); err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
		return err
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
	if err != nil {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	p, err := getPrinter()
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
	}
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 81
diff changeset
   101
	return p.printObj(attachment)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
// uploadFile uploads a media file and returns the attachment ID
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
   105
func uploadFile(filePath string) (madon.ActivityID, 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
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
	if err != nil {
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
   108
		return "", err
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
	if attachment == nil {
267
5b91a65ba95a Update to handle non-int64 IDs
rjp <zimpenfish@gmail.com>
parents: 239
diff changeset
   111
		return "", nil
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
	return attachment.ID, nil
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
}