From adf3511b1ecf18eb3c22988f95e79b461809b9d0 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Fri, 8 Jan 2021 17:16:00 +0100 Subject: [PATCH] fix lint errors, enable CI Signed-off-by: Andrea Luzzardi --- .github/workflows/ci.yml | 34 +++++++++++++++++++++++++++ .golangci.yml | 34 +++++++++++++++++++++++++++ Makefile | 20 ++++++++++++++-- cmd/dagger-frontend/main.go | 14 ----------- dagger/client.go | 7 +++--- dagger/component.go | 5 +--- dagger/component_test.go | 2 +- dagger/op.go | 7 +----- dagger/op_test.go | 4 ++-- dagger/script_test.go | 2 +- dagger/spec.cue | 46 ++++++++++++++++++------------------- dagger/spec_test.go | 1 - dagger/ui/ui.go | 4 ++-- dagger/utils.go | 42 +-------------------------------- dagger/value.go | 5 ++-- 15 files changed, 123 insertions(+), 104 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .golangci.yml delete mode 100644 cmd/dagger-frontend/main.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..65fbce80 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,34 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.14.2 + uses: actions/setup-go@v1 + with: + go-version: 1.14.2 + id: go + + - name: Insatall Dependencies + run: | + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sudo sh -s -- -b /usr/local/bin v1.23.8 + curl -L https://github.com/cuelang/cue/releases/download/v0.3.0-alpha6/cue_0.3.0-alpha6_Linux_x86_64.tar.gz | sudo tar zxf - -C /usr/local/bin + + - name: Check out + uses: actions/checkout@v2 + + - name: Build + run: | + make + + - name: Lint + run: | + make lint diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..796dff50 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,34 @@ +linters: + disable-all: true + timeout: 30m + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - golint + - goprintffuncname + - gosec + - gosimple + - govet + - ineffassign + - misspell + - nakedret + - prealloc + - rowserrcheck + - scopelint + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace diff --git a/Makefile b/Makefile index 2f859fed..b406d3bb 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,19 @@ +.PHONY: all +all: dagger + +.PHONY: generate +generate: + go generate ./dagger + .PHONY: dagger -dagger: - go generate ./dagger && go build -o ./cmd/dagger/ ./cmd/dagger/ +dagger: generate + go build -o ./cmd/dagger/ ./cmd/dagger/ + +.PHONY: cuefmt +cuefmt: + (cd ./dagger && cue fmt -s ./... && cue trim -s ./...) + +.PHONY: lint +lint: + golangci-lint run + @test -z "$$(git status -s . | grep -e "^ M" | grep .cue | cut -d ' ' -f3 | tee /dev/stderr)" \ No newline at end of file diff --git a/cmd/dagger-frontend/main.go b/cmd/dagger-frontend/main.go deleted file mode 100644 index 9894b00d..00000000 --- a/cmd/dagger-frontend/main.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "dagger.cloud/go/dagger" - "github.com/moby/buildkit/frontend/gateway/grpcclient" - "github.com/moby/buildkit/util/appcontext" -) - -func main() { - r := &dagger.Runtime{} - if err := grpcclient.RunFromEnvironment(appcontext.Context(), r.BKFrontend); err != nil { - panic(err) - } -} diff --git a/dagger/client.go b/dagger/client.go index eaf30e7e..da23e426 100644 --- a/dagger/client.go +++ b/dagger/client.go @@ -17,7 +17,7 @@ import ( // buildkit bk "github.com/moby/buildkit/client" - _ "github.com/moby/buildkit/client/connhelper/dockercontainer" + _ "github.com/moby/buildkit/client/connhelper/dockercontainer" // import the container connection driver // docker output "github.com/containerd/console" @@ -188,7 +188,7 @@ func (c *Client) buildfn(ctx context.Context, ch chan *bk.SolveStatus, w io.Writ } // Read tar export stream from buildkit Build(), and extract cue output -func (c *Client) outputfn(ctx context.Context, r io.Reader, out *Value) func() error { +func (c *Client) outputfn(_ context.Context, r io.Reader, out *Value) func() error { return func() error { defer debugf("outputfn complete") tr := tar.NewReader(r) @@ -230,7 +230,7 @@ type Node struct { } func (n Node) ComponentPath() cue.Path { - var parts []cue.Selector + parts := []cue.Selector{} for _, sel := range n.Path.Selectors() { if strings.HasPrefix(sel.String(), "#") { break @@ -326,7 +326,6 @@ func (c *Client) printfn(ctx context.Context, ch, ch2 chan *bk.SolveStatus) func // see proto 67 } } - return nil } } diff --git a/dagger/component.go b/dagger/component.go index 3e81df0b..d8dea639 100644 --- a/dagger/component.go +++ b/dagger/component.go @@ -14,10 +14,7 @@ func (c *Component) Value() *Value { func (c *Component) Exists() bool { // Does #dagger exist? - if c.Config().Err() != nil { - return false - } - return true + return c.Config().Err() == nil } // Return the contents of the "#dagger" annotation. diff --git a/dagger/component_test.go b/dagger/component_test.go index 6c6fdc66..874ea752 100644 --- a/dagger/component_test.go +++ b/dagger/component_test.go @@ -32,7 +32,7 @@ func TestValidateSimpleComponent(t *testing.T) { } n := 0 if err := s.Walk(func(op *Op) error { - n += 1 + n++ return nil }); err != nil { t.Fatal(err) diff --git a/dagger/op.go b/dagger/op.go index 3633ff81..fcef9f18 100644 --- a/dagger/op.go +++ b/dagger/op.go @@ -138,7 +138,7 @@ func (op *Op) Local(ctx context.Context, fs FS, out Fillable) (FS, error) { } func (op *Op) Exec(ctx context.Context, fs FS, out Fillable) (FS, error) { - var opts []llb.RunOption + opts := []llb.RunOption{} var cmd struct { Args []string Env map[string]string @@ -151,11 +151,6 @@ func (op *Op) Exec(ctx context.Context, fs FS, out Fillable) (FS, error) { opts = append(opts, llb.WithCustomName(op.v.Path().String())) // args opts = append(opts, llb.Args(cmd.Args)) - // dir - dir := cmd.Dir - if dir == "" { - dir = "/" - } // env for k, v := range cmd.Env { opts = append(opts, llb.AddEnv(k, v)) diff --git a/dagger/op_test.go b/dagger/op_test.go index 0702d06a..053db9c6 100644 --- a/dagger/op_test.go +++ b/dagger/op_test.go @@ -17,7 +17,7 @@ func TestLocalMatch(t *testing.T) { } n := 0 err = op.Walk(func(op *Op) error { - n += 1 + n++ return nil }) if err != nil { @@ -44,7 +44,7 @@ func TestCopyMatch(t *testing.T) { } n := 0 err = op.Walk(func(op *Op) error { - n += 1 + n++ return nil }) if err != nil { diff --git a/dagger/script_test.go b/dagger/script_test.go index 1591c329..71898fcc 100644 --- a/dagger/script_test.go +++ b/dagger/script_test.go @@ -18,7 +18,7 @@ func TestLocalScript(t *testing.T) { } n := 0 err = s.Walk(func(op *Op) error { - n += 1 + n++ return nil }) if err != nil { diff --git a/dagger/spec.cue b/dagger/spec.cue index 7cd9de4f..c8a58387 100644 --- a/dagger/spec.cue +++ b/dagger/spec.cue @@ -10,14 +10,14 @@ package dagger // The DAG architecture has many benefits: // - Because DAGs are made of nodes executing in parallel, they are easy to scale. // - Because all inputs and outputs are snapshotted and content-addressed, DAGs -// can easily be made repeatable, can be cached aggressively, and can be replayed -// at will. +// can easily be made repeatable, can be cached aggressively, and can be replayed +// at will. // - Because nodes are executed by the same container engine as docker-build, DAGs -// can be developed using any language or technology capable of running in a docker. -// Dockerfiles and docker images are natively supported for maximum compatibility. +// can be developed using any language or technology capable of running in a docker. +// Dockerfiles and docker images are natively supported for maximum compatibility. // // - Because DAGs are programmed declaratively with a powerful configuration language, -// they are much easier to test, debug and refactor than traditional programming languages. +// they are much easier to test, debug and refactor than traditional programming languages. // // To execute a DAG, the dagger runtime JIT-compiles it to a low-level format called // llb, and executes it with buildkit. @@ -42,11 +42,9 @@ package dagger // The contents of a #dagger annotation #ComponentConfig: { // script to compute the value - compute?: #Script + compute?: #Script } - - // Any component can be referenced as a directory, since // every dagger script outputs a filesystem state (aka a directory) #Dir: #Component @@ -61,19 +59,19 @@ package dagger do: "export" // Source path in the container source: string - format: "json"|"yaml"|*"string"|"number"|"boolean" + format: "json" | "yaml" | *"string" | "number" | "boolean" } #Local: { - do: "local" - dir: string + do: "local" + dir: string include?: [...string] | *[] } // FIXME: bring back load (more efficient than copy) #Load: { - do: "load" + do: "load" from: #Component | #Script } @@ -82,40 +80,40 @@ package dagger args: [...string] env?: [string]: string always?: true | *false - dir: string | *"/" + dir: string | *"/" mount?: [string]: #MountTmp | #MountCache | #MountComponent | #MountScript } -#MountTmp: "tmpfs" +#MountTmp: "tmpfs" #MountCache: "cache" #MountComponent: { input: #Component - path: string | *"/" + path: string | *"/" } #MountScript: { input: #Script - path: string | *"/" + path: string | *"/" } #FetchContainer: { - do: "fetch-container" + do: "fetch-container" ref: string } #FetchGit: { - do: "fetch-git" + do: "fetch-git" remote: string - ref: string + ref: string } #Copy: { - do: "copy" - from: #Script | #Component - src?: string | *"/" + do: "copy" + from: #Script | #Component + src?: string | *"/" dest?: string | *"/" } #TestScript: #Script & [ - { do: "fetch-container", ref: "alpine:latest" }, - { do: "exec", args: ["echo", "hello", "world" ] } + {do: "fetch-container", ref: "alpine:latest"}, + {do: "exec", args: ["echo", "hello", "world"]}, ] diff --git a/dagger/spec_test.go b/dagger/spec_test.go index 597d4025..07e528d2 100644 --- a/dagger/spec_test.go +++ b/dagger/spec_test.go @@ -81,7 +81,6 @@ func testMatch(t *testing.T, src interface{}, def string) { t.Errorf("false positive: %s: %q", cmpDef, src) } } - return } func compile(t *testing.T, cc *Compiler, src interface{}) *Value { diff --git a/dagger/ui/ui.go b/dagger/ui/ui.go index bff3e8f5..159afe75 100644 --- a/dagger/ui/ui.go +++ b/dagger/ui/ui.go @@ -8,7 +8,7 @@ import ( func Fatalf(msg string, args ...interface{}) { if !strings.HasSuffix(msg, "\n") { - msg = msg + "\n" + msg += "\n" } fmt.Fprintf(os.Stderr, msg, args...) os.Exit(1) @@ -20,7 +20,7 @@ func Fatal(msg interface{}) { func Info(msg string, args ...interface{}) { if !strings.HasSuffix(msg, "\n") { - msg = msg + "\n" + msg += "\n" } fmt.Fprintf(os.Stderr, "[info] "+msg, args...) } diff --git a/dagger/utils.go b/dagger/utils.go index 0172b1c5..678c3f97 100644 --- a/dagger/utils.go +++ b/dagger/utils.go @@ -2,53 +2,27 @@ package dagger import ( "crypto/rand" - "encoding/json" "fmt" "os" "strings" "cuelang.org/go/cue" cueerrors "cuelang.org/go/cue/errors" - cueformat "cuelang.org/go/cue/format" - "github.com/moby/buildkit/client/llb" - "github.com/moby/buildkit/client/llb/imagemetaresolver" ) -func cuePrint(v cue.Value) (string, error) { - b, err := cueformat.Node(v.Syntax()) - if err != nil { - return "", err - } - return string(b), nil -} - func cueErr(err error) error { return fmt.Errorf("%s", cueerrors.Details(err, &cueerrors.Config{})) } -func debugJSON(v interface{}) { - if os.Getenv("DEBUG") != "" { - e := json.NewEncoder(os.Stderr) - e.SetIndent("", " ") - e.Encode(v) - } -} - func debugf(msg string, args ...interface{}) { if !strings.HasSuffix(msg, "\n") { - msg = msg + "\n" + msg += "\n" } if os.Getenv("DEBUG") != "" { fmt.Fprintf(os.Stderr, msg, args...) } } -func debug(msg string) { - if os.Getenv("DEBUG") != "" { - fmt.Fprintln(os.Stderr, msg) - } -} - func randomID(size int) (string, error) { b := make([]byte, size) _, err := rand.Read(b) @@ -58,14 +32,6 @@ func randomID(size int) (string, error) { return fmt.Sprintf("%x", b), nil } -// LLB Helper to pull a Docker image + all its metadata -func llbDockerImage(ref string) llb.State { - return llb.Image( - ref, - llb.WithMetaResolver(imagemetaresolver.Default()), - ) -} - func cueStringsToCuePath(parts ...string) cue.Path { selectors := make([]cue.Selector, 0, len(parts)) for _, part := range parts { @@ -82,9 +48,3 @@ func cuePathToStrings(p cue.Path) []string { } return out } - -// Validate a cue path, and return a canonical version -func cueCleanPath(p string) (string, error) { - cp := cue.ParsePath(p) - return cp.String(), cp.Err() -} diff --git a/dagger/value.go b/dagger/value.go index a421bac2..f82aad42 100644 --- a/dagger/value.go +++ b/dagger/value.go @@ -35,7 +35,8 @@ func (v *Value) Unlock() { func (v *Value) Lookup(path ...string) *Value { v.Lock() defer v.Unlock() - return v.Wrap(v.Unwrap().Lookup(path...)) + + return v.Wrap(v.Unwrap().LookupPath(cueStringsToCuePath(path...))) } func (v *Value) LookupPath(p cue.Path) *Value { @@ -176,7 +177,7 @@ func (v *Value) RangeList(fn func(int, *Value) error) error { if err := fn(i, v.Wrap(it.Value())); err != nil { return err } - i += 1 + i++ } return nil }