Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3a79286
feat: add commands
ogzhanolguncu Jul 14, 2025
30531e7
feat: allow configuring name,desc and version
ogzhanolguncu Jul 14, 2025
53e33f9
feat: pass env to cli
ogzhanolguncu Jul 14, 2025
cc55b1d
feat: match the initial impl
ogzhanolguncu Jul 14, 2025
82d3393
feat: add new progress aniamtion
ogzhanolguncu Jul 14, 2025
d05bce4
feat: add tracker step for each phase
ogzhanolguncu Jul 14, 2025
9e1b02a
refactor: improve animations and errors
ogzhanolguncu Jul 15, 2025
aa7cde2
feat: use proper orchestrafor managing steps and trackers
ogzhanolguncu Jul 15, 2025
454828e
refactor: rename build to run
ogzhanolguncu Jul 15, 2025
f058067
refactor: remove UI logic from api
ogzhanolguncu Jul 15, 2025
7e1a3b4
chore: remove redundant commands
ogzhanolguncu Jul 15, 2025
238c658
refactor: remove ui bloat
ogzhanolguncu Jul 15, 2025
e8ab3dc
feat: add colors for make it distinguishable
ogzhanolguncu Jul 15, 2025
2cd7fa4
Merge branch 'main' into ENG-1903
ogzhanolguncu Jul 15, 2025
9152767
fix: steps
ogzhanolguncu Jul 15, 2025
52fe00d
fix: code rabbit issues
ogzhanolguncu Jul 15, 2025
f80dd82
feat: add proper flag parsing logic
ogzhanolguncu Jul 16, 2025
496a994
refactor: show help if required args are missing
ogzhanolguncu Jul 16, 2025
b8f964f
feat: add missing commands
ogzhanolguncu Jul 16, 2025
1f5b476
fix: code rabbit comments
ogzhanolguncu Jul 16, 2025
74f407a
refactor: fix redundancy
ogzhanolguncu Jul 16, 2025
2a8f0ed
Merge branch 'main' into ENG-1903
ogzhanolguncu Jul 16, 2025
20068a1
refactor: improve sub spinner
ogzhanolguncu Jul 16, 2025
02d2597
refactor: move duplicated spinner loop
ogzhanolguncu Jul 16, 2025
b1fd291
refactor: remove some commands for later
ogzhanolguncu Jul 17, 2025
8c960a1
Merge branch 'main' into ENG-1903
ogzhanolguncu Jul 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions go/cmd/cli/app/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package app

import (
"context"
"fmt"
"os"

"github.com/unkeyed/unkey/go/cmd/cli/commands"
"github.com/unkeyed/unkey/go/cmd/cli/commands/deploy"
)

// CLI represents our command line interface
type CLI struct {
Comment thread
ogzhanolguncu marked this conversation as resolved.
Outdated
args []string
name string
usage string
version string
env map[string]string
}

// New creates a new CLI instance
func New(args []string, name, usage, version string) *CLI {
env := map[string]string{
"UNKEY_WORKSPACE_ID": os.Getenv("UNKEY_WORKSPACE_ID"),
"UNKEY_PROJECT_ID": os.Getenv("UNKEY_PROJECT_ID"),
}

return &CLI{
args: args,
name: name,
usage: usage,
version: version,
env: env,
}
}

// Run executes the CLI
func (c *CLI) Run(ctx context.Context) error {
if len(c.args) < 2 {
c.PrintUsage()
return nil
}

command := c.args[1]
switch command {
case "init":
return commands.Init(c.args[2:], c.env)
case "deploy":
return deploy.Deploy(ctx, c.args[2:], c.env)
case "version":
return commands.Version(ctx, c.args[2:], c.env)
case "help", "-h", "--help":
return c.runHelp()
case "-v", "--version":
fmt.Println(c.version)
return nil
default:
c.PrintUsage()
return fmt.Errorf("unknown command: %s", command)
}
}

// runHelp handles the help command
func (c *CLI) runHelp() error {
if len(c.args) < 3 {
c.PrintUsage()
return nil
}

helpTopic := c.args[2]
switch helpTopic {
case "init":
commands.PrintInitHelp()
case "deploy":
deploy.PrintDeployHelp()
case "version":
commands.PrintVersionHelp()
default:
fmt.Printf("No help available for '%s'\n", helpTopic)
c.PrintUsage()
}
return nil
}
Comment thread
ogzhanolguncu marked this conversation as resolved.
Outdated

