Skip to content
Draft
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package main
import (
"encoding/csv"
"fmt"
"log"
"os"
)

func CreateStore() {
file, err := os.Create("store.csv")
if err != nil {
fmt.Println("Error creating store file")
log.Fatal("Error creating store file: %v", err)
}
file.Close()
fmt.Println("Store Created")
Expand All @@ -18,7 +19,7 @@ func CreateStore() {
func DeleteStore() {
file, err := os.OpenFile("store.csv", os.O_TRUNC, 0666)
if err != nil {
fmt.Println("Error deleting store file")
log.Fatalf("Error deleting store file: %v", err)
}
defer file.Close()
file.Truncate(0)
Expand All @@ -27,7 +28,7 @@ func DeleteStore() {
func WriteToFile(data [][]string) {
file, err := os.OpenFile("store.csv", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("Error opening store file")
log.Fatalf("Error opening store file: %v", err)
}
defer file.Close()
file.Truncate(0)
Expand Down
54 changes: 29 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"log"
)

func main() {
Expand Down Expand Up @@ -67,15 +68,13 @@ func main() {
CreateStore()
store, err = GetStore()
} else {
fmt.Println(err)
return
log.Fatal(err)
}
}

cmd, err := StringToCommand(args[0])
if err != nil {
fmt.Println(err)
return
log.Fatal(err)
}
switch cmd {
case Help:
Expand All @@ -86,8 +85,7 @@ func main() {
for _, arg := range args[1:] {
cmd, err := StringToCommand(arg)
if err != nil {
fmt.Println(err)
return
log.Fatal(err)
}
cmds = append(cmds, cmd)
}
Expand All @@ -97,56 +95,62 @@ func main() {
fmt.Println(GetUsage(cmd))
}
case List:
if name != "" {
switch {
case name != "":
item, err := store.GetItemByName(name)
if err != nil {
fmt.Println(err)
log.Fatal(err)
} else {
fmt.Println(item)
}
} else if id != "" {
case id != "":
item, err := store.GetItemById(id)
if err != nil {
fmt.Println(err)
log.Fatal(err)
} else {
fmt.Println(item)
}
} else if all || len(args) == 1 {
case all || len(args) == 1:
store.ListItems(showArchived)
} else {
fmt.Println("Too many arguments for list command")
default:
log.Fatal("Too many arguments for list command")
}
case CreateTestStore:
store.CreateTestStore()
case Create:
if name == "" {
fmt.Println("Please provide a name for the item to create")
return
log.Fatal("Please provide a name for the item to create")
}
err := store.CreateItem(name, description, status)
if err != nil {
log.Fatal(err)
}
store.CreateItem(name, description, status)
case Delete:
if name == "" && id == "" {
fmt.Println("Please provide a name or id for the item to delete")
return
log.Fatal("Please provide a name or id for the item to delete")
}
store.DeleteItem(name, id)
case Update:
if name == "" && id == "" {
fmt.Println("Please provide a name or id for the item to update. If you'd like to update the name, you must provide the id.")
return
log.Fatal("Please provide a name or id for the item to update. If you'd like to update the name, you must provide the id.")
}
err := store.UpdateItem(id, name, description, status, allowEmpty)
if err != nil {
log.Fatal(err)
}
store.UpdateItem(id, name, description, status, allowEmpty)
case Progress:
if name == "" && id == "" {
fmt.Println("Please provide a name or id for the item to progress")
return
log.Fatal("Please provide a name or id for the item to progress")
}
err := store.ProgressItem(id, name)
if err != nil {
log.Fatal(err)
}
store.ProgressItem(id, name)
case Archive:
store.UpdateItem(id, name, "", "archived", false)
case ChangeStatus:
store.UpdateItem(id, name, "", status, false)
default:
fmt.Println("Invalid command: " + args[0] + ". Use 'help' to see available commands.")
log.Fatal("Invalid command: " + args[0] + ". Use 'help' to see available commands.")
}
}
28 changes: 13 additions & 15 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *store) GetItemById(id string) (item, error) {
return item{}, fmt.Errorf("Item not found")
}

func (s *store) CreateItem(name string, description string, status string) {
func (s *store) CreateItem(name string, description string, status string) error {
// Currently not forcing uniqueness on names... This is going to be a problem
// but I'll punt it for now
now := GetCurrentTimeString()
Expand All @@ -81,8 +81,7 @@ func (s *store) CreateItem(name string, description string, status string) {
}
typedStatus, err := StringToStatus(status)
if err != nil {
fmt.Println(err)
return
return err
}
item := &item{
id: Guid(),
Expand All @@ -93,20 +92,20 @@ func (s *store) CreateItem(name string, description string, status string) {
description: description,
}
s.items = append(s.items, *item)
fmt.Println(s.items)
s.WriteStore()
return nil
}

func (s *store) ProgressItem(id string, name string) {
func (s *store) ProgressItem(id string, name string) error {
for i, item := range s.items {
if item.name == name && name != "" || item.id == id && id != "" {
s.items[i].ProgressStatus()
s.items[i].lastUpdated = GetCurrentTimeString()
s.WriteStore()
return
return nil
}
}
fmt.Println("Item not found")
return fmt.Errorf("Item not found")
}

func (s *store) convertItemsToRecords() [][]string {
Expand All @@ -119,18 +118,18 @@ func (s *store) convertItemsToRecords() [][]string {
return records
}

func (s *store) DeleteItem(name string, id string) {
func (s *store) DeleteItem(name string, id string) error {
for i, item := range s.items {
if item.name == name && name != "" || item.id == id && id != "" {
s.items = append(s.items[:i], s.items[i+1:]...)
s.WriteStore()
return
return nil
}
}
fmt.Println("Item not found")
return fmt.Errorf("Item not found")
}

func (s *store) UpdateItem(id string, name string, description string, status string, allowEmpty bool) {
func (s *store) UpdateItem(id string, name string, description string, status string, allowEmpty bool) error {
var itemToUpdate item
if id == "" {
itemToUpdate, _ = s.GetItemByName(name)
Expand All @@ -148,15 +147,14 @@ func (s *store) UpdateItem(id string, name string, description string, status st
if status != "" {
status, err := StringToStatus(status)
if err != nil {
fmt.Println(err)
return
return err
}
s.items[i].status = status
}
s.items[i].lastUpdated = GetCurrentTimeString()
s.WriteStore()
return
return nil
}
}
fmt.Println("Item not found")
return fmt.Errorf("Item not found")
}