Skip to content

Commit

Permalink
feat: add log command
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Nov 7, 2023
1 parent 46328de commit ef3120c
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 11 deletions.
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module github.com/charmbracelet/gum

go 1.18
go 1.19

require (
github.com/alecthomas/kong v0.8.1
github.com/alecthomas/mango-kong v0.1.0
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/glamour v0.6.1-0.20230531150759-6d5b52861a9d
github.com/charmbracelet/lipgloss v0.9.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/charmbracelet/log v0.3.0
github.com/mattn/go-isatty v0.0.20
github.com/muesli/reflow v0.3.0
github.com/muesli/roff v0.1.0
Expand All @@ -24,6 +25,7 @@ require (
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -36,10 +38,10 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/yuin/goldmark v1.5.4 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
)
20 changes: 14 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
github.com/charmbracelet/glamour v0.6.1-0.20230531150759-6d5b52861a9d h1:S4Ejl/M2VrryIgDrDbiuvkwMUDa67/t/H3Wz3i2/vUw=
github.com/charmbracelet/glamour v0.6.1-0.20230531150759-6d5b52861a9d/go.mod h1:swCB3CXFsh22H1ESDYdY1tirLiNqCziulDyJ1B6Nt7Q=
github.com/charmbracelet/lipgloss v0.9.0 h1:BHIM7U4vX77xGEld8GrTKspBMtSv7j0wxPCH73nrdxE=
github.com/charmbracelet/lipgloss v0.9.0/go.mod h1:h8KDyaivONasw1Bhb4nWiKlk4P1wHPly+3+3v6EFMmA=
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.0 h1:u5aB2KJDgNZo4WOfOC8C+KvGIkJ2rCFNlPWDu6xhnqI=
github.com/charmbracelet/log v0.3.0/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0=
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
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/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand Down Expand Up @@ -56,28 +61,31 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark-emoji v1.0.1 h1:ctuWEyzGBwiucEqxzwe0SOYDXPAucOrE9NQC18Wa1os=
github.com/yuin/goldmark-emoji v1.0.1/go.mod h1:2w1E6FEWLcDQkoTE+7HU6QF1F6SLlNGjRIBbIZQFqkQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
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/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10 changes: 10 additions & 0 deletions gum.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/gum/format"
"github.com/charmbracelet/gum/input"
"github.com/charmbracelet/gum/join"
"github.com/charmbracelet/gum/log"
"github.com/charmbracelet/gum/man"
"github.com/charmbracelet/gum/pager"
"github.com/charmbracelet/gum/spin"
Expand Down Expand Up @@ -204,4 +205,13 @@ type Gum struct {
// $ gum write > output.text
//
Write write.Options `cmd:"" help:"Prompt for long-form text"`

// Log provides a shell script interface for logging using Log.
// https://github.com/charmbracelet/log
//
// It can be used to log messages to output.
//
// $ gum log --level info "Hello, world!"
//
Log log.Options `cmd:"" help:"Log messages to output"`
}
135 changes: 135 additions & 0 deletions log/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package log

import (
"math"
"os"
"strings"

"github.com/charmbracelet/log"
)

// Run is the command-line interface for logging text.
func (o Options) Run() error {
l := log.New(os.Stderr)

if o.File != "" {
f, err := os.OpenFile(o.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)

Check failure on line 16 in log/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0644, in <argument> detected (gomnd)

Check failure on line 16 in log/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 0644, in <argument> detected (gomnd)
if err != nil {
return err

Check failure on line 18 in log/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func os.OpenFile(name string, flag int, perm io/fs.FileMode) (*os.File, error) (wrapcheck)

Check failure on line 18 in log/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func os.OpenFile(name string, flag int, perm io/fs.FileMode) (*os.File, error) (wrapcheck)
}

defer f.Close() // nolint: errcheck

Check failure on line 21 in log/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

directive `// nolint: errcheck` should be written without leading space as `//nolint: errcheck` (nolintlint)

Check failure on line 21 in log/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

directive `// nolint: errcheck` should be written without leading space as `//nolint: errcheck` (nolintlint)
l.SetOutput(f)
}

l.SetPrefix(o.Prefix)
l.SetLevel(-math.MaxInt32) // log all levels
l.SetReportTimestamp(o.Time)
l.SetTimeFormat(o.TimeFormat)

st := log.DefaultStyles()
st.Levels[log.DebugLevel] = o.LevelDebugStyle.ToLipgloss().
Inline(true).
SetString(strings.ToUpper(log.DebugLevel.String()))
st.Levels[log.InfoLevel] = o.LevelInfoStyle.ToLipgloss().
Inline(true).
SetString(strings.ToUpper(log.InfoLevel.String()))
st.Levels[log.WarnLevel] = o.LevelWarnStyle.ToLipgloss().
Inline(true).
SetString(strings.ToUpper(log.WarnLevel.String()))
st.Levels[log.ErrorLevel] = o.LevelErrorStyle.ToLipgloss().
Inline(true).
SetString(strings.ToUpper(log.ErrorLevel.String()))
st.Levels[log.FatalLevel] = o.LevelFatalStyle.ToLipgloss().
Inline(true).
SetString(strings.ToUpper(log.FatalLevel.String()))
st.Timestamp = o.TimeStyle.ToLipgloss().
Inline(true)
st.Prefix = o.PrefixStyle.ToLipgloss().
Inline(true)
st.Message = o.MessageStyle.ToLipgloss().
Inline(true)
st.Key = o.KeyStyle.ToLipgloss().
Inline(true)
st.Value = o.ValueStyle.ToLipgloss().
Inline(true)
st.Separator = o.SeparatorStyle.ToLipgloss().
Inline(true)

l.SetStyles(st)

switch o.Formatter {
case "json":
l.SetFormatter(log.JSONFormatter)
case "logfmt":
l.SetFormatter(log.LogfmtFormatter)
case "text":
l.SetFormatter(log.TextFormatter)
}

var arg0 string
var args []interface{}
if len(o.Text) > 0 {
arg0 = o.Text[0]
}

if len(o.Text) > 1 {
args = make([]interface{}, len(o.Text[1:]))
for i, arg := range o.Text[1:] {
args[i] = arg
}
}

switch o.Level {
case "none":
if o.Format {
l.Printf(arg0, args...)
} else if o.Structured {
l.Print(arg0, args...)
} else {
l.Print(strings.Join(o.Text, " "))
}
case "debug":
if o.Format {
l.Debugf(arg0, args...)
} else if o.Structured {
l.Debug(arg0, args...)
} else {
l.Debug(strings.Join(o.Text, " "))
}
case "info":
if o.Format {
l.Infof(arg0, args...)
} else if o.Structured {
l.Info(arg0, args...)
} else {
l.Info(strings.Join(o.Text, " "))
}
case "warn":
if o.Format {
l.Warnf(arg0, args...)
} else if o.Structured {
l.Warn(arg0, args...)
} else {
l.Warn(strings.Join(o.Text, " "))
}
case "error":
if o.Format {
l.Errorf(arg0, args...)
} else if o.Structured {
l.Error(arg0, args...)
} else {
l.Error(strings.Join(o.Text, " "))
}
case "fatal":
if o.Format {
l.Fatalf(arg0, args...)
} else if o.Structured {
l.Fatal(arg0, args...)
} else {
l.Fatal(strings.Join(o.Text, " "))
}
}

return nil
}
38 changes: 38 additions & 0 deletions log/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package log

import (
"github.com/alecthomas/kong"
"github.com/charmbracelet/gum/style"
)

// Options is the set of options that can configure a join.
type Options struct {
Text []string `arg:"" help:"Text to log"`

File string `short:"o" help:"Log to file"`
Format bool `short:"f" help:"Format message using printf" xor:"format,structured"`
Formatter string `help:"The log formatter to use" enum:"json,logfmt,text" default:"text"`
Level string `help:"The log level to use" enum:"none,debug,info,warn,error,fatal" default:"none"`
Prefix string `help:"Prefix to print before the message"`
Structured bool `short:"s" help:"Use structured logging" xor:"format,structured"`
Time bool `help:"Whether to print the time"`
TimeFormat string `help:"The time format to use" default:"2006/01/02 15:04:05"`

LevelDebugStyle style.Styles `embed:"" prefix:"level.debug." help:"The style of the debug level" set:"defaultBold=true" set:"defaultForeground=63" envprefix:"GUM_LOG_LEVEL_DEBUG_"` //nolint:staticcheck
LevelInfoStyle style.Styles `embed:"" prefix:"level.info." help:"The style of the info level" set:"defaultBold=true" set:"defaultForeground=83" envprefix:"GUM_LOG_LEVEL_INFO_"` //nolint:staticcheck
LevelWarnStyle style.Styles `embed:"" prefix:"level.warn." help:"The style of the warn level" set:"defaultBold=true" set:"defaultForeground=192" envprefix:"GUM_LOG_LEVEL_WARN_"` //nolint:staticcheck
LevelErrorStyle style.Styles `embed:"" prefix:"level.error." help:"The style of the error level" set:"defaultBold=true" set:"defaultForeground=204" envprefix:"GUM_LOG_LEVEL_ERROR_"` //nolint:staticcheck
LevelFatalStyle style.Styles `embed:"" prefix:"level.fatal." help:"The style of the fatal level" set:"defaultBold=true" set:"defaultForeground=134" envprefix:"GUM_LOG_LEVEL_FATAL_"` //nolint:staticcheck
TimeStyle style.Styles `embed:"" prefix:"time." help:"The style of the time" envprefix:"GUM_LOG_TIME_"`
PrefixStyle style.Styles `embed:"" prefix:"prefix." help:"The style of the prefix" set:"defaultBold=true" set:"defaultFaint=true" envprefix:"GUM_LOG_PREFIX_"` //nolint:staticcheck
MessageStyle style.Styles `embed:"" prefix:"message." help:"The style of the message" envprefix:"GUM_LOG_MESSAGE_"`
KeyStyle style.Styles `embed:"" prefix:"key." help:"The style of the key" set:"defaultFaint=true" envprefix:"GUM_LOG_KEY_"`
ValueStyle style.Styles `embed:"" prefix:"value." help:"The style of the value" envprefix:"GUM_LOG_VALUE_"`
SeparatorStyle style.Styles `embed:"" prefix:"separator." help:"The style of the separator" set:"defaultFaint=true" envprefix:"GUM_LOG_SEPARATOR_"`
}

// BeforeReset hook. Used to unclutter style flags.
func (o Options) BeforeReset(ctx *kong.Context) error {
style.HideFlags(ctx)
return nil
}
2 changes: 1 addition & 1 deletion table/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (o Options) Run() error {
}

if o.Print {
headers := make([]any, 0, len(columnNames))
headers := make([]string, 0, len(columnNames))
for _, name := range columnNames {

Check failure on line 91 in table/command.go

View workflow job for this annotation

GitHub Actions / lint

S1011: should replace loop with `headers = append(headers, columnNames...)` (gosimple)
headers = append(headers, name)
}
Expand Down

0 comments on commit ef3120c

Please sign in to comment.