feat: post a status from a file #17

Manually merged
dananglin merged 1 commit from post-status-from-file into main 2024-05-31 04:20:40 +01:00
5 changed files with 52 additions and 8 deletions

View file

@ -133,7 +133,10 @@ func (a *AddExecutor) addNoteToAccount(gtsClient *client.Client) error {
} }
if a.content == "" { if a.content == "" {
return EmptyContentError{} return EmptyContentError{
ResourceType: resourceNote,
Hint: "please use --" + flagContent,
}
} }
if err := gtsClient.SetPrivateNote(accountID, a.content); err != nil { if err := gtsClient.SetPrivateNote(accountID, a.content); err != nil {

View file

@ -10,6 +10,7 @@ const (
flagEnableReplies = "enable-replies" flagEnableReplies = "enable-replies"
flagEnableReposts = "enable-reposts" flagEnableReposts = "enable-reposts"
flagFrom = "from" flagFrom = "from"
flagFromFile = "from-file"
flagInstance = "instance" flagInstance = "instance"
flagLanguage = "language" flagLanguage = "language"
flagLimit = "limit" flagLimit = "limit"

View file

@ -6,6 +6,7 @@ import (
"codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model" "codeflow.dananglin.me.uk/apollo/enbas/internal/model"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
) )
type CreateExecutor struct { type CreateExecutor struct {
@ -19,6 +20,7 @@ type CreateExecutor struct {
sensitive bool sensitive bool
content string content string
contentType string contentType string
fromFile string
language string language string
spoilerText string spoilerText string
resourceType string resourceType string
@ -41,6 +43,7 @@ func NewCreateExecutor(tlf TopLevelFlags, name, summary string) *CreateExecutor
createExe.BoolVar(&createExe.sensitive, flagSensitive, false, "specify if the status should be marked as sensitive") createExe.BoolVar(&createExe.sensitive, flagSensitive, false, "specify if the status should be marked as sensitive")
createExe.StringVar(&createExe.content, flagContent, "", "the content of the status to create") createExe.StringVar(&createExe.content, flagContent, "", "the content of the status to create")
createExe.StringVar(&createExe.contentType, flagContentType, "plain", "the type that the contents should be parsed from (valid values are plain and markdown)") createExe.StringVar(&createExe.contentType, flagContentType, "plain", "the type that the contents should be parsed from (valid values are plain and markdown)")
createExe.StringVar(&createExe.fromFile, flagFromFile, "", "the file path where to read the contents from")
createExe.StringVar(&createExe.language, flagLanguage, "", "the ISO 639 language code for this status") createExe.StringVar(&createExe.language, flagLanguage, "", "the ISO 639 language code for this status")
createExe.StringVar(&createExe.spoilerText, flagSpoilerText, "", "the text to display as the status' warning or subject") createExe.StringVar(&createExe.spoilerText, flagSpoilerText, "", "the text to display as the status' warning or subject")
createExe.StringVar(&createExe.visibility, flagVisibility, "", "the visibility of the posted status") createExe.StringVar(&createExe.visibility, flagVisibility, "", "the visibility of the posted status")
@ -103,15 +106,28 @@ func (c *CreateExecutor) createList(gtsClient *client.Client) error {
} }
func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
if c.content == "" {
return FlagNotSetError{flagText: flagContent}
}
var ( var (
err error
content string
language string language string
visibility string visibility string
) )
switch {
case c.content != "":
content = c.content
case c.fromFile != "":
content, err = utilities.ReadFile(c.fromFile)
if err != nil {
return fmt.Errorf("unable to get the status contents from %q; %w", c.fromFile, err)
}
default:
return EmptyContentError{
ResourceType: resourceStatus,
Hint: "please use --" + flagContent + " or --" + flagFromFile,
}
}
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)
@ -140,7 +156,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
} }
form := client.CreateStatusForm{ form := client.CreateStatusForm{
Content: c.content, Content: content,
ContentType: parsedContentType, ContentType: parsedContentType,
Language: language, Language: language,
SpoilerText: c.spoilerText, SpoilerText: c.spoilerText,

View file

@ -48,10 +48,19 @@ func (e UnsupportedRemoveOperationError) Error() string {
return "removing '" + e.ResourceType + "' from '" + e.RemoveFromResourceType + "' is not supported" return "removing '" + e.ResourceType + "' from '" + e.RemoveFromResourceType + "' is not supported"
} }
type EmptyContentError struct{} type EmptyContentError struct{
ResourceType string
Hint string
}
func (e EmptyContentError) Error() string { func (e EmptyContentError) Error() string {
return "content should not be empty" message := "the content of this " + e.ResourceType + " should not be empty"
if e.Hint != "" {
message += ", " + e.Hint
}
return message
} }
type InvalidStatusVisibilityError struct { type InvalidStatusVisibilityError struct {

View file

@ -0,0 +1,15 @@
package utilities
import (
"fmt"
"os"
)
func ReadFile(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("unable to read the data from the file; %w", err)
}
return string(data), nil
}