Skip to content

Commit

Permalink
save state somewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme authored and Antoine Grondin committed Jan 14, 2023
1 parent e7ad32d commit 64c6d86
Show file tree
Hide file tree
Showing 22 changed files with 2,681 additions and 306 deletions.
118 changes: 77 additions & 41 deletions cmd/humanlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"os/signal"
"runtime"
"strings"

"github.com/aybabtme/rgbterm"
"github.com/blang/semver"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/humanlogio/humanlog"
"github.com/humanlogio/humanlog/internal/pkg/config"
"github.com/humanlogio/humanlog/internal/pkg/sink/stdiosink"
"github.com/humanlogio/humanlog/internal/pkg/state"
"github.com/mattn/go-colorable"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -150,47 +150,22 @@ func newApp() *cli.App {
app.Usage = "reads structured logs from stdin, makes them pretty on stdout!"

var (
ctx context.Context
cancel context.CancelFunc
statefile *StateFile
updateRes <-chan *checkForUpdateRes
ctx context.Context
cancel context.CancelFunc
cfg *config.Config
stateFilepath string
statefile *state.State
updateRes <-chan *checkForUpdateRes
)

app.Before = func(c *cli.Context) error {
ctx, cancel = signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
req := &checkForUpdateReq{
arch: runtime.GOARCH,
os: runtime.GOOS,
current: Version,
}
if statefile != nil {
req.accountID = statefile.AccountID
req.machineID = statefile.MachineID
}
updateRes = checkForUpdate(ctx, req)
return nil
}
app.After = func(c *cli.Context) error {
cancel()
select {
case nextVersion := <-updateRes:
semverVersion.LT(nextVersion)
if Version != nextVersion {
log.Printf("a new version of humanlog is available: please update")
}
default:
}
return nil
}

app.Flags = []cli.Flag{configFlag, skipFlag, keepFlag, sortLongest, skipUnchanged, truncates, truncateLength, colorFlag, lightBg, timeFormat, ignoreInterrupts, messageFieldsFlag, timeFieldsFlag, levelFieldsFlag}
app.Action = func(c *cli.Context) error {

configFilepath, err := config.GetDefaultConfigFilepath()
if err != nil {
return fmt.Errorf("looking up config file path: %v", err)
}
// read config
var cfg *config.Config
if c.IsSet(configFlag.Name) {
configFilepath = c.String(configFlag.Name)
cfgFromFlag, err := config.ReadConfigFile(configFilepath, &config.DefaultConfig)
Expand All @@ -206,8 +181,63 @@ func newApp() *cli.App {
cfg = cfgFromDir
}

// flags overwrite config file
stateFilepath, err = state.GetDefaultStateFilepath()
if err != nil {
return fmt.Errorf("looking up state file path: %v", err)
}
// read state
statefile, err = state.ReadStateFile(stateFilepath, &state.DefaultState)
if err != nil {
return fmt.Errorf("reading default config file: %v", err)
}

if cfg.CheckForUpdates != nil && *cfg.CheckForUpdates {
req := &checkForUpdateReq{
arch: runtime.GOARCH,
os: runtime.GOOS,
current: Version,
}
if statefile != nil {
if statefile.AccountID != nil {
req.accountID = *statefile.AccountID
}
if statefile.MachineID != nil {
req.machineID = *statefile.MachineID
}
}
updateRes = checkForUpdate(ctx, req)
}
return nil
}
app.After = func(c *cli.Context) error {
cancel()
select {
case res := <-updateRes:
if semverVersion.LT(res.sem) {
log.Printf("a new version of humanlog is available: please update")
}
updateStatefile := false
if statefile.AccountID == nil && res.accountID != "" {
updateStatefile = true
statefile.AccountID = &res.accountID
}
if statefile.MachineID == nil && res.machineID != "" {
updateStatefile = true
statefile.MachineID = &res.machineID
}
if updateStatefile {
if err := state.WriteStateFile(stateFilepath, statefile); err != nil {
log.Printf("failed to update statefile")
}
}
default:
}
return nil
}

app.Flags = []cli.Flag{configFlag, skipFlag, keepFlag, sortLongest, skipUnchanged, truncates, truncateLength, colorFlag, lightBg, timeFormat, ignoreInterrupts, messageFieldsFlag, timeFieldsFlag, levelFieldsFlag}
app.Action = func(c *cli.Context) error {
// flags overwrite config file
if c.IsSet(sortLongest.Name) {
cfg.SortLongest = ptr(c.BoolT(sortLongest.Name))
}
Expand Down Expand Up @@ -288,15 +318,17 @@ const apiURL = "https://api.humanlog.io"
type checkForUpdateReq struct {
arch string
os string
accountID string
machineID string
accountID int64
machineID int64
current *types.Version
}
type checkForUpdateRes struct {
pb *types.Version
sem semver.Version
url string
sha256 string
pb *types.Version
sem semver.Version
url string
sha256 string
accountID int64
machineID int64
}

func checkForUpdate(ctx context.Context, req *checkForUpdateReq) <-chan *checkForUpdateRes {
Expand Down Expand Up @@ -332,7 +364,11 @@ func checkForUpdate(ctx context.Context, req *checkForUpdateReq) <-chan *checkFo
} else if nexVersion.GT(semverVersion) {
log.Printf("next version is %q, you're running %q", nexVersion, semverVersion)
}
out <- &checkForUpdateRes{pb: nextVersion, sem: nexVersion}
out <- &checkForUpdateRes{
pb: nextVersion,
sem: nexVersion,
machineID: res.Msg.Machine.Id,
}
}()
return out
}
14 changes: 0 additions & 14 deletions cmd/humanlog/statefile.go

This file was deleted.

4 changes: 3 additions & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package humanlog

import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"os"
Expand All @@ -13,6 +14,7 @@ import (
)

func TestHarness(t *testing.T) {
ctx := context.Background()
root := "test/cases"
des, err := os.ReadDir(root)
if err != nil {
Expand Down Expand Up @@ -47,7 +49,7 @@ func TestHarness(t *testing.T) {
t.Fatalf("errs=%v", errs)
}
s := stdiosink.NewStdio(gotw, sinkOpts)
err = Scanner(bytes.NewReader(input), s, HandlerOptionsFrom(cfg))
err = Scanner(ctx, bytes.NewReader(input), s, HandlerOptionsFrom(cfg))
if err != nil {
t.Fatalf("scanning input: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ require (
github.com/bufbuild/connect-go v1.1.0
github.com/fatih/color v1.13.0
github.com/go-logfmt/logfmt v0.5.1
github.com/humanlog-io/api/go v0.0.0-20221107072741-9ae9a031ec70
github.com/humanlog-io/api/go v0.0.0-20221107080824-690520531f97
github.com/kr/logfmt v0.0.0-20210122060352-19f9bcb100e6
github.com/mattn/go-colorable v0.1.13
github.com/urfave/cli v1.22.10
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/bufbuild/connect-go v1.1.0 h1:AUgqqO2ePdOJSpPOep6BPYz5v2moW1Lb8sQh0EeRzQ8=
github.com/bufbuild/connect-go v1.1.0/go.mod h1:9iNvh/NOsfhNBUH5CtvXeVUskQO1xsrEviH7ZArwZ3I=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
Expand All @@ -15,8 +16,8 @@ github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KE
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/humanlog-io/api/go v0.0.0-20221107072741-9ae9a031ec70 h1:1+OjXPCi/YD77YsO9gyDuLqEFpZ4GJssOm2L3g5cnNY=
github.com/humanlog-io/api/go v0.0.0-20221107072741-9ae9a031ec70/go.mod h1:s70Li+2S6iuILyGnpQwxtIX9t5W8VdT67pbCz0Pi6/Y=
github.com/humanlog-io/api/go v0.0.0-20221107080824-690520531f97 h1:B2E/MKTEXp3zvxcrkGZcTJBx2LcgBwNJsl2Cb52ooKc=
github.com/humanlog-io/api/go v0.0.0-20221107080824-690520531f97/go.mod h1:s70Li+2S6iuILyGnpQwxtIX9t5W8VdT67pbCz0Pi6/Y=
github.com/kr/logfmt v0.0.0-20210122060352-19f9bcb100e6 h1:ZK1mH67KVyVW/zOLu0xLva+f6xJ8vt+LGrkQq5FJYLY=
github.com/kr/logfmt v0.0.0-20210122060352-19f9bcb100e6/go.mod h1:JIiJcj9TX57tEvCXjm6eaHd2ce4pZZf9wzYuThq45u8=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand All @@ -28,9 +29,9 @@ github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peK
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/urfave/cli v1.22.10 h1:p8Fspmz3iTctJstry1PYS3HVdllxnEzTEsgIgtxTrCk=
Expand Down
62 changes: 32 additions & 30 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ import (
)

var DefaultConfig = Config{
Version: 1,
Skip: ptr([]string{}),
Keep: ptr([]string{}),
TimeFields: ptr([]string{"time", "ts", "@timestamp", "timestamp"}),
MessageFields: ptr([]string{"message", "msg"}),
LevelFields: ptr([]string{"level", "lvl", "loglevel", "severity"}),
SortLongest: ptr(true),
SkipUnchanged: ptr(true),
Truncates: ptr(true),
LightBg: ptr(false),
ColorMode: ptr("auto"),
TruncateLength: ptr(15),
TimeFormat: ptr(time.Stamp),
Interrupt: ptr(false),
Palette: nil,
Version: 1,
Skip: ptr([]string{}),
Keep: ptr([]string{}),
TimeFields: ptr([]string{"time", "ts", "@timestamp", "timestamp"}),
MessageFields: ptr([]string{"message", "msg"}),
LevelFields: ptr([]string{"level", "lvl", "loglevel", "severity"}),
SortLongest: ptr(true),
SkipUnchanged: ptr(true),
Truncates: ptr(true),
LightBg: ptr(false),
ColorMode: ptr("auto"),
TruncateLength: ptr(15),
TimeFormat: ptr(time.Stamp),
Interrupt: ptr(false),
CheckForUpdates: ptr(true),
Palette: nil,
}

func GetDefaultConfigFilepath() (string, error) {
Expand Down Expand Up @@ -84,21 +85,22 @@ func ReadConfigFile(path string, dflt *Config) (*Config, error) {
}

type Config struct {
Version int `json:"version"`
Skip *[]string `json:"skip"`
Keep *[]string `json:"keep"`
TimeFields *[]string `json:"time-fields"`
MessageFields *[]string `json:"message-fields"`
LevelFields *[]string `json:"level-fields"`
SortLongest *bool `json:"sort-longest"`
SkipUnchanged *bool `json:"skip-unchanged"`
Truncates *bool `json:"truncates"`
LightBg *bool `json:"light-bg"`
ColorMode *string `json:"color-mode"`
TruncateLength *int `json:"truncate-length"`
TimeFormat *string `json:"time-format"`
Palette *TextPalette `json:"palette"`
Interrupt *bool `json:"interrupt"`
Version int `json:"version"`
Skip *[]string `json:"skip"`
Keep *[]string `json:"keep"`
TimeFields *[]string `json:"time-fields"`
MessageFields *[]string `json:"message-fields"`
LevelFields *[]string `json:"level-fields"`
SortLongest *bool `json:"sort-longest"`
SkipUnchanged *bool `json:"skip-unchanged"`
Truncates *bool `json:"truncates"`
LightBg *bool `json:"light-bg"`
ColorMode *string `json:"color-mode"`
TruncateLength *int `json:"truncate-length"`
TimeFormat *string `json:"time-format"`
Palette *TextPalette `json:"palette"`
Interrupt *bool `json:"interrupt"`
CheckForUpdates *bool `json:"check_updates"`
}

func (cfg Config) populateEmpty(other *Config) *Config {
Expand Down
Loading

0 comments on commit 64c6d86

Please sign in to comment.