Skip to content

addresses maintainability #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ func printTodo(i int, t todo) string {
func write(todos []todo, path string) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("There was an error opening the file: %v\n", err))
os.Exit(1)
reportErrorAndExit("There was an error opening the file: %v\n", err)
}
f.Truncate(0)
f.Seek(0, 0)
Expand All @@ -145,8 +144,7 @@ func write(todos []todo, path string) error {

b, err := json.Marshal(&t)
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("There was an error marshaling the todo: %v\n", err))
os.Exit(1)
reportErrorAndExit("There was an error marshaling the todo: %v\n", err)
}
_, err = w.WriteString(fmt.Sprintf("%v\n", string(b)))
w.Flush()
Expand All @@ -158,8 +156,7 @@ func read(app todoApp) []todo {
todos := make([]todo, 0)
f, err := os.OpenFile(app.DataFilePath, os.O_CREATE|os.O_RDONLY, 0644)
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("unable to read todo file: %v", err))
os.Exit(1)
reportErrorAndExit("unable to read todo file: %v", err)
}
defer f.Close()
r := bufio.NewScanner(f)
Expand All @@ -177,3 +174,8 @@ func backup(todos []todo, path string) error {
}
return nil
}

func reportErrorAndExit(message string, err error) {
os.Stderr.WriteString(fmt.Sprintf(message, err))
os.Exit(1)
}