Skip to content

Commit

Permalink
Merge pull request #5 from drewstinnett/feature-pretty-log
Browse files Browse the repository at this point in the history
Feature: Pretty logging
  • Loading branch information
drewstinnett authored Jan 20, 2024
2 parents 59ac532 + a6ede1d commit 8c0df0d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</p>

---
[![codecov](https://codecov.io/gh/drewstinnett/letseat/graph/badge.svg?token=L5DYWFXHRE)](https://codecov.io/gh/drewstinnett/letseat)

# Lets Eat

Expand Down
5 changes: 1 addition & 4 deletions cmd/letseat/cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ Package cmd is the cli app
package cmd

import (
"errors"
"fmt"
"log/slog"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -43,8 +41,7 @@ func runAnalyze(cmd *cobra.Command, args []string) error {
// Find best rated meals
placesDetails := diary.PlaceDetails()
if len(placesDetails) == 0 {
slog.Error(fmt.Sprintf("No entries found! Try adding some with %v log", os.Args[0]))
return errors.New("no entries found")
return fmt.Errorf("No entries found! Try adding some with %v log", os.Args[0])
}

// Print highest rated
Expand Down
23 changes: 19 additions & 4 deletions cmd/letseat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path"
"time"

"github.com/charmbracelet/log"

"github.com/adrg/xdg"
"github.com/spf13/cobra"

Expand All @@ -18,6 +20,7 @@ var (
g *gout.Client
config configPaths
version string = "dev"
verbose bool
)

type configPaths struct {
Expand All @@ -43,7 +46,8 @@ func newRootCmd() *cobra.Command {
},
// Run: func(cmd *cobra.Command, args []string) { },
}
cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.letseat.yaml)")
cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "use a custom configuration")
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "use verbose logging")
bindRootArgs(cmd)
cmd.AddCommand(
newAnalyzeCmd(),
Expand All @@ -58,7 +62,11 @@ func newRootCmd() *cobra.Command {
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(newRootCmd().Execute())
err := newRootCmd().Execute()
if err != nil {
slog.Error("exiting", "error", err)
os.Exit(2)
}
}

func init() {
Expand Down Expand Up @@ -103,17 +111,24 @@ func initConfig() {
// Find home directory.
// home, err := homedir.Dir()
// cobra.CheckErr(err)
opts := log.Options{}
if verbose {
opts.Level = log.DebugLevel
}
logger := slog.New(
log.NewWithOptions(os.Stderr, opts),
)
slog.SetDefault(logger)

// Search config in home directory with name ".cli" (without extension).
// viper.AddConfigPath(home)
viper.SetConfigName("letseat")
viper.AddConfigPath(config.ConfigPath)

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
config.ConfigFile = viper.ConfigFileUsed()
slog.Info("using config file", "file", config.DataFile)
slog.Debug("using config file", "file", config.ConfigFile)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/huh v0.2.3
github.com/charmbracelet/lipgloss v0.9.1
github.com/charmbracelet/log v0.3.1
github.com/drewstinnett/go-output-format/v2 v2.1.0
github.com/drewstinnett/gout/v2 v2.1.2
github.com/gosimple/slug v1.13.1
Expand All @@ -29,6 +30,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down Expand Up @@ -56,6 +58,7 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/yuin/goldmark v1.6.0 // indirect
github.com/yuin/goldmark-emoji v1.0.2 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/charmbracelet/huh v0.2.3 h1:fZaqnd/fiO7jlfcLqhP2iwpLt670IaHQfL/7Qu+fB
github.com/charmbracelet/huh v0.2.3/go.mod h1:XmADLRnJs/Jqw7zIbi9BTss5gXbOkR6feyVoNAp19rA=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/charmbracelet/log v0.3.1 h1:TjuY4OBNbxmHWSwO3tosgqs5I3biyY8sQPny/eCMTYw=
github.com/charmbracelet/log v0.3.1/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -95,6 +97,8 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -284,6 +288,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down

0 comments on commit 8c0df0d

Please sign in to comment.