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 != "":
content = c.content
case c.fromFile != "":
content, err = utilities.ReadTextFile(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)
}

View file

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