Compare commits

..

7 commits

Author SHA1 Message Date
4472808f20
renamed ReadFile to ReadTextFile 2024-08-15 15:33:11 +01:00
df38f0f62f
use local attachmentIDs variable 2024-08-15 15:31:52 +01:00
348d7ee00d
checkpoint(untested): should now be able to attach media to statuses from file 2024-08-15 15:31:52 +01:00
70d543132b
checkpoint: updated media attachment flags
Updated the flags used for the media attachment stuff.
2024-08-15 15:31:51 +01:00
27715e3cae
checkpoint: can now create statuses with media attachments 2024-08-15 15:31:51 +01:00
64ca911226
checkpoint: create and edit media attachments 2024-08-15 15:31:51 +01:00
4e76d20a7a
fix: strip trailing empty line when reading files
Updated the ReadFile function in 'utilities' by using the Scanner from
the bufio package to read lines from a text file. The trailing empty
line is stripped after scanning and before returning back to the caller.
2024-08-15 15:24:25 +01:00
2 changed files with 24 additions and 6 deletions

View file

@ -137,7 +137,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
case c.content != "":
content = c.content
case c.fromFile != "":
content, err = utilities.ReadFile(c.fromFile)
content, err = utilities.ReadTextFile(c.fromFile)
if err != nil {
return fmt.Errorf("unable to get the status contents from %q: %w", c.fromFile, err)
}

View file

@ -1,6 +1,7 @@
package utilities
import (
"bufio"
"errors"
"fmt"
"os"
@ -14,16 +15,33 @@ func ReadContents(text string) (string, error) {
return text, nil
}
return ReadFile(strings.TrimPrefix(text, filePrefix))
return ReadTextFile(strings.TrimPrefix(text, filePrefix))
}
func ReadFile(path string) (string, error) {
data, err := os.ReadFile(path)
func ReadTextFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("unable to read the data from the file: %w", err)
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())
}
return string(data), nil
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) {