Skip to content

Commit

Permalink
feat: add default config
Browse files Browse the repository at this point in the history
  • Loading branch information
dcilke committed Apr 29, 2023
1 parent d4e9241 commit f1049b3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 25 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,45 @@ brew tap dcilke/taps
brew install dcilke/taps/hz
```

## Help

```zsh
hz --help
Usage:
hz [FILE]

Application Options:
-l, --level= only output lines at this level
-s, --strict exclude non JSON output
-f, --flat flatten objects
-v, --vertical vertical output
-r, --raw raw output
-n, --no-pin exclude pinning of fields

Help Options:
-h, --help Show this help message
```

## Config

Default command options can be specified in a config file located at `$HOME/.config/hz/config.yml`.

```yaml
level:
- trace
- debug
- info
- warn
- error
- fatal
- panic
strict: false
flat: false
vertical: false
plain: false
noPin: false
```
## Why?
I use [zerolog](https://github.com/rs/zerolog) for structured logging and want to be able to quickly tap into the log streams.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ require (
github.com/jessevdk/go-flags v1.5.0
github.com/mattn/go-colorable v0.1.12
github.com/stretchr/testify v1.8.2
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dcilke/goj v0.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/dcilke/gu v0.1.0
github.com/dcilke/heron v0.2.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
golang.org/x/sys v0.6.0 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
14 changes: 7 additions & 7 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var filecases = []string{"strings", "ndjson", "pretty", "array", "mixed"}
func TestCLI(t *testing.T) {
for _, tc := range filecases {
t.Run(tc, func(t *testing.T) {
output, err := hz(fn(tc), "--plain")
output, err := hz(fn(tc), "--raw")
require.NoError(t, err)
golden.Assert(t, output)
})
Expand All @@ -22,7 +22,7 @@ func TestCLI(t *testing.T) {
func TestCLI_Strict(t *testing.T) {
for _, tc := range filecases {
t.Run(tc, func(t *testing.T) {
output, err := hz(fn(tc), "--plain", "--strict")
output, err := hz(fn(tc), "--raw", "--strict")
require.NoError(t, err)
golden.Assert(t, output)
})
Expand All @@ -33,7 +33,7 @@ func TestCLI_Level(t *testing.T) {
for _, tc := range filecases {
for _, level := range []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"} {
t.Run(tc+"/"+level, func(t *testing.T) {
output, err := hz(fn(tc), "--plain", "--level", level)
output, err := hz(fn(tc), "--raw", "--level", level)
require.NoError(t, err)
golden.Assert(t, output)
})
Expand All @@ -45,21 +45,21 @@ func TestCLI_Help(t *testing.T) {
testcases := []string{"--help", "-h"}
for _, tc := range testcases {
t.Run(tc, func(t *testing.T) {
output, err := hz(tc, "--plain")
output, err := hz(tc, "--raw")
require.NoError(t, err)
golden.Assert(t, output)
})
}
}

func TestCLI_Flat(t *testing.T) {
output, err := hz(fn("nested"), "--plain", "--flat")
output, err := hz(fn("nested"), "--raw", "--flat")
require.NoError(t, err)
golden.Assert(t, output)
}

func TestCLI_Vert(t *testing.T) {
output, err := hz(fn("nested"), "--plain", "--vertical")
output, err := hz(fn("nested"), "--raw", "--vertical")
require.NoError(t, err)
golden.Assert(t, output)
}
Expand All @@ -71,7 +71,7 @@ func TestCLI_Color(t *testing.T) {
}

func TestCLI_NoPin(t *testing.T) {
output, err := hz(fn("mixed"), "--plain", "--no-pin")
output, err := hz(fn("mixed"), "--raw", "--no-pin")
require.NoError(t, err)
golden.Assert(t, output)
}
8 changes: 4 additions & 4 deletions integration/testdata/golden/TestCLI_Help/--help
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Usage:

Application Options:
-l, --level= only output lines at this level
-s, --strict strict mode
-f, --flat flatten output
-s, --strict exclude non JSON output
-f, --flat flatten objects
-v, --vertical vertical output
-p, --plain plain (no color) output
-n, --no-pin don't pin any fields
-r, --raw raw output
-n, --no-pin exclude pinning of fields

Help Options:
-h, --help Show this help message
8 changes: 4 additions & 4 deletions integration/testdata/golden/TestCLI_Help/-h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Usage:

Application Options:
-l, --level= only output lines at this level
-s, --strict strict mode
-f, --flat flatten output
-s, --strict exclude non JSON output
-f, --flat flatten objects
-v, --vertical vertical output
-p, --plain plain (no color) output
-n, --no-pin don't pin any fields
-r, --raw raw output
-n, --no-pin exclude pinning of fields

Help Options:
-h, --help Show this help message
45 changes: 38 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,47 @@ package main
import (
"fmt"
"os"
"path/filepath"

"github.com/dcilke/gu"
"github.com/dcilke/heron"
"github.com/dcilke/hz/pkg/writer"
"github.com/jessevdk/go-flags"
"gopkg.in/yaml.v3"
)

const (
newline = "\n"
)

var cfgPath string

func init() {
home, err := os.UserHomeDir()
if err != nil {
home = "." // fallback to current directory
}
cfgPath = filepath.Join(home, ".config", "hz", "config.yml")
}

type Cmd struct {
Level []string `short:"l" long:"level" description:"only output lines at this level"`
Strict bool `short:"s" long:"strict" description:"strict mode"`
Flat bool `short:"f" long:"flat" description:"flatten output"`
Vertical bool `short:"v" long:"vertical" description:"vertical output"`
Plain bool `short:"p" long:"plain" description:"plain (no color) output"`
NoPin bool `short:"n" long:"no-pin" description:"don't pin any fields"`
Level []string `short:"l" long:"level" description:"only output lines at this level" yaml:"level"`
Strict bool `short:"s" long:"strict" description:"exclude non JSON output" yaml:"strict"`
Flat bool `short:"f" long:"flat" description:"flatten objects" yaml:"flat"`
Vertical bool `short:"v" long:"vertical" description:"vertical output" yaml:"vertical"`
Raw bool `short:"r" long:"raw" description:"raw output" yaml:"plain"`
NoPin bool `short:"n" long:"no-pin" description:"exclude pinning of fields" yaml:"noPin"`
}

func main() {
var cmd Cmd
// read in config file, if it exists
err := loadDefaults(&cmd)
if err != nil {
fmt.Fprint(os.Stderr, fmt.Errorf("WARN: unable to load config: %w", err), "\n")
}

// parse command line flags
parser := flags.NewParser(&cmd, flags.HelpFlag|flags.PassDoubleDash)
parser.Usage = "[FILE]"
filenames, err := parser.Parse()
Expand All @@ -45,7 +64,7 @@ func main() {
writer.WithLevelFilters(cmd.Level),
writer.WithFlatten(cmd.Flat),
writer.WithVertical(cmd.Vertical),
writer.WithColor(!cmd.Plain),
writer.WithColor(!cmd.Raw),
}

if cmd.NoPin {
Expand Down Expand Up @@ -97,3 +116,15 @@ func main() {

h.Process(os.Stdin)
}

func loadDefaults(cfg *Cmd) error {
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
return nil
}

bytes, err := os.ReadFile(cfgPath)
if err != nil {
return fmt.Errorf("unable to read config file: %w", err)
}
return yaml.Unmarshal(bytes, &cfg)
}

0 comments on commit f1049b3

Please sign in to comment.