Skip to content

Commit

Permalink
respond to PR comments (#10)
Browse files Browse the repository at this point in the history
defers file Close after error checks
removes indent level by using guard clause and early return
  • Loading branch information
scotthelm authored Oct 16, 2017
1 parent 4f61369 commit 8912f70
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ func add(todos []todo, addFlag string) []todo {
}

func complete(todos []todo, completeFlag int) []todo {
if completeFlag > -1 {
for i := range todos {
if i == completeFlag {
todos[i].Completed = !todos[i].Completed
todos[i].CompletedAt = time.Now()
}
if completeFlag < 0 || completeFlag > len(todos)-1 {
return todos
}
for i := range todos {
if i == completeFlag {
todos[i].Completed = !todos[i].Completed
todos[i].CompletedAt = time.Now()
}
}
return todos
Expand Down Expand Up @@ -156,11 +157,11 @@ func write(todos []todo, path string) error {
func read(app todoApp) []todo {
todos := make([]todo, 0)
f, err := os.OpenFile(app.DataFilePath, os.O_CREATE|os.O_RDONLY, 0644)
defer f.Close()
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("unable to read todo file: %v", err))
os.Exit(1)
}
defer f.Close()
r := bufio.NewScanner(f)
for r.Scan() {
var t todo
Expand Down

0 comments on commit 8912f70

Please sign in to comment.