// PrintUsage prints general usage information
func (c *CLI) PrintUsage() {
fmt.Printf("%s - %s\n", c.name, c.usage)
fmt.Println("")
fmt.Println("USAGE:")
fmt.Printf(" %s <command> [flags]\n", c.name)
fmt.Println("")
fmt.Println("VERSION:")
fmt.Printf(" %s\n", c.version)
fmt.Println("")
fmt.Println("COMMANDS:")
fmt.Println(" init Initialize configuration file")
fmt.Println(" deploy Deploy a new version")
fmt.Println(" version Manage API versions")
fmt.Println(" help Show help information")
fmt.Println("")
fmt.Println("FLAGS:")
fmt.Println(" -h, --help Show help")
fmt.Println(" -v, --version Show version")
fmt.Println("")
fmt.Println("ENVIRONMENT VARIABLES:")
fmt.Println(" UNKEY_WORKSPACE_ID Workspace ID (can be overridden by --workspace-id)")
fmt.Println(" UNKEY_PROJECT_ID Project ID (can be overridden by --project-id)")
fmt.Println("")
fmt.Println("EXAMPLES:")
fmt.Printf(" %s help\n", c.name)
fmt.Printf(" %s help deploy\n", c.name)
fmt.Printf(" %s init\n", c.name)
fmt.Printf(" %s deploy --workspace-id=ws_123 --project-id=proj_456\n", c.name)
fmt.Printf(" UNKEY_WORKSPACE_ID=ws_123 %s deploy\n", c.name)
fmt.Printf(" UNKEY_WORKSPACE_ID=ws_123 UNKEY_PROJECT_ID=proj_456 %s deploy\n", c.name)
fmt.Println("")
fmt.Printf("For detailed help on a command, use '%s help <command>'\n", c.name)
}
84 changes: 84 additions & 0 deletions go/cmd/cli/commands/deploy/build_docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package deploy

import (
"context"
"fmt"
"os/exec"
"strings"
"time"

"github.com/unkeyed/unkey/go/pkg/git"
)

func generateImageTag(opts *DeployOptions, gitInfo git.Info) string {
if gitInfo.ShortSHA != "" {
return fmt.Sprintf("%s-%s", opts.Branch, gitInfo.ShortSHA)
}
return fmt.Sprintf("%s-%d", opts.Branch, time.Now().Unix())
}

func buildImage(ctx context.Context, opts *DeployOptions, dockerImage string) error {
buildArgs := []string{"build"}
if opts.Dockerfile != "Dockerfile" {
buildArgs = append(buildArgs, "-f", opts.Dockerfile)
}
buildArgs = append(buildArgs,
"-t", dockerImage,
"--build-arg", fmt.Sprintf("VERSION=%s", opts.Commit),
opts.Context,
)

cmd := exec.CommandContext(ctx, "docker", buildArgs...)

// Stream output directly instead of complex pipe handling
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Docker build failed:\n%s\n", string(output))
return ErrDockerBuildFailed
}

return nil
}
Comment thread
ogzhanolguncu marked this conversation as resolved.

func pushImage(ctx context.Context, dockerImage, registry string) error {
cmd := exec.CommandContext(ctx, "docker", "push", dockerImage)
output, err := cmd.CombinedOutput()
if err != nil {
detailedMsg := classifyPushError(string(output), registry)
return fmt.Errorf("%s", detailedMsg)
}
fmt.Printf("%s\n", string(output))
return nil
}
Comment thread
ogzhanolguncu marked this conversation as resolved.

func classifyPushError(output, registry string) string {
output = strings.TrimSpace(output)
registryHost := getRegistryHost(registry)

switch {
case strings.Contains(output, "denied"):
return fmt.Sprintf("registry access denied. Try: docker login %s", registryHost)

case strings.Contains(output, "not found") || strings.Contains(output, "404"):
return "registry not found. Create repository or use --registry=your-registry/your-app"

case strings.Contains(output, "unauthorized"):
return fmt.Sprintf("authentication required. Run: docker login %s", registryHost)

default:
return output
}
}

func getRegistryHost(registry string) string {
parts := strings.Split(registry, "/")
if len(parts) > 0 {
return parts[0]
}
return "docker.io"
}
Comment thread
ogzhanolguncu marked this conversation as resolved.

func isDockerAvailable() bool {
cmd := exec.Command("docker", "--version")
return cmd.Run() == nil
}
Loading
Loading