Compare commits

..

5 commits

2 changed files with 6 additions and 24 deletions

View file

@ -137,7 +137,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
case c.content != "": case c.content != "":
content = c.content content = c.content
case c.fromFile != "": case c.fromFile != "":
content, err = utilities.ReadTextFile(c.fromFile) content, err = utilities.ReadFile(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)
} }

View file

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