Skip to content

Commit

Permalink
use kong
Browse files Browse the repository at this point in the history
  • Loading branch information
dskiff committed Apr 25, 2024
1 parent 2f9fb0d commit 753a2a6
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 82 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/alecthomas/kong v0.9.0
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down
91 changes: 9 additions & 82 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"os/signal"

"github.com/dskiff/tko/pkg/build"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/authn/github"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/dskiff/tko/pkg/cmd"

"github.com/alecthomas/kong"
"github.com/joho/godotenv"
)

Expand All @@ -28,89 +26,18 @@ func main() {
godotenv.Load(".env.local")
godotenv.Load(".env")

TKO_TARGET_REPO := os.Getenv("TKO_TARGET_REPO")
TKO_BASE_IMAGE := os.Getenv("TKO_BASE_IMAGE")
TKO_DEST_PATH := os.Getenv("TKO_DEST_PATH")
TKO_ENTRYPOINT := os.Getenv("TKO_ENTRYPOINT")
TKO_TEMP_PATH := os.Getenv("TKO_TEMP_PATH")
TKO_TARGET_TYPE := os.Getenv("TKO_TARGET_TYPE")
TKO_LOG_LEVEL := os.Getenv("TKO_LOG_LEVEL")

if TKO_BASE_IMAGE == "" {
TKO_BASE_IMAGE = "cgr.dev/chainguard/static:latest"
}
if TKO_DEST_PATH == "" {
TKO_DEST_PATH = "/tko-app"
}
if TKO_ENTRYPOINT == "" {
TKO_ENTRYPOINT = "/tko-app/app"
}
if TKO_LOG_LEVEL == "" {
TKO_LOG_LEVEL = "info"
}

var targetType build.TargetType
switch TKO_TARGET_TYPE {
case "REMOTE":
targetType = build.REMOTE
case "LOCAL_DAEMON":
targetType = build.LOCAL_DAEMON
case "":
targetType = build.REMOTE
default:
log.Fatalf("Invalid TKO_TARGET_TYPE: %s", TKO_TARGET_TYPE)
}

// TODO: Better command line parsing
if len(os.Args) < 2 {
log.Fatalln("source path is required. Usage: tko <source-path>")
}
srcPath := os.Args[1]
if srcPath == "" {
log.Fatalln("source path is required. Usage: tko <source-path>")
}

keychain := authn.NewMultiKeychain(
authn.DefaultKeychain,
google.Keychain,
github.Keychain,
)

logs.Warn.SetOutput(os.Stderr)
logs.Progress.SetOutput(os.Stderr)
if TKO_LOG_LEVEL == "debug" {
logs.Debug.SetOutput(os.Stderr)
}

log.Println("TKO_TARGET_REPO:", TKO_TARGET_REPO)
log.Println("TKO_BASE_IMAGE:", TKO_BASE_IMAGE)
log.Println("TKO_DEST_PATH:", TKO_DEST_PATH)
log.Println("TKO_ENTRYPOINT:", TKO_ENTRYPOINT)
log.Println("TKO_TEMP_PATH:", TKO_TEMP_PATH)
log.Println("TKO_TARGET_TYPE:", TKO_TARGET_TYPE)
log.Println("TKO_LOG_LEVEL:", TKO_LOG_LEVEL)
log.Println("Source path:", srcPath)
log.Println("")

exitCleanWatcher := build.NewExitCleanupWatcher()
defer exitCleanWatcher.Close()

err := build.Run(ctx, build.RunConfig{
SrcPath: srcPath,
DstPath: TKO_DEST_PATH,
Entrypoint: TKO_ENTRYPOINT,

BaseImage: TKO_BASE_IMAGE,
TargetRepo: TKO_TARGET_REPO,
TargetType: targetType,
RemoteKeychain: keychain,
cliContext := cmd.CliCtx{
Ctx: &ctx,
Version: version,
ExitCleanWatcher: exitCleanWatcher,
}

PlatformOs: "linux",
PlatformArch: "amd64",
args := kong.Parse(&cmd.CLI)

TempPath: TKO_TEMP_PATH,
ExitCleanupWatcher: exitCleanWatcher,
})
err := args.Run(&cliContext)
if err != nil {
log.Println(err)
exitCleanWatcher.Close()
Expand Down
113 changes: 113 additions & 0 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cmd

import (
"context"
"fmt"
"log"
"os"

"github.com/dskiff/tko/pkg/build"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/authn/github"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/v1/google"
)

type CliCtx struct {
Ctx *context.Context
Version string
ExitCleanWatcher *build.ExitCleanupWatcher
}

type VersionCmd struct{}

func (v *VersionCmd) Run(cliCtx *CliCtx) error {
fmt.Println(cliCtx.Version)
return nil
}

type BuildCmd struct {
SourcePath string `arg:"" name:"path" help:"Path to artifacts to embed" type:"path"`
}

func (b *BuildCmd) Run(cliCtx *CliCtx) error {
TKO_TARGET_REPO := os.Getenv("TKO_TARGET_REPO")
TKO_BASE_IMAGE := os.Getenv("TKO_BASE_IMAGE")
TKO_DEST_PATH := os.Getenv("TKO_DEST_PATH")
TKO_ENTRYPOINT := os.Getenv("TKO_ENTRYPOINT")
TKO_TEMP_PATH := os.Getenv("TKO_TEMP_PATH")
TKO_TARGET_TYPE := os.Getenv("TKO_TARGET_TYPE")
TKO_LOG_LEVEL := os.Getenv("TKO_LOG_LEVEL")

if TKO_BASE_IMAGE == "" {
TKO_BASE_IMAGE = "cgr.dev/chainguard/static:latest"
}
if TKO_DEST_PATH == "" {
TKO_DEST_PATH = "/tko-app"
}
if TKO_ENTRYPOINT == "" {
TKO_ENTRYPOINT = "/tko-app/app"
}
if TKO_LOG_LEVEL == "" {
TKO_LOG_LEVEL = "info"
}

var targetType build.TargetType
switch TKO_TARGET_TYPE {
case "REMOTE":
targetType = build.REMOTE
case "LOCAL_DAEMON":
targetType = build.LOCAL_DAEMON
case "":
targetType = build.REMOTE
default:
log.Fatalf("Invalid TKO_TARGET_TYPE: %s", TKO_TARGET_TYPE)
}

srcPath := b.SourcePath

keychain := authn.NewMultiKeychain(
authn.DefaultKeychain,
google.Keychain,
github.Keychain,
)

logs.Warn.SetOutput(os.Stderr)
logs.Progress.SetOutput(os.Stderr)
if TKO_LOG_LEVEL == "debug" {
logs.Debug.SetOutput(os.Stderr)
}

log.Println("TKO_TARGET_REPO:", TKO_TARGET_REPO)
log.Println("TKO_BASE_IMAGE:", TKO_BASE_IMAGE)
log.Println("TKO_DEST_PATH:", TKO_DEST_PATH)
log.Println("TKO_ENTRYPOINT:", TKO_ENTRYPOINT)
log.Println("TKO_TEMP_PATH:", TKO_TEMP_PATH)
log.Println("TKO_TARGET_TYPE:", TKO_TARGET_TYPE)
log.Println("TKO_LOG_LEVEL:", TKO_LOG_LEVEL)
log.Println("Source path:", srcPath)
log.Println("")

return build.Run(*cliCtx.Ctx, build.RunConfig{
SrcPath: srcPath,
DstPath: TKO_DEST_PATH,
Entrypoint: TKO_ENTRYPOINT,

BaseImage: TKO_BASE_IMAGE,
TargetRepo: TKO_TARGET_REPO,
TargetType: targetType,
RemoteKeychain: keychain,

PlatformOs: "linux",
PlatformArch: "amd64",

TempPath: TKO_TEMP_PATH,
ExitCleanupWatcher: cliCtx.ExitCleanWatcher,
})
}

var CLI struct {
Version VersionCmd `cmd:"" help:"Show version."`

Build BuildCmd `cmd:"" help:"Build and publish a container image."`
}

0 comments on commit 753a2a6

Please sign in to comment.