Skip to content

Commit

Permalink
Add flags to control output
Browse files Browse the repository at this point in the history
Signed-off-by: Luiz Carvalho <[email protected]>
  • Loading branch information
lcarva committed Jun 23, 2023
1 parent ed7322f commit 848edc4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
8 changes: 4 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

func Character(today time.Time, config Config) (string, error) {
func Character(today time.Time, config Config) (string, string, error) {

for _, rule := range config.Rules {
var end time.Time
Expand All @@ -18,13 +18,13 @@ func Character(today time.Time, config Config) (string, error) {
weekday := time.Weekday(rule.Weekday)
end = endOfNthWeekdayOfMonth(today, month, rule.Week, weekday)
} else {
return "", errors.New(fmt.Sprint(rule.Name, " is not a valid rule"))
return "", "", errors.New(fmt.Sprint(rule.Name, " is not a valid rule"))
}
if inSeason(today, end, rule.Span) {
return rule.Emoji, nil
return rule.Emoji, rule.Name, nil
}
}
return config.Default, nil
return config.Default, "(default)", nil
}

func inSeason(today time.Time, end time.Time, spanDays int) bool {
Expand Down
36 changes: 31 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/lcarva/festoji/app"
)

var (
n = flag.Int("n", 1, "number of characters/days to display")
day = flag.Bool("day", false, "display day for the character")
rule = flag.Bool("rule", false, "display matching rule for the character")
)

func main() {
flag.Parse()

userConfigPath := os.ExpandEnv("${HOME}/.festoji.yaml")
config, errConfig := app.NewConfig(userConfigPath)
if errConfig != nil {
log.Fatal("Unable to load configuration: ", errConfig)
return
}
character, errChar := app.Character(time.Now(), config)
if errChar != nil {
log.Fatal("Unable to get character: ", errChar)
return

t := time.Now()
for i := 0; i < *n; i++ {
character, ruleName, errChar := app.Character(t, config)
if errChar != nil {
log.Fatal("Unable to get character: ", errChar)
return
}

var parts []string
if *day {
parts = append(parts, t.Format("02/Jan/2006"))
}
parts = append(parts, character)
if *rule {
parts = append(parts, ruleName)
}
fmt.Println(strings.Join(parts, " "))

t = t.Add(time.Hour * 24)
}
fmt.Println(character)

}

0 comments on commit 848edc4

Please sign in to comment.