Compare commits
7 commits
a31bbe369c
...
4472808f20
Author | SHA1 | Date | |
---|---|---|---|
4472808f20 | |||
df38f0f62f | |||
348d7ee00d | |||
70d543132b | |||
27715e3cae | |||
64ca911226 | |||
4e76d20a7a |
7 changed files with 408 additions and 55 deletions
|
@ -1,8 +1,14 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
||||||
)
|
)
|
||||||
|
@ -31,12 +37,123 @@ func (g *Client) GetMediaAttachment(mediaAttachmentID string) (model.Attachment,
|
||||||
return attachment, nil
|
return attachment, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//type CreateMediaAttachmentForm struct {
|
func (g *Client) CreateMediaAttachment(path, description, focus string) (model.Attachment, error) {
|
||||||
// Description string
|
file, err := os.Open(path)
|
||||||
// Focus string
|
if err != nil {
|
||||||
// Filepath string
|
return model.Attachment{}, fmt.Errorf("unable to open the file: %w", err)
|
||||||
//}
|
}
|
||||||
//
|
defer file.Close()
|
||||||
//func (g *Client) CreateMediaAttachment(form CreateMediaAttachmentForm) (model.Attachment, error) {
|
|
||||||
// return model.Attachment{}, nil
|
// create the request body using a writer from the multipart package
|
||||||
//}
|
requestBody := bytes.Buffer{}
|
||||||
|
requestBodyWriter := multipart.NewWriter(&requestBody)
|
||||||
|
|
||||||
|
filename := filepath.Base(path)
|
||||||
|
|
||||||
|
part, err := requestBodyWriter.CreateFormFile("file", filename)
|
||||||
|
if err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf("unable to create the new part: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(part, file); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf("unable to copy the file contents to the form: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add the description
|
||||||
|
if description != "" {
|
||||||
|
descriptionFormFieldWriter, err := requestBodyWriter.CreateFormField("description")
|
||||||
|
if err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to create the writer for the 'description' form field: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.WriteString(descriptionFormFieldWriter, description); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to write the description to the form: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add the focus values
|
||||||
|
if focus != "" {
|
||||||
|
focusFormFieldWriter, err := requestBodyWriter.CreateFormField("focus")
|
||||||
|
if err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to create the writer for the 'focus' form field: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.WriteString(focusFormFieldWriter, focus); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to write the focus values to the form: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := requestBodyWriter.Close(); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf("unable to close the writer: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
url := g.Authentication.Instance + baseMediaPath
|
||||||
|
|
||||||
|
var attachment model.Attachment
|
||||||
|
|
||||||
|
params := requestParameters{
|
||||||
|
httpMethod: http.MethodPost,
|
||||||
|
url: url,
|
||||||
|
requestBody: &requestBody,
|
||||||
|
contentType: requestBodyWriter.FormDataContentType(),
|
||||||
|
output: &attachment,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := g.sendRequest(params); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"received an error after sending the request to create the media attachment: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return attachment, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Client) UpdateMediaAttachment(mediaAttachmentID, description, focus string) (model.Attachment, error) {
|
||||||
|
form := struct {
|
||||||
|
Description string `json:"description"`
|
||||||
|
Focus string `json:"focus"`
|
||||||
|
}{
|
||||||
|
Description: description,
|
||||||
|
Focus: focus,
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := json.Marshal(form)
|
||||||
|
if err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf("unable to marshal the form: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
requestBody := bytes.NewBuffer(data)
|
||||||
|
url := g.Authentication.Instance + baseMediaPath + "/" + mediaAttachmentID
|
||||||
|
|
||||||
|
var updatedMediaAttachment model.Attachment
|
||||||
|
|
||||||
|
params := requestParameters{
|
||||||
|
httpMethod: http.MethodPut,
|
||||||
|
url: url,
|
||||||
|
requestBody: requestBody,
|
||||||
|
contentType: applicationJSON,
|
||||||
|
output: &updatedMediaAttachment,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := g.sendRequest(params); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"received an error after sending the request to update the media attachment: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedMediaAttachment, nil
|
||||||
|
}
|
||||||
|
|
|
@ -38,18 +38,19 @@ func (g *Client) GetStatus(statusID string) (model.Status, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateStatusForm struct {
|
type CreateStatusForm struct {
|
||||||
Content string `json:"status"`
|
Content string `json:"status"`
|
||||||
InReplyTo string `json:"in_reply_to_id"`
|
InReplyTo string `json:"in_reply_to_id"`
|
||||||
Language string `json:"language"`
|
Language string `json:"language"`
|
||||||
SpoilerText string `json:"spoiler_text"`
|
SpoilerText string `json:"spoiler_text"`
|
||||||
Boostable bool `json:"boostable"`
|
Boostable bool `json:"boostable"`
|
||||||
Federated bool `json:"federated"`
|
Federated bool `json:"federated"`
|
||||||
Likeable bool `json:"likeable"`
|
Likeable bool `json:"likeable"`
|
||||||
Replyable bool `json:"replyable"`
|
Replyable bool `json:"replyable"`
|
||||||
Sensitive bool `json:"sensitive"`
|
Sensitive bool `json:"sensitive"`
|
||||||
Poll *CreateStatusPollForm `json:"poll,omitempty"`
|
Poll *CreateStatusPollForm `json:"poll,omitempty"`
|
||||||
ContentType model.StatusContentType `json:"content_type"`
|
ContentType model.StatusContentType `json:"content_type"`
|
||||||
Visibility model.StatusVisibility `json:"visibility"`
|
Visibility model.StatusVisibility `json:"visibility"`
|
||||||
|
AttachmentIDs []string `json:"media_ids,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateStatusPollForm struct {
|
type CreateStatusPollForm struct {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package executor
|
package executor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
|
@ -19,8 +20,9 @@ func (c *CreateExecutor) Execute() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
resourceList: c.createList,
|
resourceList: c.createList,
|
||||||
resourceStatus: c.createStatus,
|
resourceStatus: c.createStatus,
|
||||||
|
resourceMediaAttachment: c.createMediaAttachment,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[c.resourceType]
|
doFunc, ok := funcMap[c.resourceType]
|
||||||
|
@ -66,21 +68,95 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
sensitive bool
|
sensitive bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
attachmentIDs := []string(c.attachmentIDs)
|
||||||
|
|
||||||
|
if !c.mediaFiles.Empty() {
|
||||||
|
descriptionsExists := false
|
||||||
|
focusValuesExists := false
|
||||||
|
expectedLength := len(c.mediaFiles)
|
||||||
|
mediaDescriptions := make([]string, expectedLength)
|
||||||
|
|
||||||
|
if !c.mediaDescriptions.Empty() {
|
||||||
|
descriptionsExists = true
|
||||||
|
|
||||||
|
if !c.mediaDescriptions.ExpectedLength(expectedLength) {
|
||||||
|
return errors.New("the number of media descriptions does not match the number of media files")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.mediaFocusValues.Empty() {
|
||||||
|
focusValuesExists = true
|
||||||
|
|
||||||
|
if !c.mediaFocusValues.ExpectedLength(expectedLength) {
|
||||||
|
return errors.New("the number of media focus values does not match the number of media files")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if descriptionsExists {
|
||||||
|
for ind := 0; ind < expectedLength; ind++ {
|
||||||
|
content, err := utilities.ReadContents(c.mediaDescriptions[ind])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to read the contents from %s: %w", c.mediaDescriptions[ind], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mediaDescriptions[ind] = content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for ind := 0; ind < expectedLength; ind++ {
|
||||||
|
var (
|
||||||
|
mediaFile string
|
||||||
|
description string
|
||||||
|
focus string
|
||||||
|
)
|
||||||
|
|
||||||
|
mediaFile = c.mediaFiles[ind]
|
||||||
|
|
||||||
|
if descriptionsExists {
|
||||||
|
description = mediaDescriptions[ind]
|
||||||
|
}
|
||||||
|
|
||||||
|
if focusValuesExists {
|
||||||
|
focus = c.mediaFocusValues[ind]
|
||||||
|
}
|
||||||
|
|
||||||
|
attachment, err := gtsClient.CreateMediaAttachment(
|
||||||
|
mediaFile,
|
||||||
|
description,
|
||||||
|
focus,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create the media attachment for %s: %w", mediaFile, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
attachmentIDs = append(attachmentIDs, attachment.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case c.content != "":
|
case c.content != "":
|
||||||
content = c.content
|
content = c.content
|
||||||
case c.fromFile != "":
|
case c.fromFile != "":
|
||||||
content, err = utilities.ReadFile(c.fromFile)
|
content, err = utilities.ReadTextFile(c.fromFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to get the status contents from %q: %w", c.fromFile, err)
|
return fmt.Errorf("unable to get the status contents from %q: %w", c.fromFile, err)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return EmptyContentError{
|
if len(attachmentIDs) == 0 {
|
||||||
ResourceType: resourceStatus,
|
// TODO: revisit this error type
|
||||||
Hint: "please use --" + flagContent + " or --" + flagFromFile,
|
return EmptyContentError{
|
||||||
|
ResourceType: resourceStatus,
|
||||||
|
Hint: "please use --" + flagContent + " or --" + flagFromFile,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
numAttachmentIDs := len(attachmentIDs)
|
||||||
|
|
||||||
|
if c.addPoll && numAttachmentIDs > 0 {
|
||||||
|
return fmt.Errorf("attaching media to a poll is not allowed")
|
||||||
|
}
|
||||||
|
|
||||||
preferences, err := gtsClient.GetUserPreferences()
|
preferences, err := gtsClient.GetUserPreferences()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("WARNING: Unable to get your posting preferences: %w", err)
|
fmt.Println("WARNING: Unable to get your posting preferences: %w", err)
|
||||||
|
@ -115,18 +191,23 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
form := client.CreateStatusForm{
|
form := client.CreateStatusForm{
|
||||||
Content: content,
|
Content: content,
|
||||||
ContentType: parsedContentType,
|
ContentType: parsedContentType,
|
||||||
Language: language,
|
Language: language,
|
||||||
SpoilerText: c.spoilerText,
|
SpoilerText: c.spoilerText,
|
||||||
Boostable: c.boostable,
|
Boostable: c.boostable,
|
||||||
Federated: c.federated,
|
Federated: c.federated,
|
||||||
InReplyTo: c.inReplyTo,
|
InReplyTo: c.inReplyTo,
|
||||||
Likeable: c.likeable,
|
Likeable: c.likeable,
|
||||||
Replyable: c.replyable,
|
Replyable: c.replyable,
|
||||||
Sensitive: sensitive,
|
Sensitive: sensitive,
|
||||||
Visibility: parsedVisibility,
|
Visibility: parsedVisibility,
|
||||||
Poll: nil,
|
Poll: nil,
|
||||||
|
AttachmentIDs: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
if numAttachmentIDs > 0 {
|
||||||
|
form.AttachmentIDs = attachmentIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.addPoll {
|
if c.addPoll {
|
||||||
|
@ -153,3 +234,40 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CreateExecutor) createMediaAttachment(gtsClient *client.Client) error {
|
||||||
|
expectedNumValues := 1
|
||||||
|
if !c.mediaFiles.ExpectedLength(expectedNumValues) {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"received an unexpected number of media files: want %d",
|
||||||
|
expectedNumValues,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.mediaDescriptions.ExpectedLength(expectedNumValues) {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"received an unexpected number of media descriptions: want %d",
|
||||||
|
expectedNumValues,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.mediaFocusValues.ExpectedLength(expectedNumValues) {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"received an unexpected number of media focus values: want %d",
|
||||||
|
expectedNumValues,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
attachment, err := gtsClient.CreateMediaAttachment(
|
||||||
|
c.mediaFiles[0],
|
||||||
|
c.mediaDescriptions[0],
|
||||||
|
c.mediaFocusValues[0],
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create the media attachment: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.printer.PrintSuccess("Successfully created the media attachment with ID: " + attachment.ID)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -13,7 +13,8 @@ func (e *EditExecutor) Execute() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
resourceList: e.editList,
|
resourceList: e.editList,
|
||||||
|
resourceMediaAttachment: e.editMediaAttachment,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[e.resourceType]
|
doFunc, ok := funcMap[e.resourceType]
|
||||||
|
@ -57,8 +58,55 @@ func (e *EditExecutor) editList(gtsClient *client.Client) error {
|
||||||
return fmt.Errorf("unable to update the list: %w", err)
|
return fmt.Errorf("unable to update the list: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.printer.PrintSuccess("Successfully updated the list.")
|
e.printer.PrintSuccess("Successfully edited the list.")
|
||||||
e.printer.PrintList(updatedList)
|
e.printer.PrintList(updatedList)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *EditExecutor) editMediaAttachment(gtsClient *client.Client) error {
|
||||||
|
expectedNumValues := 1
|
||||||
|
if !e.attachmentIDs.ExpectedLength(expectedNumValues) {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"received an unexpected number of media attachment IDs: want %d",
|
||||||
|
expectedNumValues,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !e.mediaDescriptions.ExpectedLength(expectedNumValues) {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"received an unexpected number of media descriptions: want %d",
|
||||||
|
expectedNumValues,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !e.mediaFocusValues.ExpectedLength(expectedNumValues) {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"received an unexpected number of media focus values: want %d",
|
||||||
|
expectedNumValues,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
attachment, err := gtsClient.GetMediaAttachment(e.attachmentIDs[0])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to get the media attachment: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
description := e.mediaDescriptions[0]
|
||||||
|
if description == "" {
|
||||||
|
description = attachment.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
focus := e.mediaFocusValues[0]
|
||||||
|
if focus == "" {
|
||||||
|
focus = fmt.Sprintf("%f,%f", attachment.Meta.Focus.X, attachment.Meta.Focus.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = gtsClient.UpdateMediaAttachment(e.attachmentIDs[0], description, focus); err != nil {
|
||||||
|
return fmt.Errorf("unable to update the media attachment: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e.printer.PrintSuccess("Successfully edited the media attachment.")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -121,6 +121,7 @@ type CreateExecutor struct {
|
||||||
printer *printer.Printer
|
printer *printer.Printer
|
||||||
config *config.Config
|
config *config.Config
|
||||||
addPoll bool
|
addPoll bool
|
||||||
|
attachmentIDs internalFlag.StringSliceValue
|
||||||
content string
|
content string
|
||||||
contentType string
|
contentType string
|
||||||
federated bool
|
federated bool
|
||||||
|
@ -132,6 +133,9 @@ type CreateExecutor struct {
|
||||||
language string
|
language string
|
||||||
listRepliesPolicy string
|
listRepliesPolicy string
|
||||||
listTitle string
|
listTitle string
|
||||||
|
mediaDescriptions internalFlag.StringSliceValue
|
||||||
|
mediaFocusValues internalFlag.StringSliceValue
|
||||||
|
mediaFiles internalFlag.StringSliceValue
|
||||||
pollAllowsMultipleChoices bool
|
pollAllowsMultipleChoices bool
|
||||||
pollExpiresIn internalFlag.TimeDurationValue
|
pollExpiresIn internalFlag.TimeDurationValue
|
||||||
pollHidesVoteCounts bool
|
pollHidesVoteCounts bool
|
||||||
|
@ -147,17 +151,22 @@ func NewCreateExecutor(
|
||||||
config *config.Config,
|
config *config.Config,
|
||||||
) *CreateExecutor {
|
) *CreateExecutor {
|
||||||
exe := CreateExecutor{
|
exe := CreateExecutor{
|
||||||
FlagSet: flag.NewFlagSet("create", flag.ExitOnError),
|
FlagSet: flag.NewFlagSet("create", flag.ExitOnError),
|
||||||
printer: printer,
|
printer: printer,
|
||||||
config: config,
|
config: config,
|
||||||
pollExpiresIn: internalFlag.NewTimeDurationValue(),
|
attachmentIDs: internalFlag.NewStringSliceValue(),
|
||||||
pollOptions: internalFlag.NewStringSliceValue(),
|
mediaDescriptions: internalFlag.NewStringSliceValue(),
|
||||||
sensitive: internalFlag.NewBoolPtrValue(),
|
mediaFocusValues: internalFlag.NewStringSliceValue(),
|
||||||
|
mediaFiles: internalFlag.NewStringSliceValue(),
|
||||||
|
pollExpiresIn: internalFlag.NewTimeDurationValue(),
|
||||||
|
pollOptions: internalFlag.NewStringSliceValue(),
|
||||||
|
sensitive: internalFlag.NewBoolPtrValue(),
|
||||||
}
|
}
|
||||||
|
|
||||||
exe.Usage = usage.ExecutorUsageFunc("create", "Creates a specific resource", exe.FlagSet)
|
exe.Usage = usage.ExecutorUsageFunc("create", "Creates a specific resource", exe.FlagSet)
|
||||||
|
|
||||||
exe.BoolVar(&exe.addPoll, "add-poll", false, "Set to true to add a poll when creating a status")
|
exe.BoolVar(&exe.addPoll, "add-poll", false, "Set to true to add a poll when creating a status")
|
||||||
|
exe.Var(&exe.attachmentIDs, "attachment-id", "The ID of the media attachment")
|
||||||
exe.StringVar(&exe.content, "content", "", "The content of the created resource")
|
exe.StringVar(&exe.content, "content", "", "The content of the created resource")
|
||||||
exe.StringVar(&exe.contentType, "content-type", "plain", "The type that the contents should be parsed from (valid values are plain and markdown)")
|
exe.StringVar(&exe.contentType, "content-type", "plain", "The type that the contents should be parsed from (valid values are plain and markdown)")
|
||||||
exe.BoolVar(&exe.federated, "enable-federation", true, "Set to true to federate the status beyond the local timelines")
|
exe.BoolVar(&exe.federated, "enable-federation", true, "Set to true to federate the status beyond the local timelines")
|
||||||
|
@ -169,6 +178,9 @@ func NewCreateExecutor(
|
||||||
exe.StringVar(&exe.language, "language", "", "The ISO 639 language code for this status")
|
exe.StringVar(&exe.language, "language", "", "The ISO 639 language code for this status")
|
||||||
exe.StringVar(&exe.listRepliesPolicy, "list-replies-policy", "list", "The replies policy of the list")
|
exe.StringVar(&exe.listRepliesPolicy, "list-replies-policy", "list", "The replies policy of the list")
|
||||||
exe.StringVar(&exe.listTitle, "list-title", "", "The title of the list")
|
exe.StringVar(&exe.listTitle, "list-title", "", "The title of the list")
|
||||||
|
exe.Var(&exe.mediaDescriptions, "media-description", "The description of the media attachment that will be used as the alt-text")
|
||||||
|
exe.Var(&exe.mediaFocusValues, "media-focus", "The focus of the media file")
|
||||||
|
exe.Var(&exe.mediaFiles, "media-file", "The path to the media file")
|
||||||
exe.BoolVar(&exe.pollAllowsMultipleChoices, "poll-allows-multiple-choices", false, "Set to true to allow viewers to make multiple choices in the poll")
|
exe.BoolVar(&exe.pollAllowsMultipleChoices, "poll-allows-multiple-choices", false, "Set to true to allow viewers to make multiple choices in the poll")
|
||||||
exe.Var(&exe.pollExpiresIn, "poll-expires-in", "The duration in which the poll is open for")
|
exe.Var(&exe.pollExpiresIn, "poll-expires-in", "The duration in which the poll is open for")
|
||||||
exe.BoolVar(&exe.pollHidesVoteCounts, "poll-hides-vote-counts", false, "Set to true to hide the vote count until the poll is closed")
|
exe.BoolVar(&exe.pollHidesVoteCounts, "poll-hides-vote-counts", false, "Set to true to hide the vote count until the poll is closed")
|
||||||
|
@ -213,9 +225,12 @@ type EditExecutor struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
printer *printer.Printer
|
printer *printer.Printer
|
||||||
config *config.Config
|
config *config.Config
|
||||||
|
attachmentIDs internalFlag.StringSliceValue
|
||||||
listID string
|
listID string
|
||||||
listTitle string
|
listTitle string
|
||||||
listRepliesPolicy string
|
listRepliesPolicy string
|
||||||
|
mediaDescriptions internalFlag.StringSliceValue
|
||||||
|
mediaFocusValues internalFlag.StringSliceValue
|
||||||
resourceType string
|
resourceType string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,16 +239,22 @@ func NewEditExecutor(
|
||||||
config *config.Config,
|
config *config.Config,
|
||||||
) *EditExecutor {
|
) *EditExecutor {
|
||||||
exe := EditExecutor{
|
exe := EditExecutor{
|
||||||
FlagSet: flag.NewFlagSet("edit", flag.ExitOnError),
|
FlagSet: flag.NewFlagSet("edit", flag.ExitOnError),
|
||||||
printer: printer,
|
printer: printer,
|
||||||
config: config,
|
config: config,
|
||||||
|
attachmentIDs: internalFlag.NewStringSliceValue(),
|
||||||
|
mediaDescriptions: internalFlag.NewStringSliceValue(),
|
||||||
|
mediaFocusValues: internalFlag.NewStringSliceValue(),
|
||||||
}
|
}
|
||||||
|
|
||||||
exe.Usage = usage.ExecutorUsageFunc("edit", "Edit a specific resource", exe.FlagSet)
|
exe.Usage = usage.ExecutorUsageFunc("edit", "Edit a specific resource", exe.FlagSet)
|
||||||
|
|
||||||
|
exe.Var(&exe.attachmentIDs, "attachment-id", "The ID of the media attachment")
|
||||||
exe.StringVar(&exe.listID, "list-id", "", "The ID of the list in question")
|
exe.StringVar(&exe.listID, "list-id", "", "The ID of the list in question")
|
||||||
exe.StringVar(&exe.listTitle, "list-title", "", "The title of the list")
|
exe.StringVar(&exe.listTitle, "list-title", "", "The title of the list")
|
||||||
exe.StringVar(&exe.listRepliesPolicy, "list-replies-policy", "", "The replies policy of the list")
|
exe.StringVar(&exe.listRepliesPolicy, "list-replies-policy", "", "The replies policy of the list")
|
||||||
|
exe.Var(&exe.mediaDescriptions, "media-description", "The description of the media attachment that will be used as the alt-text")
|
||||||
|
exe.Var(&exe.mediaFocusValues, "media-focus", "The focus of the media file")
|
||||||
exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)")
|
exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)")
|
||||||
|
|
||||||
return &exe
|
return &exe
|
||||||
|
|
|
@ -1,18 +1,47 @@
|
||||||
package utilities
|
package utilities
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReadFile(path string) (string, error) {
|
const filePrefix string = "file@"
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
func ReadContents(text string) (string, error) {
|
||||||
return "", fmt.Errorf("unable to read the data from the file: %w", err)
|
if !strings.HasPrefix(text, filePrefix) {
|
||||||
|
return text, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(data), nil
|
return ReadTextFile(strings.TrimPrefix(text, filePrefix))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadTextFile(path string) (string, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("unable to open %q: %w", path, err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return "", fmt.Errorf("received an error after scanning the contents from %q: %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if lines[len(lines)-1] == "" {
|
||||||
|
lines = lines[:len(lines)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(lines, "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FileExists(path string) (bool, error) {
|
func FileExists(path string) (bool, error) {
|
||||||
|
|
|
@ -96,6 +96,18 @@
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "The replies policy of the list"
|
"description": "The replies policy of the list"
|
||||||
},
|
},
|
||||||
|
"media-description": {
|
||||||
|
"type": "StringSliceValue",
|
||||||
|
"description": "The description of the media attachment that will be used as the alt-text"
|
||||||
|
},
|
||||||
|
"media-file": {
|
||||||
|
"type": "StringSliceValue",
|
||||||
|
"description": "The path to the media file"
|
||||||
|
},
|
||||||
|
"media-focus": {
|
||||||
|
"type": "StringSliceValue",
|
||||||
|
"description": "The focus of the media file"
|
||||||
|
},
|
||||||
"mute-duration": {
|
"mute-duration": {
|
||||||
"type": "TimeDurationValue",
|
"type": "TimeDurationValue",
|
||||||
"description": "Specify how long the mute should last for. To mute indefinitely, set this to 0s"
|
"description": "Specify how long the mute should last for. To mute indefinitely, set this to 0s"
|
||||||
|
@ -234,6 +246,7 @@
|
||||||
"additionalFields": [],
|
"additionalFields": [],
|
||||||
"flags": [
|
"flags": [
|
||||||
{ "flag": "add-poll", "default": "false" },
|
{ "flag": "add-poll", "default": "false" },
|
||||||
|
{ "flag": "attachment-id", "fieldName": "attachmentIDs" },
|
||||||
{ "flag": "content", "default": "" },
|
{ "flag": "content", "default": "" },
|
||||||
{ "flag": "content-type", "default": "plain" },
|
{ "flag": "content-type", "default": "plain" },
|
||||||
{ "flag": "enable-federation", "fieldName": "federated", "default": "true" },
|
{ "flag": "enable-federation", "fieldName": "federated", "default": "true" },
|
||||||
|
@ -245,6 +258,9 @@
|
||||||
{ "flag": "language", "default": "" },
|
{ "flag": "language", "default": "" },
|
||||||
{ "flag": "list-replies-policy", "default": "list" },
|
{ "flag": "list-replies-policy", "default": "list" },
|
||||||
{ "flag": "list-title", "default": "" },
|
{ "flag": "list-title", "default": "" },
|
||||||
|
{ "flag": "media-description", "fieldName": "mediaDescriptions" },
|
||||||
|
{ "flag": "media-focus", "fieldName": "mediaFocusValues" },
|
||||||
|
{ "flag": "media-file", "fieldName": "mediaFiles" },
|
||||||
{ "flag": "poll-allows-multiple-choices", "default": "false" },
|
{ "flag": "poll-allows-multiple-choices", "default": "false" },
|
||||||
{ "flag": "poll-expires-in" },
|
{ "flag": "poll-expires-in" },
|
||||||
{ "flag": "poll-hides-vote-counts", "default": "false" },
|
{ "flag": "poll-hides-vote-counts", "default": "false" },
|
||||||
|
@ -271,9 +287,12 @@
|
||||||
"edit": {
|
"edit": {
|
||||||
"additionalFields": [],
|
"additionalFields": [],
|
||||||
"flags": [
|
"flags": [
|
||||||
|
{ "flag": "attachment-id", "fieldName": "attachmentIDs" },
|
||||||
{ "flag": "list-id", "fieldName": "listID", "default": ""},
|
{ "flag": "list-id", "fieldName": "listID", "default": ""},
|
||||||
{ "flag": "list-title", "default": "" },
|
{ "flag": "list-title", "default": "" },
|
||||||
{ "flag": "list-replies-policy", "default": "" },
|
{ "flag": "list-replies-policy", "default": "" },
|
||||||
|
{ "flag": "media-description", "fieldName": "mediaDescriptions" },
|
||||||
|
{ "flag": "media-focus", "fieldName": "mediaFocusValues" },
|
||||||
{ "flag": "type", "fieldName": "resourceType", "default": "" }
|
{ "flag": "type", "fieldName": "resourceType", "default": "" }
|
||||||
],
|
],
|
||||||
"summary": "Edit a specific resource",
|
"summary": "Edit a specific resource",
|
||||||
|
|
Loading…
Reference in a new issue