Skip to content

Commit

Permalink
Standardize file read
Browse files Browse the repository at this point in the history
Changed os.Open() to ioutil.ReadFile()

Signed-off-by: Lucas Zampieri <lzampier@redhat.com>
  • Loading branch information
zampierilucas committed Oct 4, 2021
1 parent fd3d2ce commit 9977dae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
20 changes: 6 additions & 14 deletions cmd/edit_common.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"bufio"
"fmt"
"os"
"io/ioutil"
"strings"

"github.com/zaquestion/lab/internal/git"
Expand Down Expand Up @@ -69,23 +68,16 @@ func editGetTitleDescription(title string, body string, msgs []string, nFlag int
// remaining is the description.
func editGetTitleDescFromFile(filename string) (string, string, error) {
var title, body string
var lines []string

file, err := os.Open(filename)
content, err := ioutil.ReadFile(filename)
if err != nil {
return "", "", nil
}
defer file.Close()
lines = strings.Split(string(content), "\n")

fileScan := bufio.NewScanner(file)
fileScan.Split(bufio.ScanLines)

// The first line in the file is the title.
fileScan.Scan()
title = fileScan.Text()

for fileScan.Scan() {
body = body + fileScan.Text() + "\n"
}
title = lines[0]
body = strings.Join(lines[1:], "\n")

return title, body, nil
}
14 changes: 5 additions & 9 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,15 @@ func LoadGitLabTmpl(tmplName string) string {
}

tmplFile := filepath.Join(wd, ".gitlab", tmplName)
f, err := os.Open(tmplFile)
if os.IsNotExist(err) {
return ""
} else if err != nil {
log.Fatal(err)
}

tmpl, err := ioutil.ReadAll(f)
content, err := ioutil.ReadFile(tmplFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return ""
}
log.Fatal(err)
}

return strings.TrimSpace(string(tmpl))
return strings.TrimSpace(string(content))
}

var localProjects map[string]*gitlab.Project = make(map[string]*gitlab.Project)
Expand Down

0 comments on commit 9977dae

Please sign in to comment.