Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
golangci-lint: fix
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed May 27, 2023
1 parent 9c14527 commit e32adcd
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 107 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ For a description of each command, run 'timecraft help <command>'.`

func help(ctx context.Context, args []string) error {
flagSet := newFlagSet("timecraft help", helpUsage)
flagSet.Parse(args)
parseFlags(flagSet, args)

var cmd string
var msg string
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func profile(ctx context.Context, args []string) error {
stringVar(flagSet, &cpuProfile, "cpuprofile")
stringVar(flagSet, &memProfile, "memprofile")
stringVar(flagSet, &registryPath, "r", "registry")
flagSet.Parse(args)
parseFlags(flagSet, args)

if time.Time(startTime).IsZero() {
startTime = timestamp(time.Unix(0, 0))
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func replay(ctx context.Context, args []string) error {
flagSet := newFlagSet("timecraft replay", replayUsage)
stringVar(flagSet, &registryPath, "r", "registry")
boolVar(flagSet, &trace, "T", "trace")
flagSet.Parse(args)
parseFlags(flagSet, args)

args = flagSet.Args()
if len(args) != 1 {
Expand Down
9 changes: 8 additions & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func init() {
// Root is the timecraft entrypoint.
func Root(ctx context.Context, args ...string) int {
flagSet := newFlagSet("timecraft", helpUsage)
flagSet.Parse(args)
parseFlags(flagSet, args)

if args = flagSet.Args(); len(args) == 0 {
fmt.Println(rootUsage)
Expand Down Expand Up @@ -175,6 +175,13 @@ func newFlagSet(cmd, usage string) *flag.FlagSet {
return flagSet
}

func parseFlags(f *flag.FlagSet, args []string) {
// The flag set is consutrcted with ExitOnError, it should never error.
if err := f.Parse(args); err != nil {
panic(err)
}
}

func customVar(f *flag.FlagSet, dst flag.Value, name string, alias ...string) {
f.Var(dst, name, "")
for _, name := range alias {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func run(ctx context.Context, args []string) error {
boolVar(flagSet, &record, "R", "record")
intVar(flagSet, &batchSize, "record-batch-size")
stringVar(flagSet, &compression, "record-compression")
flagSet.Parse(args)
parseFlags(flagSet, args)

envs = append(os.Environ(), envs...)
args = flagSet.Args()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Options:

func version(ctx context.Context, args []string) error {
flagSet := newFlagSet("timecraft version", versionUsage)
flagSet.Parse(args)
parseFlags(flagSet, args)
fmt.Printf("timecraft %s\n", currentVersion())
return nil
}
Expand Down
8 changes: 5 additions & 3 deletions internal/object/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ func testObjectStoreListWhileCreate(t *testing.T, ctx context.Context, store obj
assert.OK(t, store.CreateObject(ctx, "test-3", r))
}()

io.WriteString(w, "H")
_, err := io.WriteString(w, "H")
assert.OK(t, err)

beforeCreateObject := readValues(t, store.ListObjects(ctx, "."))
clearCreatedAt(beforeCreateObject)
Expand All @@ -183,8 +184,9 @@ func testObjectStoreListWhileCreate(t *testing.T, ctx context.Context, store obj
{Name: "test-2", Size: 1},
})

io.WriteString(w, "ello World!")
w.Close()
_, err = io.WriteString(w, "ello World!")
assert.OK(t, err)
assert.OK(t, w.Close())
<-done

afterCreateObject := readValues(t, store.ListObjects(ctx, "."))
Expand Down
18 changes: 0 additions & 18 deletions internal/timemachine/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"fmt"
"io"

flatbuffers "github.com/google/flatbuffers/go"
"github.com/stealthrocket/timecraft/format"
"github.com/stealthrocket/timecraft/format/types"
)

type Hash = format.Hash
Expand All @@ -23,19 +21,3 @@ func UUIDv4(r io.Reader) Hash {
s := fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
return Hash{Algorithm: "uuidv4", Digest: s}
}

func makeHash(h *types.Hash) Hash {
return Hash{
Algorithm: string(h.Algorithm()),
Digest: string(h.Digest()),
}
}

func prependHash(b *flatbuffers.Builder, h Hash) flatbuffers.UOffsetT {
algorithm := b.CreateSharedString(h.Algorithm)
digest := b.CreateString(h.Digest)
types.HashStart(b)
types.HashAddAlgorithm(b, algorithm)
types.HashAddDigest(b, digest)
return types.HashEnd(b)
}
20 changes: 3 additions & 17 deletions internal/timemachine/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,12 @@ func errorCreateObject(hash format.Hash, value format.Resource, err error) error
return fmt.Errorf("create object: %s: %s: %w", hash, value.ContentType(), err)
}

func errorDeleteObject(hash format.Hash, err error) error {
return fmt.Errorf("delete object: %s: %w", hash, err)
}

func errorLookupObject(hash format.Hash, value format.Resource, err error) error {
return fmt.Errorf("lookup object: %s: %s: %w", hash, value.ContentType(), err)
}

func errorLookupDescriptor(hash format.Hash, err error) error {
return fmt.Errorf("lookup descriptor: %s: %w", hash, err)
func errorLookupDescriptor(hash format.Hash, value format.Resource, err error) error {
return fmt.Errorf("lookup descriptor: %s: %s: %w", hash, value.ContentType(), err)
}

func (reg *Registry) createObject(ctx context.Context, value format.ResourceMarshaler) (*format.Descriptor, error) {
Expand All @@ -111,7 +107,7 @@ func (reg *Registry) createObject(ctx context.Context, value format.ResourceMars
return descriptor, nil
}
if !errors.Is(err, object.ErrNotExist) {
return nil, errorCreateObject(hash, value, err)
return nil, errorLookupDescriptor(hash, value, err)
}

descriptor = &format.Descriptor{
Expand All @@ -133,16 +129,6 @@ func (reg *Registry) createObject(ctx context.Context, value format.ResourceMars
return descriptor, nil
}

func (reg *Registry) deleteObject(ctx context.Context, hash format.Hash) error {
if err := reg.objects.DeleteObject(ctx, reg.objectKey(hash)); err != nil {
return errorDeleteObject(hash, err)
}
if err := reg.objects.DeleteObject(ctx, reg.descriptorKey(hash)); err != nil {
return errorDeleteObject(hash, err)
}
return nil
}

func (reg *Registry) lookupDescriptor(ctx context.Context, key string) (*format.Descriptor, error) {
r, err := reg.objects.ReadObject(ctx, key)
if err != nil {
Expand Down
Loading

0 comments on commit e32adcd

Please sign in to comment.