Skip to content

Commit

Permalink
prepare version update lookup by channel
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 3a79679 commit 941c0a3
Show file tree
Hide file tree
Showing 462 changed files with 83,945 additions and 9,197 deletions.
97 changes: 91 additions & 6 deletions cmd/humanlog/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package main

import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"

"github.com/aybabtme/rgbterm"
"github.com/blang/semver"
"github.com/bufbuild/connect-go"
cliupdatepb "github.com/humanlog-io/api/go/svc/cliupdate/v1"
"github.com/humanlog-io/api/go/svc/cliupdate/v1/cliupdatev1connect"
types "github.com/humanlog-io/api/go/types/v1"
"github.com/humanlogio/humanlog"
"github.com/humanlogio/humanlog/internal/pkg/config"
"github.com/humanlogio/humanlog/internal/pkg/sink/stdiosink"
"github.com/mattn/go-colorable"
"github.com/urfave/cli"
"humanlog.io/humanlog"
types "humanlog.io/humanlog/api/go/types/v1"
)

var Version = &types.Version{}
var (
Version = &types.Version{Minor: 6}
semverVersion = func() semver.Version {
v, err := Version.AsSemver()
if err != nil {
panic(err)
}
return v
}()
)

func fatalf(c *cli.Context, format string, args ...interface{}) {
log.Printf(format, args...)
Expand Down Expand Up @@ -130,11 +144,33 @@ func newApp() *cli.App {
app.Author = "Antoine Grondin"
app.Email = "antoinegrondin@gmail.com"
app.Name = "humanlog"
app.Version = Version
app.Version = semverVersion.String()
app.Usage = "reads structured logs from stdin, makes them pretty on stdout!"

app.Flags = []cli.Flag{configFlag, skipFlag, keepFlag, sortLongest, skipUnchanged, truncates, truncateLength, colorFlag, lightBg, timeFormat, ignoreInterrupts, messageFieldsFlag, timeFieldsFlag, levelFieldsFlag}
var (
ctx context.Context
cancel context.CancelFunc
updateRes <-chan *checkForUpdateRes
)
app.Before = func(c *cli.Context) error {
ctx, cancel = signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
updateRes = checkForUpdate(ctx, &checkForUpdateReq{})
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()
Expand Down Expand Up @@ -222,9 +258,10 @@ func newApp() *cli.App {
handlerOpts := humanlog.HandlerOptionsFrom(*cfg)

log.Print("reading stdin...")
if err := humanlog.Scanner(os.Stdin, sink, handlerOpts); err != nil {
if err := humanlog.Scanner(ctx, os.Stdin, sink, handlerOpts); err != nil {
log.Fatalf("scanning caught an error: %v", err)
}

return nil
}
return app
Expand All @@ -233,3 +270,51 @@ func newApp() *cli.App {
func ptr[T any](v T) *T {
return &v
}

const apiURL = "https://api.humanlog.io"

type checkForUpdateReq struct {
arch string
os string
accountID string
machineID string
current *types.Version
}
type checkForUpdateRes struct {
pb *types.Version
sem semver.Version
url string
sha256 string
}

func checkForUpdate(ctx context.Context) <-chan *checkForUpdateRes {
out := make(chan *checkForUpdateRes, 1)
go func() {
defer close(out)
client := &http.Client{}
updateClient := cliupdatev1connect.NewUpdateServiceClient(client, apiURL)
res, err := updateClient.GetNextUpdate(ctx, &connect.Request[cliupdatepb.GetNextUpdateRequest]{
Msg: &cliupdatepb.GetNextUpdateRequest{CurrentVersion: Version},
})
if err != nil {
log.Printf("looking for update failed: %v", err)
return
}
nextVersion := res.Msg.NextVersion

nexVersion, err := nextVersion.AsSemver()
if err != nil {
log.Printf("looking for update returned bogus version: %v", err)
return
}
if nexVersion.EQ(semverVersion) {
log.Printf("running latest version: %v", semverVersion)
} else if nexVersion.LT(semverVersion) {
log.Printf("you appear to be running an unreleased version")
} else if nexVersion.GT(semverVersion) {
log.Printf("next version is %q, you're running %q", nexVersion, semverVersion)
}
out <- &checkForUpdateRes{pb: nextVersion, sem: nexVersion}
}()
return out
}
21 changes: 14 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ go 1.19

require (
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59
github.com/fatih/color v1.7.1-0.20180516100307-2d684516a886
github.com/go-logfmt/logfmt v0.4.0
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515
github.com/mattn/go-colorable v0.1.0
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2
github.com/blang/semver v3.5.1+incompatible
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/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/mattn/go-isatty v0.0.4 // indirect
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // 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
golang.org/x/sys v0.1.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
62 changes: 48 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/fatih/color v1.7.1-0.20180516100307-2d684516a886 h1:NAFoy+QgUpERgK3y1xiVh5HcOvSeZHpXTTo5qnvnuK4=
github.com/fatih/color v1.7.1-0.20180516100307-2d684516a886/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/mattn/go-colorable v0.1.0 h1:v2XXALHHh6zHfYTJ+cSkwtyffnaOyR1MXaA91mTrb8o=
github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2 h1:xAkHCttGHKXIr10OSiFzNt0XOJyHMdng0ylSynT8sMo=
github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 h1:2B5p2L5IfGiD7+b9BOoRMC6DgObAVZV+Fsp050NqXik=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
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/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=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
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/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=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
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/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=
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/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-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
15 changes: 14 additions & 1 deletion scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package humanlog
import (
"bufio"
"bytes"
"context"
"io"
"time"

Expand All @@ -13,7 +14,7 @@ import (
// Scanner reads JSON-structured lines from src and prettify them onto dst. If
// the lines aren't JSON-structured, it will simply write them out with no
// prettification.
func Scanner(src io.Reader, sink sink.Sink, opts *HandlerOptions) error {
func Scanner(ctx context.Context, src io.Reader, sink sink.Sink, opts *HandlerOptions) error {
in := bufio.NewScanner(src)
in.Split(bufio.ScanLines)

Expand All @@ -27,6 +28,7 @@ func Scanner(src io.Reader, sink sink.Sink, opts *HandlerOptions) error {
ev.Structured = data

for in.Scan() {

line++
lineData := in.Bytes()

Expand Down Expand Up @@ -59,6 +61,17 @@ func Scanner(src io.Reader, sink sink.Sink, opts *HandlerOptions) error {
if err := sink.Receive(ev); err != nil {
return err
}
select {
case <-ctx.Done():
return nil
default:
}
}

select {
case <-ctx.Done():
return nil
default:
}

switch err := in.Err(); err {
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/blang/semver/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/blang/semver/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 941c0a3

Please sign in to comment.