Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cobra #427

Closed
wants to merge 15 commits into from
Closed

cobra #427

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
dotenv
dotenv_if_exists .env.local

if ! has nix_direnv_version || ! nix_direnv_version 3.0.4; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.4/direnvrc" "sha256-DzlYZ33mWF/Gs8DDeyjr8mnVmQGx7ASYqA5WlxwvBG4="
fi

watch_file flake.nix
watch_file nix/devshell.nix

use flake
38 changes: 38 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
linters:
enable:
- errname
- exhaustive
- gci
- gochecknoglobals
- gochecknoinits
- goconst
- godot
- gofumpt
- goheader
- goimports
- gosec
- importas
- ireturn
- lll
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- prealloc
- predeclared
- revive
- rowserrcheck
- stylecheck
- tagliatelle
- tenv
- testpackage
- unconvert
- unparam
- wastedassign
- whitespace
- wsl
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ builds:
- env:
- CGO_ENABLED=0
ldflags:
- -s -w -X git.numtide.com/numtide/treefmt/build.Version=v{{.Version}}
- -s -w -X github.com/numtide/treefmt/build.Version=v{{.Version}}
goos:
- linux
- darwin
Expand Down
21 changes: 0 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +0,0 @@
MIT License

Copyright (c) Numtide & Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 3 additions & 1 deletion build/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package build

var (
Name = "treefmt"
//nolint:gochecknoglobals
Name = "treefmt"
//nolint:gochecknoglobals
Version = "v0.0.1+dev"
)
48 changes: 26 additions & 22 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ package cache

import (
"context"
"crypto/sha1"
"crypto/sha1" //nolint:gosec
"encoding/hex"
"fmt"
"os"
"runtime"
"time"

"git.numtide.com/numtide/treefmt/stats"

"git.numtide.com/numtide/treefmt/format"
"git.numtide.com/numtide/treefmt/walk"

"github.com/charmbracelet/log"

"github.com/adrg/xdg"
"github.com/charmbracelet/log"
"github.com/numtide/treefmt/format"
"github.com/numtide/treefmt/stats"
"github.com/numtide/treefmt/walk"
"github.com/vmihailenco/msgpack/v5"
bolt "go.etcd.io/bbolt"
)
Expand All @@ -33,10 +30,10 @@ type Entry struct {
}

var (
db *bolt.DB
logger *log.Logger
db *bolt.DB //nolint:gochecknoglobals
logger *log.Logger //nolint:gochecknoglobals

ReadBatchSize = 1024 * runtime.NumCPU()
ReadBatchSize = 1024 * runtime.NumCPU() //nolint:gochecknoglobals
)

// Open creates an instance of bolt.DB for a given treeRoot path.
Expand All @@ -48,11 +45,12 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)
logger = log.WithPrefix("cache")

// determine a unique and consistent db name for the tree root
h := sha1.New()
h := sha1.New() //nolint:gosec
h.Write([]byte(treeRoot))
digest := h.Sum(nil)

name := hex.EncodeToString(digest)

path, err := xdg.CacheFile(fmt.Sprintf("treefmt/eval-cache/%v.db", name))
if err != nil {
return fmt.Errorf("could not resolve local path for the cache: %w", err)
Expand All @@ -64,7 +62,7 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)
return fmt.Errorf("failed to open cache at %v: %w", path, err)
}

err = db.Update(func(tx *bolt.Tx) error {
return db.Update(func(tx *bolt.Tx) error {
// create bucket for tracking paths
pathsBucket, err := tx.CreateBucketIfNotExists([]byte(pathsBucket))
if err != nil {
Expand All @@ -79,7 +77,6 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)

// check for any newly configured or modified formatters
for name, formatter := range formatters {

stat, err := os.Lstat(formatter.Executable())
if err != nil {
return fmt.Errorf("failed to stat formatter executable %v: %w", formatter.Executable(), err)
Expand Down Expand Up @@ -130,6 +127,7 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)
// indicate a clean is required
clean = true
}

return nil
}); err != nil {
return fmt.Errorf("failed to check cache for removed formatters: %w", err)
Expand All @@ -147,15 +145,14 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)

return nil
})

return
}

// Close closes any open instance of the cache.
func Close() error {
if db == nil {
return nil
}

return db.Close()
}

Expand All @@ -167,10 +164,11 @@ func getEntry(bucket *bolt.Bucket, path string) (*Entry, error) {
if err := msgpack.Unmarshal(b, &cached); err != nil {
return nil, fmt.Errorf("failed to unmarshal cache info for path '%v': %w", path, err)
}

return &cached, nil
} else {
return nil, nil
}
//nolint:nilnil
return nil, nil
}

// putEntry is a helper for writing cache entries into bolt.
Expand All @@ -183,20 +181,23 @@ func putEntry(bucket *bolt.Bucket, path string, entry *Entry) error {
if err = bucket.Put([]byte(path), bytes); err != nil {
return fmt.Errorf("failed to put cache path %v: %w", path, err)
}

return nil
}

// ChangeSet is used to walk a filesystem, starting at root, and outputting any new or changed paths using pathsCh.
// It determines if a path is new or has changed by comparing against cache entries.
func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.File) error {
func ChangeSet(ctx context.Context, statz *stats.Stats, walker walk.Walker, filesCh chan<- *walk.File) error {
start := time.Now()

defer func() {
logger.Debugf("finished generating change set in %v", time.Since(start))
}()

var tx *bolt.Tx

var bucket *bolt.Bucket

var processed int

defer func() {
Expand Down Expand Up @@ -226,6 +227,7 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil
if err != nil {
return fmt.Errorf("failed to open a new cache read tx: %w", err)
}

bucket = tx.Bucket([]byte(pathsBucket))
}

Expand All @@ -236,13 +238,14 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil

changedOrNew := cached == nil || !(cached.Modified == file.Info.ModTime() && cached.Size == file.Info.Size())

stats.Add(stats.Traversed, 1)
statz.Add(stats.Traversed, 1)

if !changedOrNew {
// no change
return nil
}

stats.Add(stats.Emitted, 1)
statz.Add(stats.Emitted, 1)

// pass on the path
select {
Expand All @@ -253,10 +256,11 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil
}

// close the current tx if we have reached the batch size
processed += 1
processed++
if processed == ReadBatchSize {
err = tx.Rollback()
tx = nil

return err
}

Expand Down
61 changes: 0 additions & 61 deletions cli/cli.go

This file was deleted.

Loading