Skip to content

Commit

Permalink
use kong for params
Browse files Browse the repository at this point in the history
  • Loading branch information
dskiff committed Apr 25, 2024
1 parent a3f61bc commit aca5410
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ drop ALL `capabilities` to your hearts content.
# Perform build, output to ./dist
# Provide credentials using docker config, GITHUB_TOKEN, etc
export TKO_TARGET_REPO="destination/repo"
tko build ./dist
tko build --target-repo="destination/repo" ./dist
```

## What?
Expand Down
25 changes: 25 additions & 0 deletions pkg/build/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"context"
"fmt"
"strings"

"github.com/google/go-containerregistry/pkg/authn"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -89,3 +90,27 @@ func mutateConfig(img v1.Image, layer BuildSpecInjectLayer) (v1.Image, error) {

return mutate.ConfigFile(img, imgCfg)
}

func ParsePlatform(str string) (Platform, error) {
parts := strings.Split(str, "/")
if len(parts) != 2 {
return Platform{}, fmt.Errorf("invalid platform string: %s", str)
}
return Platform{
OS: parts[0],
Arch: parts[1],
}, nil
}

func ParseTargetType(str string) (TargetType, error) {
switch str {
case "REMOTE":
return REMOTE, nil
case "LOCAL_DAEMON":
return LOCAL_DAEMON, nil
case "":
return REMOTE, nil
default:
return -1, fmt.Errorf("invalid target type: %s", str)
}
}
61 changes: 21 additions & 40 deletions pkg/cmd/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"log"
"os"

Expand All @@ -15,13 +14,27 @@ import (
)

type BuildCmd struct {
SourcePath string `arg:"" name:"path" help:"Path to artifacts to embed" type:"path"`
BaseRef string `short:"b" help:"Base image reference" env:"TKO_BASE_IMAGE" default:"cgr.dev/chainguard/static:latest"`

Platform string `short:"p" help:"Platform to build for" env:"TKO_PLATFORM" default:"linux/amd64"`

SourcePath string `arg:"" name:"path" help:"Path to artifacts to embed" type:"path" env:"TKO_SOURCE_PATH"`
DestinationPath string `short:"d" help:"Path to embed artifacts in" env:"TKO_DEST_PATH" default:"/tko-app"`
Entrypoint string `short:"e" help:"Entrypoint for the embedded artifacts" env:"TKO_ENTRYPOINT" default:"/tko-app/app"`

TargetRepo string `short:"t" help:"Target repository" env:"TKO_TARGET_REPO" required:"true"`
TargetType string `short:"T" help:"Target type" env:"TKO_TARGET_TYPE" default:"REMOTE" enum:"REMOTE,LOCAL_DAEMON"`

Verbose bool `short:"v" help:"Enable verbose output"`
}

func (b *BuildCmd) Run(cliCtx *CliCtx) error {
targetType, err := parseTargetType(os.Getenv("TKO_TARGET_TYPE"))
targetType, err := build.ParseTargetType(b.TargetType)
if err != nil {
return err
}

platform, err := build.ParsePlatform(b.Platform)
if err != nil {
return err
}
Expand All @@ -33,38 +46,19 @@ func (b *BuildCmd) Run(cliCtx *CliCtx) error {
)

cfg := build.BuildSpec{
BaseRef: os.Getenv("TKO_BASE_IMAGE"),
BaseRef: b.BaseRef,
InjectLayer: build.BuildSpecInjectLayer{
Platform: build.Platform{
OS: "linux",
Arch: "amd64",
},
Platform: platform,
SourcePath: b.SourcePath,
DestinationPath: os.Getenv("TKO_DEST_PATH"),
Entrypoint: os.Getenv("TKO_ENTRYPOINT"),
DestinationPath: b.DestinationPath,
Entrypoint: b.Entrypoint,
},
Target: build.BuildSpecTarget{
Repo: os.Getenv("TKO_TARGET_REPO"),
Repo: b.TargetRepo,
Type: targetType,
},
}

if cfg.BaseRef == "" {
cfg.BaseRef = "cgr.dev/chainguard/static:latest"
}

if cfg.Target.Repo == "" {
return fmt.Errorf("target repo must be set")
}

if cfg.InjectLayer.DestinationPath == "" {
cfg.InjectLayer.DestinationPath = "/tko-app"
}

if cfg.InjectLayer.Entrypoint == "" {
cfg.InjectLayer.Entrypoint = "/tko-app/app"
}

out, err := yaml.Marshal(cfg)
if err != nil {
return err
Expand All @@ -88,16 +82,3 @@ func (b *BuildCmd) Run(cliCtx *CliCtx) error {
TempPath: os.Getenv("TKO_TEMP_PATH"),
}, cfg)
}

func parseTargetType(str string) (build.TargetType, error) {
switch str {
case "REMOTE":
return build.REMOTE, nil
case "LOCAL_DAEMON":
return build.LOCAL_DAEMON, nil
case "":
return build.REMOTE, nil
default:
return -1, fmt.Errorf("invalid target type: %s", str)
}
}

0 comments on commit aca5410

Please sign in to comment.