with actual cli

This commit is contained in:
Kasper Juul Hermansen 2022-10-30 18:33:59 +01:00
parent 2b641df577
commit 18f7e1fc27
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
10 changed files with 107 additions and 66 deletions

13
example/cli/main.go Normal file
View File

@ -0,0 +1,13 @@
package main
import (
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/cli"
)
func main() {
if err := cli.NewCli().Execute(); err != nil {
log.Fatal(err)
}
}

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"log" "log"
"git.front.kjuulh.io/kjuulh/dagger-go/internal" "git.front.kjuulh.io/kjuulh/dagger-go/pkg/builder"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines" "git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines"
) )
@ -16,7 +16,7 @@ func main() {
} }
} }
func run(ctx context.Context) error { func run(ctx context.Context) error {
builder, err := internal.New(ctx) builder, err := builder.New(ctx)
if err != nil { if err != nil {
return err return err
} }

13
main.go Normal file
View File

@ -0,0 +1,13 @@
package main
import (
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/cli"
)
func main() {
if err := cli.NewCli().Execute(); err != nil {
log.Fatal(err)
}
}

View File

@ -1,4 +1,4 @@
package internal package builder
import ( import (
"context" "context"

15
pkg/cli/build.go Normal file
View File

@ -0,0 +1,15 @@
package cli
import (
"github.com/spf13/cobra"
)
func Build() *cobra.Command {
cmd := &cobra.Command{
Use: "build",
}
cmd.AddCommand(BuildGolangBin())
return cmd
}

View File

@ -0,0 +1,45 @@
package cli
import (
"errors"
"fmt"
"os"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/builder"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines"
"github.com/spf13/cobra"
)
func BuildGolangBin() *cobra.Command {
cmd := &cobra.Command{
Use: "golangbin",
RunE: func(cmd *cobra.Command, args []string) error {
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()
builder, err := builder.New(ctx)
if err != nil {
return err
}
defer builder.CleanUp()
return pipelines.
New(builder).
WithGolangBin(&pipelines.GolangBinOpts{
DockerImageOpt: &pipelines.DockerImageOpt{
ImageName: imageTag,
},
BuildPath: "main.go",
BinName: "main",
}).
Execute(ctx)
},
}
return cmd
}

View File

@ -1,55 +0,0 @@
package cli
import (
"errors"
"fmt"
"log"
"os"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines"
"github.com/spf13/cobra"
)
func Build(mainGoPath string, imageTag string) *cobra.Command {
cmd := &cobra.Command{
Use: "build",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ParseFlags(args); err != nil {
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)
builder, err := internal.New(ctx)
if err != nil {
return err
}
defer builder.CleanUp()
return pipelines.
New(builder).
WithGolangBin(&pipelines.GolangBinOpts{
DockerImageOpt: &pipelines.DockerImageOpt{
ImageName: "golang-bin",
},
BuildPath: "example/golang-bin/main.go",
BinName: "golang-bin",
}).
Execute(ctx)
},
}
return cmd
}

13
pkg/cli/root.go Normal file
View File

@ -0,0 +1,13 @@
package cli
import "github.com/spf13/cobra"
func NewCli() *cobra.Command {
cmd := &cobra.Command{
Use: "dagger",
}
cmd.AddCommand(Build())
return cmd
}

View File

@ -1,13 +1,12 @@
package pipelines package pipelines
import ( import (
"context"
"log" "log"
"git.front.kjuulh.io/kjuulh/byg" "git.front.kjuulh.io/kjuulh/byg"
) )
func (p *Pipeline) WithDefault() error { func (p *Pipeline) WithDefault() *byg.Builder {
return byg. return byg.
New(). New().
Step( Step(
@ -17,7 +16,5 @@ func (p *Pipeline) WithDefault() error {
log.Println("Hello, world!") log.Println("Hello, world!")
return nil return nil
}, },
}). })
Execute(context.Background())
} }

View File

@ -4,16 +4,16 @@ import (
"context" "context"
"git.front.kjuulh.io/kjuulh/byg" "git.front.kjuulh.io/kjuulh/byg"
"git.front.kjuulh.io/kjuulh/dagger-go/internal" "git.front.kjuulh.io/kjuulh/dagger-go/pkg/builder"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
type Pipeline struct { type Pipeline struct {
builder *internal.Builder builder *builder.Builder
pipelines []*byg.Builder pipelines []*byg.Builder
} }
func New(builder *internal.Builder) *Pipeline { func New(builder *builder.Builder) *Pipeline {
return &Pipeline{builder: builder} return &Pipeline{builder: builder}
} }