Skip to content

Commit

Permalink
feat: replace hclog with charmbracelet/log (#118)
Browse files Browse the repository at this point in the history
* feat: replace hclog with charm-log

* fix lint
  • Loading branch information
j178 authored Feb 21, 2023
1 parent 3589408 commit 806fd0d
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 87 deletions.
6 changes: 3 additions & 3 deletions cmd/contest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
"github.com/charmbracelet/log"
"github.com/fatih/color"
"github.com/hako/durafmt"
"github.com/hashicorp/go-hclog"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -173,7 +173,7 @@ leetgo contest left w330
if err != nil {
return err
}
hclog.L().Info("registered", "contest", contest.Title, "user", user.Whoami(c))
log.Info("registered", "contest", contest.Title, "user", user.Whoami(c))
} else {
return nil
}
Expand Down Expand Up @@ -256,7 +256,7 @@ var unregisterCmd = &cobra.Command{
if err != nil {
return err
}
hclog.L().Info("unregistered", "contest", contest.Title, "user", user.Whoami(c))
log.Info("unregistered", "contest", contest.Title, "user", user.Whoami(c))
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/PullRequestInc/go-gpt3"
"github.com/charmbracelet/glamour"
"github.com/hashicorp/go-hclog"
"github.com/charmbracelet/log"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -112,7 +112,7 @@ func askOpenAI(cmd *cobra.Command, q *leetcode.QuestionData, code string) (strin
q.GetFormattedContent(),
code,
)
hclog.L().Debug("requesting openai", "prompt", prompt)
log.Debug("requesting openai", "prompt", prompt)
spin := newSpinner(cmd.OutOrStdout())
spin.Suffix = " Waiting for OpenAI..."
spin.Start()
Expand All @@ -133,7 +133,7 @@ func askOpenAI(cmd *cobra.Command, q *leetcode.QuestionData, code string) (strin
if len(resp.Choices) == 0 {
return "", errNoFix
}
hclog.L().Debug("got response from openai", "response", resp.Choices)
log.Debug("got response from openai", "response", resp.Choices)
text := resp.Choices[0].Text
text = utils.EnsureTrailingNewline(text)
return text, nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"strings"

"github.com/hashicorp/go-hclog"
"github.com/charmbracelet/log"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
Expand All @@ -27,7 +27,7 @@ var infoCmd = &cobra.Command{
for _, qid := range args {
qs, err := leetcode.ParseQID(qid, c)
if err != nil {
hclog.L().Error("failed to get question", "qid", qid, "err", err)
log.Error("failed to get question", "qid", qid, "err", err)
continue
}
questions = append(questions, qs...)
Expand Down
8 changes: 4 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

"github.com/hashicorp/go-hclog"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"

"github.com/j178/leetgo/config"
Expand Down Expand Up @@ -58,7 +58,7 @@ func createConfigDir() error {
if err != nil {
return err
}
hclog.L().Info("config dir created", "dir", dir)
log.Info("config dir created", "dir", dir)
return nil
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func createConfigFiles(dir string) error {
if err != nil {
return err
}
hclog.L().Info("global config file created", "file", globalFile)
log.Info("global config file created", "file", globalFile)
}

projectFile := filepath.Join(dir, config.ProjectConfigFilename)
Expand Down Expand Up @@ -118,7 +118,7 @@ leetcode:
site,
),
)
hclog.L().Info("project config file created", "file", projectFile)
log.Info("project config file created", "file", projectFile)

return nil
}
Expand Down
18 changes: 6 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"runtime"
"runtime/debug"

"github.com/charmbracelet/log"
"github.com/fatih/color"
"github.com/hashicorp/go-hclog"
cc "github.com/ivanpirog/coloredcobra"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -74,25 +74,19 @@ func UsageString() string {

func initWorkDir() error {
if dir := os.Getenv("LEETGO_WORKDIR"); dir != "" {
hclog.L().Debug("change workdir to LEETGO_WORKDIR", "dir", dir)
log.Debug("change workdir to LEETGO_WORKDIR", "dir", dir)
return os.Chdir(dir)
}
return nil
}

func initLogger() {
opts := &hclog.LoggerOptions{
Level: hclog.Info,
DisableTime: true,
Color: hclog.AutoColor,
ColorHeaderOnly: true,
}
log.SetReportTimestamp(false)
log.SetLevel(log.InfoLevel)
if config.Debug {
opts.Level = hclog.Trace
opts.DisableTime = false
opts.Color = hclog.ColorOff
log.SetReportTimestamp(true)
log.SetLevel(log.DebugLevel)
}
hclog.SetDefault(hclog.New(opts))
}

func initCommands() {
Expand Down
6 changes: 3 additions & 3 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"

"github.com/hashicorp/go-hclog"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"

"github.com/j178/leetgo/config"
Expand Down Expand Up @@ -43,10 +43,10 @@ leetgo submit w330/
limiter := newLimiter(user)

for _, q := range qs {
hclog.L().Info("submitting solution", "question", q.TitleSlug, "user", user.Whoami(c))
log.Info("submitting solution", "question", q.TitleSlug, "user", user.Whoami(c))
result, err := submitSolution(cmd, q, c, gen, limiter)
if err != nil {
hclog.L().Error("failed to submit solution", "question", q.TitleSlug, "err", err)
log.Error("failed to submit solution", "question", q.TitleSlug, "err", err)
continue
}
cmd.Print(result.Display(qs[0]))
Expand Down
12 changes: 6 additions & 6 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/briandowns/spinner"
"github.com/hashicorp/go-hclog"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"

"github.com/j178/leetgo/config"
Expand Down Expand Up @@ -88,17 +88,17 @@ leetgo test w330/`,
for _, q := range qs {
localPassed, remotePassed := true, true
if runLocally {
hclog.L().Info("running test locally", "question", q.TitleSlug)
log.Info("running test locally", "question", q.TitleSlug)
localPassed, err = lang.RunLocalTest(q)
if err != nil {
hclog.L().Error("failed to run test locally", "question", q.TitleSlug, "err", err)
log.Error("failed to run test locally", "question", q.TitleSlug, "err", err)
}
}
if runRemotely {
hclog.L().Info("running test remotely", "question", q.TitleSlug, "user", user.Whoami(c))
log.Info("running test remotely", "question", q.TitleSlug, "user", user.Whoami(c))
result, err := runTestRemotely(cmd, q, c, gen, testLimiter)
if err != nil {
hclog.L().Error("failed to run test remotely", "question", q.TitleSlug, "err", err)
log.Error("failed to run test remotely", "question", q.TitleSlug, "err", err)
remotePassed = false
} else {
cmd.Print(result.Display(q))
Expand All @@ -109,7 +109,7 @@ leetgo test w330/`,
if localPassed && remotePassed && autoSubmit {
result, err := submitSolution(cmd, q, c, gen, submitLimiter)
if err != nil {
hclog.L().Error("failed to submit solution", "question", q.TitleSlug, "err", err)
log.Error("failed to submit solution", "question", q.TitleSlug, "err", err)
} else {
cmd.Print(result.Display(q))
}
Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/hashicorp/go-hclog"
"github.com/charmbracelet/log"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -316,7 +316,7 @@ func Load(init bool) error {
if err != nil {
if os.IsNotExist(err) {
if !init {
hclog.L().Warn(
log.Warn(
"global config file not found, have you ran `leetgo init`?",
"file",
cfg.GlobalConfigFile(),
Expand All @@ -334,7 +334,7 @@ func Load(init bool) error {
err = viper.MergeInConfig()
if err != nil {
if os.IsNotExist(err) {
hclog.L().Warn(
log.Warn(
fmt.Sprintf("%s not found, use global config only", ProjectConfigFilename),
"file",
cfg.GlobalConfigFile(),
Expand Down
12 changes: 6 additions & 6 deletions config/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package config
import (
"os"

"github.com/charmbracelet/log"
"github.com/goccy/go-json"
"github.com/hashicorp/go-hclog"

"github.com/j178/leetgo/utils"
)
Expand All @@ -30,15 +30,15 @@ func loadStates() States {
file := Get().StateFile()
f, err := os.Open(file)
if err != nil {
hclog.L().Debug("failed to open state file", "err", err)
log.Debug("failed to open state file", "err", err)
return s
}
defer func() { _ = f.Close() }()

dec := json.NewDecoder(f)
err = dec.Decode(&s)
if err != nil {
hclog.L().Debug("failed to load state", "err", err)
log.Debug("failed to load state", "err", err)
}

return s
Expand All @@ -58,17 +58,17 @@ func SaveState(s State) {

err := utils.CreateIfNotExists(file, false)
if err != nil {
hclog.L().Error("failed to create state file", "err", err)
log.Error("failed to create state file", "err", err)
return
}
f, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
hclog.L().Error("failed to open state file", "err", err)
log.Error("failed to open state file", "err", err)
return
}
enc := json.NewEncoder(f)
err = enc.Encode(states)
if err != nil {
hclog.L().Error("failed to save state", "err", err)
log.Error("failed to save state", "err", err)
}
}
12 changes: 6 additions & 6 deletions editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"os/exec"

"github.com/hashicorp/go-hclog"
"github.com/charmbracelet/log"

"github.com/j178/leetgo/config"
"github.com/j178/leetgo/lang"
Expand Down Expand Up @@ -42,7 +42,7 @@ var editors = map[string]Opener{
type noneEditor struct{}

func (e *noneEditor) Open(file lang.FileOutput) error {
hclog.L().Info("none editor is used, skip opening files")
log.Info("none editor is used, skip opening files")
return nil
}

Expand All @@ -52,7 +52,7 @@ type commonEditor struct {
}

func (e *commonEditor) Open(file lang.FileOutput) error {
hclog.L().Info("opening file", "command", e.command)
log.Info("opening file", "command", e.command)
return runCmd(e.command, e.args, file.Path)
}

Expand All @@ -65,7 +65,7 @@ func (e *commonMultiEditor) OpenMulti(files []lang.FileOutput) error {
for i, f := range files {
paths[i] = f.Path
}
hclog.L().Info("opening files", "command", e.command)
log.Info("opening files", "command", e.command)
return runCmd(e.command, e.args, paths...)
}

Expand All @@ -74,10 +74,10 @@ type customEditor struct{}
func (e *customEditor) Open(file lang.FileOutput) error {
cfg := config.Get()
if cfg.Editor.Command == "" {
hclog.L().Warn("editor.command is empty, skip opening files")
log.Warn("editor.command is empty, skip opening files")
return nil
}
hclog.L().Info("opening files", "command", cfg.Editor.Command)
log.Info("opening files", "command", cfg.Editor.Command)
return runCmd(cfg.Editor.Command, cfg.Editor.Args, file.Path)
}

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ require (
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/lipgloss v0.6.0
github.com/charmbracelet/log v0.1.0
github.com/dghubble/sling v1.4.1
github.com/dop251/goja v0.0.0-20230216180835-5937a312edda
github.com/fatih/color v1.14.1
github.com/goccy/go-json v0.10.0
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
github.com/hashicorp/go-hclog v1.4.0
github.com/hexops/gotextdiff v1.0.3
github.com/ivanpirog/coloredcobra v1.0.1
github.com/jedib0t/go-pretty/v6 v6.4.4
Expand Down Expand Up @@ -55,6 +55,7 @@ require (
github.com/dlclark/regexp2 v1.8.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-sqlite/sqlite3 v0.0.0-20180313105335-53dd8e640ee7 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
Expand Down
Loading

0 comments on commit 806fd0d

Please sign in to comment.