From aad9b31169afec2dba4a1e218876b8ba4be2968b Mon Sep 17 00:00:00 2001 From: "T.v.Dein" Date: Tue, 10 Jun 2025 16:12:54 +0200 Subject: [PATCH] update dependencies (#46) * update dependencies --- .github/workflows/ci.yaml | 2 +- .github/workflows/pushimage.yaml | 4 +- .golangci.bck.yaml | 6 ++ .golangci.yaml | 7 +++ app/db.go | 13 +++-- app/io.go | 3 +- cfg/config.go | 4 +- cmd/crud.go | 2 +- cmd/extra.go | 13 ++++- common/io.go | 3 +- go.mod | 12 ++-- go.sum | 14 +++++ output/list.go | 95 ++++++++++++++++++++++---------- output/single.go | 36 +++++++++--- rest/serve.go | 6 +- 15 files changed, 154 insertions(+), 66 deletions(-) create mode 100644 .golangci.bck.yaml create mode 100644 .golangci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 71cdae3..7116bbc 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -53,4 +53,4 @@ jobs: go-version: 1.23 - uses: actions/checkout@v4 - name: golangci-lint - uses: golangci/golangci-lint-action@v6 + uses: golangci/golangci-lint-action@v8 diff --git a/.github/workflows/pushimage.yaml b/.github/workflows/pushimage.yaml index 9384c5a..8a5fa92 100644 --- a/.github/workflows/pushimage.yaml +++ b/.github/workflows/pushimage.yaml @@ -22,13 +22,13 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push Docker image - uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4 + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 with: push: true tags: ghcr.io/tlinden/anydb:${{ github.ref_name}} - name: Build and push latest Docker image - uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4 + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 with: push: true tags: ghcr.io/tlinden/anydb:latest diff --git a/.golangci.bck.yaml b/.golangci.bck.yaml new file mode 100644 index 0000000..58aeea5 --- /dev/null +++ b/.golangci.bck.yaml @@ -0,0 +1,6 @@ +linters: + exclusions: + rules: + - linters: + - staticcheck + text: "QF1008:" diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..4cdd836 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,7 @@ +version: "2" +linters: + exclusions: + rules: + - linters: + - staticcheck + text: "QF1008:" diff --git a/app/db.go b/app/db.go index d7785f9..06720d6 100644 --- a/app/db.go +++ b/app/db.go @@ -20,6 +20,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "log/slog" "os" "path/filepath" @@ -115,8 +116,10 @@ func (db *DB) Open() error { return nil } -func (db *DB) Close() error { - return db.DB.Close() +func (db *DB) Close() { + if err := db.DB.Close(); err != nil { + log.Fatal(err) + } } func (db *DB) List(attr *DbAttr, fulltext bool) (DbEntries, error) { @@ -242,14 +245,14 @@ func (db *DB) Set(attr *DbAttr) error { return err } } - + if oldentry != nil { if len(oldentry.Tags) > 0 && len(entry.Tags) == 0 { // initialize update entry with tags from old entry entry.Tags = oldentry.Tags } } - + slog.Debug("+++ MARSHAL") // marshall our data pbentry, err := proto.Marshal(&entry) @@ -304,7 +307,7 @@ func (db *DB) Set(attr *DbAttr) error { // internal DB getter, assumes db.DB has already been // opened successfully. Do NOT call this w/o valid // DB handle! -func (db *DB)txGet(attr *DbAttr) (*DbEntry, error) { +func (db *DB) txGet(attr *DbAttr) (*DbEntry, error) { entry := DbEntry{} err := db.DB.View(func(tx *bolt.Tx) error { diff --git a/app/io.go b/app/io.go index 109e770..7754bf1 100644 --- a/app/io.go +++ b/app/io.go @@ -20,8 +20,7 @@ import "os" func cleanError(file string, err error) error { // remove given [backup] file and forward the given error - os.Remove(file) - return err + return os.Remove(file) } func fileExists(filename string) bool { diff --git a/cfg/config.go b/cfg/config.go index b335b99..285aa00 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -1,5 +1,5 @@ /* -Copyright © 2024 Thomas von Dein +Copyright © 2025 Thomas von Dein This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,7 +26,7 @@ import ( "github.com/tlinden/anydb/common" ) -var Version string = "v0.2.2" +var Version string = "v0.2.3" type BucketConfig struct { Encrypt bool diff --git a/cmd/crud.go b/cmd/crud.go index 5bef5dc..4e125c8 100644 --- a/cmd/crud.go +++ b/cmd/crud.go @@ -156,7 +156,7 @@ func Del(conf *cfg.Config) *cobra.Command { Long: `Delete key and value matching key`, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("No key specified") + return errors.New("no key specified") } // errors at this stage do not cause the usage to be shown diff --git a/cmd/extra.go b/cmd/extra.go index 343be55..a86112d 100644 --- a/cmd/extra.go +++ b/cmd/extra.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "io" + "log" "os" "os/exec" @@ -285,7 +286,11 @@ func editContent(editor string, content string) (string, error) { if err != nil { return "", fmt.Errorf("failed to create templ file: %w", err) } - defer os.Remove(tmp.Name()) + defer func() { + if err := os.Remove(tmp.Name()); err != nil { + log.Fatal(err) + } + }() // put the content into a tmp file _, err = tmp.WriteString(content) @@ -310,7 +315,11 @@ func editContent(editor string, content string) (string, error) { if err != nil { return "", fmt.Errorf("failed to open temp file: %w", err) } - defer modified.Close() + defer func() { + if err := modified.Close(); err != nil { + log.Fatal(err) + } + }() newcontent, err := io.ReadAll(modified) if err != nil { diff --git a/common/io.go b/common/io.go index 0cf69d2..47933fb 100644 --- a/common/io.go +++ b/common/io.go @@ -20,8 +20,7 @@ import "os" func CleanError(file string, err error) error { // remove given [backup] file and forward the given error - os.Remove(file) - return err + return os.Remove(file) } func FileExists(filename string) bool { diff --git a/go.mod b/go.mod index f051fe3..472f5a9 100644 --- a/go.mod +++ b/go.mod @@ -6,16 +6,16 @@ toolchain go1.24.1 require ( github.com/dustin/go-humanize v1.0.1 - github.com/gofiber/fiber/v2 v2.52.6 + github.com/gofiber/fiber/v2 v2.52.8 github.com/inconshreveable/mousetrap v1.1.0 - github.com/olekukonko/tablewriter v0.0.5 + github.com/olekukonko/tablewriter v1.0.7 github.com/pelletier/go-toml v1.9.5 github.com/rogpeppe/go-internal v1.14.1 github.com/spf13/cobra v1.9.1 github.com/tlinden/yadu v0.1.3 go.etcd.io/bbolt v1.4.0 - golang.org/x/crypto v0.37.0 - golang.org/x/term v0.31.0 + golang.org/x/crypto v0.38.0 + golang.org/x/term v0.32.0 google.golang.org/protobuf v1.36.6 ) @@ -27,12 +27,14 @@ require ( github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6 // indirect + github.com/olekukonko/ll v0.0.8 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/spf13/pflag v1.0.6 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.55.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect - golang.org/x/sys v0.32.0 // indirect + golang.org/x/sys v0.33.0 // indirect golang.org/x/tools v0.26.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0182aed..f22ae6e 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI= github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw= +github.com/gofiber/fiber/v2 v2.52.8 h1:xl4jJQ0BV5EJTA2aWiKw/VddRpHrKeZLF0QPUxqn0x4= +github.com/gofiber/fiber/v2 v2.52.8/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw= 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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -24,8 +26,14 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6 h1:r3FaAI0NZK3hSmtTDrBVREhKULp8oUeqLT5Eyl2mSPo= +github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y= +github.com/olekukonko/ll v0.0.8 h1:sbGZ1Fx4QxJXEqL/6IG8GEFnYojUSQ45dJVwN2FH2fc= +github.com/olekukonko/ll v0.0.8/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g= 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/olekukonko/tablewriter v1.0.7 h1:HCC2e3MM+2g72M81ZcJU11uciw6z/p82aEnm4/ySDGw= +github.com/olekukonko/tablewriter v1.0.7/go.mod h1:H428M+HzoUXC6JU2Abj9IT9ooRmdq9CxuDmKMtrOCMs= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -55,13 +63,19 @@ go.etcd.io/bbolt v1.4.0 h1:TU77id3TnN/zKr7CO/uk+fBCwF2jGcMuw2B/FMAzYIk= go.etcd.io/bbolt v1.4.0/go.mod h1:AsD+OCi/qPN1giOX1aiLAha3o1U8rAz65bvN4j0sRuk= golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE= golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= +golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= +golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o= golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw= +golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg= +golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ= golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= diff --git a/output/list.go b/output/list.go index a7659a7..0048930 100644 --- a/output/list.go +++ b/output/list.go @@ -1,5 +1,5 @@ /* -Copyright © 2024 Thomas von Dein +Copyright © 2025 Thomas von Dein This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -28,6 +28,8 @@ import ( "github.com/dustin/go-humanize" "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" "github.com/tlinden/anydb/app" "github.com/tlinden/anydb/cfg" ) @@ -71,7 +73,9 @@ func ListTemplate(writer io.Writer, conf *cfg.Config, entries app.DbEntries) err } if buf.Len() > 0 { - fmt.Fprintln(writer, buf.String()) + if _, err := fmt.Fprintln(writer, buf.String()); err != nil { + return fmt.Errorf("failed to write output: %w", err) + } } } @@ -80,13 +84,46 @@ func ListTemplate(writer io.Writer, conf *cfg.Config, entries app.DbEntries) err func ListTable(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error { tableString := &strings.Builder{} - table := tablewriter.NewWriter(tableString) + + styleTSV := tw.NewSymbolCustom("space").WithColumn("\t") + + table := tablewriter.NewTable(tableString, + tablewriter.WithRenderer( + renderer.NewBlueprint(tw.Rendition{ + Borders: tw.BorderNone, + Symbols: styleTSV, + Settings: tw.Settings{ + Separators: tw.Separators{BetweenRows: tw.Off, BetweenColumns: tw.On}, + Lines: tw.Lines{ShowFooterLine: tw.Off, ShowHeaderLine: tw.Off}, + }, + })), + tablewriter.WithConfig(tablewriter.Config{ + Header: tw.CellConfig{ + Formatting: tw.CellFormatting{ + AutoFormat: tw.Off, + }, + Padding: tw.CellPadding{ + Global: tw.Padding{Left: "", Right: ""}, + }, + }, + Row: tw.CellConfig{ + Formatting: tw.CellFormatting{ + AutoWrap: tw.WrapNone, + Alignment: tw.AlignLeft, + }, + Padding: tw.CellPadding{ + Global: tw.Padding{Left: "", Right: ""}, + }, + }, + }), + tablewriter.WithPadding(tw.PaddingNone), + ) if !conf.NoHeaders { if conf.Mode == "wide" { - table.SetHeader([]string{"KEY", "TAGS", "SIZE", "UPDATED", "VALUE"}) + table.Header([]string{"KEY", "TAGS", "SIZE", "UPDATED", "VALUE"}) } else { - table.SetHeader([]string{"KEY", "VALUE"}) + table.Header([]string{"KEY", "VALUE"}) } } @@ -94,44 +131,42 @@ func ListTable(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error if conf.Mode == "wide" { switch conf.NoHumanize { case true: - table.Append([]string{ - row.Key, - strings.Join(row.Tags, ","), - strconv.FormatUint(row.Size, 10), - row.Created.AsTime().Format("02.01.2006T03:04.05"), - row.Preview, - }) + if err := + table.Append([]string{ + row.Key, + strings.Join(row.Tags, ","), + strconv.FormatUint(row.Size, 10), + row.Created.AsTime().Format("02.01.2006T03:04.05"), + row.Preview, + }); err != nil { + return fmt.Errorf("failed to add data to table: %w", err) + } default: - table.Append([]string{ + if err := table.Append([]string{ row.Key, strings.Join(row.Tags, ","), humanize.Bytes(uint64(row.Size)), humanize.Time(row.Created.AsTime()), row.Preview, - }) + }); err != nil { + return fmt.Errorf("failed to add data to table: %w", err) + } } } else { - table.Append([]string{row.Key, row.Preview}) + if err := table.Append([]string{row.Key, row.Preview}); err != nil { + return fmt.Errorf("failed to add data to table: %w", err) + } } } - table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) - table.SetAutoWrapText(false) - table.SetAutoFormatHeaders(true) - table.SetAlignment(tablewriter.ALIGN_LEFT) - table.SetCenterSeparator("") - table.SetColumnSeparator("") - table.SetRowSeparator("") - table.SetHeaderLine(false) - table.SetBorder(false) - table.SetNoWhiteSpace(true) + if err := table.Render(); err != nil { + return fmt.Errorf("failed to render table: %w", err) + } - table.SetTablePadding("\t") // pad with tabs - - table.Render() - - fmt.Fprint(writer, tableString.String()) + if _, err := fmt.Fprint(writer, tableString.String()); err != nil { + return fmt.Errorf("failed to write output: %w", err) + } return nil } diff --git a/output/single.go b/output/single.go index 22c1c95..352ad9f 100644 --- a/output/single.go +++ b/output/single.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "io" + "log" "os" "reflect" @@ -43,7 +44,9 @@ func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEn if isatty { fmt.Println("binary data omitted") } else { - os.Stdout.WriteString(entry.Value) + if _, err := os.Stdout.WriteString(entry.Value); err != nil { + return err + } } } else { fmt.Print(string(entry.Value)) @@ -79,7 +82,11 @@ func WriteFile(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app. if err != nil { return fmt.Errorf("failed to open file %s for writing: %w", attr.File, err) } - defer fd.Close() + defer func() { + if err := fd.Close(); err != nil { + log.Fatal(err) + } + }() fileHandle = fd } @@ -102,34 +109,45 @@ func WriteFile(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app. } func Info(writer io.Writer, conf *cfg.Config, info *app.DbInfo) error { - fmt.Fprintf(writer, "Database: %s\n", info.Path) + if _, err := fmt.Fprintf(writer, "Database: %s\n", info.Path); err != nil { + return fmt.Errorf("failed to write output: %w", err) + } for _, bucket := range info.Buckets { if conf.NoHumanize { - fmt.Fprintf( + if _, err := fmt.Fprintf( writer, "%19s: %s\n%19s: %d\n%19s: %d\n%19s: %t\n", "Bucket", bucket.Name, "Size", bucket.Size, "Keys", bucket.Keys, - "Encrypted", conf.Encrypt) + "Encrypted", conf.Encrypt); err != nil { + return fmt.Errorf("failed to write output: %w", err) + } } else { - fmt.Fprintf( + if _, err := fmt.Fprintf( writer, "%19s: %s\n%19s: %s\n%19s: %d\n", "Bucket", bucket.Name, "Size", humanize.Bytes(uint64(bucket.Size)), - "Keys", bucket.Keys) + "Keys", bucket.Keys); err != nil { + return fmt.Errorf("failed to write output: %w", err) + } } if conf.Debug { val := reflect.ValueOf(&bucket.Stats).Elem() for i := 0; i < val.NumField(); i++ { - fmt.Fprintf(writer, "%19s: %v\n", val.Type().Field(i).Name, val.Field(i)) + if _, err := fmt.Fprintf(writer, "%19s: %v\n", val.Type().Field(i).Name, val.Field(i)); err != nil { + return fmt.Errorf("failed to write output: %w", err) + } } } - fmt.Fprintln(writer) + if _, err := fmt.Fprintln(writer); err != nil { + return fmt.Errorf("failed to write output: %w", err) + } } + return nil } diff --git a/rest/serve.go b/rest/serve.go index 1484ea7..837b08b 100644 --- a/rest/serve.go +++ b/rest/serve.go @@ -104,11 +104,7 @@ Wrapper to respond with proper json status, message and code, shall be prepared and called by the handlers directly. */ func JsonStatus(c *fiber.Ctx, code int, msg string) error { - success := true - - if code != fiber.StatusOK { - success = false - } + success := code == fiber.StatusOK return c.Status(code).JSON(Result{ Code: code,