Skip to content

Commit

Permalink
improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dskiff committed Apr 25, 2024
1 parent 753a2a6 commit 8037130
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 95 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ drop ALL `capabilities` to your hearts content.
# Provide credentials using docker config, GITHUB_TOKEN, etc
export TKO_TARGET_REPO="destination/repo"
tko ./dist
tko build ./dist
```

## What?
Expand Down Expand Up @@ -58,7 +58,7 @@ Unfortunately, ko is only for go. If you're using go, and by some weird SEO quir
- use: dskiff/tko-setup@latest
- name: Publish
run: tko "./out"
run: tko build "./out"
env:
TKO_BASE_IMAGE: debian:bookworm-slim@sha256:155280b00ee0133250f7159b567a07d7cd03b1645714c3a7458b2287b0ca83cb
GITHUB_TOKEN: ${{ github.token }}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/joho/godotenv v1.5.1
)

require gopkg.in/yaml.v3 v3.0.1 // indirect

require (
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ var commit = "none"
var date = "unknown"

func main() {
log.Printf("tko %s (%s) built on %s\n", version, commit, date)

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

Expand All @@ -30,8 +28,10 @@ func main() {
defer exitCleanWatcher.Close()

cliContext := cmd.CliCtx{
Ctx: &ctx,
Version: version,
Ctx: ctx,
TkoBuildVersion: version,
TkoBuildCommit: commit,
TkoBuildDate: date,
ExitCleanWatcher: exitCleanWatcher,
}

Expand Down
97 changes: 97 additions & 0 deletions pkg/cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"fmt"
"log"
"os"

"gopkg.in/yaml.v3"

"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 BuildCmd struct {
SourcePath string `arg:"" name:"path" help:"Path to artifacts to embed" type:"path"`

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

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

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

cfg := build.RunConfig{
SrcPath: b.SourcePath,
DstPath: os.Getenv("TKO_DEST_PATH"),
Entrypoint: os.Getenv("TKO_ENTRYPOINT"),

BaseImage: os.Getenv("TKO_BASE_IMAGE"),
TargetRepo: os.Getenv("TKO_TARGET_REPO"),
TargetType: targetType,
RemoteKeychain: keychain,

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

TempPath: os.Getenv("TKO_TEMP_PATH"),
ExitCleanupWatcher: cliCtx.ExitCleanWatcher,
}

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

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

if cfg.DstPath == "" {
cfg.DstPath = "/tko-app"
}

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

out, err := yaml.Marshal(cfg)
if err != nil {
return err
}

log.Printf("tko %s (%s) built on %s\n", cliCtx.TkoBuildVersion, cliCtx.TkoBuildCommit, cliCtx.TkoBuildDate)
log.Println("Build configuration:", "\n"+string(out))

// Enable go-containerregistry logging
logs.Warn.SetOutput(os.Stderr)
logs.Progress.SetOutput(os.Stderr)
if b.Verbose {
logs.Debug.SetOutput(os.Stderr)
}

return build.Run(cliCtx.Ctx, 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)
}
}
94 changes: 5 additions & 89 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,25 @@ 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
Ctx context.Context
TkoBuildVersion string
TkoBuildCommit string
TkoBuildDate string
ExitCleanWatcher *build.ExitCleanupWatcher
}

type VersionCmd struct{}

func (v *VersionCmd) Run(cliCtx *CliCtx) error {
fmt.Println(cliCtx.Version)
fmt.Println(cliCtx.TkoBuildVersion)
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."`

Expand Down

0 comments on commit 8037130

Please sign in to comment.