Skip to content

Commit

Permalink
add cli to add fridays and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoegel committed Dec 28, 2023
1 parent cf3b260 commit 0cccc7d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
92 changes: 88 additions & 4 deletions pkg/pizza/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"

"go.uber.org/zap"
)

func Edit(args []string) {
nextFriday := time.Now()

fs := flag.NewFlagSet("edit", flag.ExitOnError)
isInteractive := fs.Bool("i", false, "interactive mode")
_ = fs.String("addFriday", formatEditDate(nextFriday), "add friday")
_ = fs.String("removeFriday", formatEditDate(nextFriday), "remove friday")
add := fs.String("a", "", `add a new friend or friday, formatted as
friend://<email>/<first name>/<last name> - to add a new friend
friday://YYYY/MM/DD - to add a new friday`)
remove := fs.String("d", "", `remove a new friend or friday, formatted as
friend://<email>/<first name>/<last name> - to remove a friend
friday://YYYY/MM/DD - to remove a friday`)
fs.Parse(args)

config := LoadConfigEnv()
Expand All @@ -28,11 +32,91 @@ func Edit(args []string) {
}
}

if len(*add) > 0 {
addParts := strings.SplitN(*add, "://", 2)
if len(addParts) != 2 {
fmt.Println("invalid add argument")
os.Exit(1)
}
switch addParts[0] {
case "friend":
addNewFriend(accessor, addParts[1])
case "friday":
addNewFriday(accessor, addParts[1])
default:
fmt.Println("invalid add target")
os.Exit(1)
}
}

if len(*remove) > 0 {
removeParts := strings.SplitN(*remove, "://", 2)
if len(removeParts) != 2 {
fmt.Println("invalid remove argument")
os.Exit(1)
}
switch removeParts[0] {
case "friend":
removeFriend(accessor, removeParts[1])
case "friday":
removeFriday(accessor, removeParts[1])
default:
fmt.Println("invalid remove target")
os.Exit(1)
}
}

if *isInteractive {
interactiveEdit(accessor)
}
}

func addNewFriend(accessor Accessor, newFriend string) {
newFriendParts := strings.SplitN(newFriend, "/", 2)
if len(newFriendParts) != 2 {
fmt.Printf("invalid friend format: %s\n", newFriend)
os.Exit(1)
}
email := newFriendParts[0]
name := newFriendParts[1]
name = strings.ReplaceAll(name, "/", " ")
if err := accessor.AddFriend(email, name); err != nil {
fmt.Printf("failed to add friend: %v\n", err)
} else {
fmt.Printf("added new friend: %s\n", email)
}
}

func addNewFriday(accessor Accessor, newFriday string) {
newFridayParts := strings.SplitN(newFriday, "/", 3)
if len(newFridayParts) != 3 {
fmt.Printf("invalid friday format: %s\n", newFriday)
os.Exit(1)
}
year, err1 := strconv.Atoi(newFridayParts[0])
month, err2 := strconv.Atoi(newFridayParts[1])
day, err3 := strconv.Atoi(newFridayParts[2])
if err1 != nil && err2 != nil && err3 != nil {
fmt.Printf("invalid friday: %s\n", newFriday)
os.Exit(1)
}
loc, _ := time.LoadLocation("America/New_York")
f := time.Date(year, time.Month(month), day, 17, 30, 0, 0, loc)
if err := accessor.AddFriday(f); err != nil {
fmt.Printf("accessor failure: %v\n", err)
os.Exit(1)
}
fmt.Printf("added friday: %s\n", formatEditDate(f))
}

func removeFriend(accessor Accessor, friend string) {

}

func removeFriday(accessor Accessor, fridayStr string) {

}

func interactiveEdit(accessor Accessor) {
nextFourFridays := make([]time.Time, 4)
start := time.Now()
Expand Down
5 changes: 5 additions & 0 deletions pkg/pizza/fauna.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,8 @@ func (c *FaunaClient) AddFriday(date time.Time) error {
Log.Panic("not implemented")
return nil
}

func (c *FaunaClient) AddFriend(email, name string) error {
Log.Panic("not implemented")
return nil
}
1 change: 1 addition & 0 deletions pkg/pizza/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Accessor interface {
GetUpcomingFridays(daysAhead int) ([]time.Time, error)
GetUpcomingFridaysAfter(after time.Time, daysAhead int) ([]time.Time, error)
AddFriday(date time.Time) error
AddFriend(email, name string) error
}

const (
Expand Down
5 changes: 5 additions & 0 deletions pkg/pizza/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (m *MockAccessor) AddFriday(date time.Time) error {
return args.Error(0)
}

func (m *MockAccessor) AddFriend(email, name string) error {
args := m.Called(email, name)
return args.Error(0)
}

func TestStore_IsFriendAllowed(t *testing.T) {
// GIVEN
friend := "ted@tedlasso.com"
Expand Down

0 comments on commit 0cccc7d

Please sign in to comment.