Skip to content

Commit

Permalink
simplify refidtool logging
Browse files Browse the repository at this point in the history
remove zerolog dependency, just wrap some very basic logging
  • Loading branch information
dropwhile committed Jan 17, 2025
1 parent 7c56813 commit cda8d29
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 74 deletions.
29 changes: 0 additions & 29 deletions cmd/refidtool/debug.go

This file was deleted.

5 changes: 2 additions & 3 deletions cmd/refidtool/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"github.com/dropwhile/refid/v2"
"github.com/rs/zerolog/log"
)

type DecodeCmd struct {
Expand All @@ -18,12 +17,12 @@ func (cmd *DecodeCmd) Run() error {
case 26, 32, 22:
// ok
default:
log.Fatal().Msg("invalid refid argument length")
Fatal("invalid refid argument length")
}

xID, err := refid.Parse(cmd.RefID)
if err != nil {
log.Fatal().Err(err).Msg("error parsing refid")
Fatalf("error parsing refid: %s", err)
}

PrintRefID(xID)
Expand Down
69 changes: 69 additions & 0 deletions cmd/refidtool/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2023 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package main

import (
"fmt"
"log"
"log/slog"
"strconv"
"time"

"github.com/dropwhile/refid/v2"
)

var logLevel = new(slog.LevelVar)

func Debug(msg string) {
if logLevel.Level() >= slog.LevelDebug {
log.Print("DEBUG: " + msg)
}
}

func Debugf(msg string, v ...any) {
if logLevel.Level() >= slog.LevelDebug {
log.Printf(msg, v...)
}
}

func Info(msg string) {
if logLevel.Level() >= slog.LevelDebug {
log.Print(msg)
}
}

func Infof(msg string, v ...any) {
if logLevel.Level() >= slog.LevelDebug {
log.Printf(msg, v...)
}
}

func Fatal(msg string) {
log.Fatal(msg)
}

func Fatalf(msg string, v ...any) {
log.Fatalf(msg, v...)
}

func PrintBytes(b []byte) {
for i := 0; i < len(b); i++ {
fmt.Printf("%08s ", strconv.FormatInt(int64(b[i]), 2))
if i != 0 && (i+1)%4 == 0 {
fmt.Println()
}
}
}

func PrintRefID(xID refid.RefID) {
tx := xID.Time().UTC()
fmt.Printf("native enc: %s\n", xID.String())
fmt.Printf("hex enc: %s\n", xID.ToHexString())
fmt.Printf("base64 enc: %s\n", xID.ToBase64String())
fmt.Printf("tag value: %d\n", xID.Tag())
fmt.Printf("type: %s\n", xID.Type())
fmt.Printf("time(string): %s\n", tx.Format(time.RFC3339Nano))
fmt.Printf("time(millis): %d\n", tx.UnixMilli())
}
22 changes: 5 additions & 17 deletions cmd/refidtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@
package main

import (
"os"
"log/slog"
"runtime/debug"

"github.com/alecthomas/kong"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

var Version string

type verboseFlag bool

func (v verboseFlag) BeforeApply() error {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("debug logging enabled")
return nil
}

func GetVersion() string {
if len(Version) > 0 {
return Version
Expand All @@ -40,7 +30,7 @@ func GetVersion() string {

type CLI struct {
// global options
Verbose verboseFlag `name:"verbose" short:"v" help:"enable verbose logging"`
Verbose bool `name:"verbose" short:"v" help:"enable verbose logging"`
Version kong.VersionFlag `name:"version" short:"V" help:"Print version information and quit"`

// subcommands
Expand All @@ -49,11 +39,6 @@ type CLI struct {
}

func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
PartsExclude: []string{zerolog.TimestampFieldName},
})

cli := CLI{}
ctx := kong.Parse(&cli,
kong.Name("refidtool"),
Expand All @@ -63,6 +48,9 @@ func main() {
"version": GetVersion(),
},
)
if cli.Verbose {
logLevel.Set(slog.LevelDebug)
}
err := ctx.Run(&cli)
ctx.FatalIfErrorf(err)
}
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ go 1.21

require (
github.com/alecthomas/kong v1.6.1
github.com/rs/zerolog v1.33.0
gotest.tools/v3 v3.5.1
)

require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.29.0 // indirect
)
require github.com/google/go-cmp v0.6.0 // indirect
18 changes: 0 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,9 @@ github.com/alecthomas/kong v1.6.1 h1:/7bVimARU3uxPD0hbryPE8qWrS3Oz3kPQoxA/H2NKG8
github.com/alecthomas/kong v1.6.1/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=

0 comments on commit cda8d29

Please sign in to comment.