From a872bad97d6f4edff5fc7725a9f8f311483328b9 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 29 Oct 2022 23:33:46 +0200 Subject: [PATCH] with more generic way, but still wonky --- dagger_go_template.yaml | 4 +--- example/main.go | 2 +- pkg/cli/cli.go | 23 ++++++++++++----------- pkg/tasks/build.go | 1 - 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/dagger_go_template.yaml b/dagger_go_template.yaml index cd869f2..f1149e0 100644 --- a/dagger_go_template.yaml +++ b/dagger_go_template.yaml @@ -1,5 +1,3 @@ -{{ $imageTag := "harbor.front.kjuulh.io/library/${DRONE_REPO_NAME}" }} - type: docker kind: pipeline name: "drone-dagger-test" @@ -18,7 +16,7 @@ steps: - mkdir -p tmp/dagger-go - git clone "https://git.front.kjuulh.io/kjuulh/dagger-go.git" tmp/dagger-go - docker build -t dagger-go-builder:${DRONE_COMMIT} -f tmp/dagger-go/Dockerfile . - - 'docker run -v "$PWD/:/src/" -v /var/run/docker.sock:/var/run/docker.sock dagger-go-builder:${DRONE_COMMIT} dagger-go build --image-tag "{{ or .input.imageTag $imageTag }}"' + - 'docker run -v "$PWD/:/src/" -v /var/run/docker.sock:/var/run/docker.sock dagger-go-builder:${DRONE_COMMIT} dagger-go build' services: - name: docker diff --git a/example/main.go b/example/main.go index 6d583a5..fbb6085 100644 --- a/example/main.go +++ b/example/main.go @@ -23,5 +23,5 @@ func run(ctx context.Context) error { } defer builder.CleanUp() - return tasks.Build(builder, "some-image") + return tasks.Build(builder, "some-image", "example/main.go") } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index c81b020..725a879 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -1,19 +1,17 @@ package cli import ( + "errors" + "fmt" "log" + "os" "git.front.kjuulh.io/kjuulh/dagger-go/internal" "git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks" "github.com/spf13/cobra" ) -func Build() *cobra.Command { - var ( - imageTag string - mainGoPath string - ) - +func Build(mainGoPath string, imageTag string) *cobra.Command { cmd := &cobra.Command{ Use: "build", RunE: func(cmd *cobra.Command, args []string) error { @@ -21,6 +19,14 @@ func Build() *cobra.Command { return err } + if imageTag == "" { + repoName := os.Getenv("DRONE_REPO_NAME") + if repoName == "" { + return errors.New("could not find DRONE_REPO_NAME") + } + imageTag = fmt.Sprintf("harbor.front.kjuulh.io/library/%s", repoName) + } + ctx := cmd.Context() log.Printf("Building image: %s\n", imageTag) @@ -35,10 +41,5 @@ func Build() *cobra.Command { }, } - cmd.PersistentFlags().StringVar(&imageTag, "image-tag", "", "the url for which to tag the docker image, defaults to private url, with repo as image name") - cmd.MarkPersistentFlagRequired("image-tag") - - cmd.PersistentFlags().StringVar(&mainGoPath, "main-path", "main.go", "main.go path") - return cmd } diff --git a/pkg/tasks/build.go b/pkg/tasks/build.go index e2cec79..d81ba34 100644 --- a/pkg/tasks/build.go +++ b/pkg/tasks/build.go @@ -3,7 +3,6 @@ package tasks import ( "context" "log" - "os" "dagger.io/dagger" "git.front.kjuulh.io/kjuulh/byg"