-
Notifications
You must be signed in to change notification settings - Fork 633
feat: Unkey Deploy CLI #3564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat: Unkey Deploy CLI #3564
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3a79286
feat: add commands
ogzhanolguncu 30531e7
feat: allow configuring name,desc and version
ogzhanolguncu 53e33f9
feat: pass env to cli
ogzhanolguncu cc55b1d
feat: match the initial impl
ogzhanolguncu 82d3393
feat: add new progress aniamtion
ogzhanolguncu d05bce4
feat: add tracker step for each phase
ogzhanolguncu 9e1b02a
refactor: improve animations and errors
ogzhanolguncu aa7cde2
feat: use proper orchestrafor managing steps and trackers
ogzhanolguncu 454828e
refactor: rename build to run
ogzhanolguncu f058067
refactor: remove UI logic from api
ogzhanolguncu 7e1a3b4
chore: remove redundant commands
ogzhanolguncu 238c658
refactor: remove ui bloat
ogzhanolguncu e8ab3dc
feat: add colors for make it distinguishable
ogzhanolguncu 2cd7fa4
Merge branch 'main' into ENG-1903
ogzhanolguncu 9152767
fix: steps
ogzhanolguncu 52fe00d
fix: code rabbit issues
ogzhanolguncu f80dd82
feat: add proper flag parsing logic
ogzhanolguncu 496a994
refactor: show help if required args are missing
ogzhanolguncu b8f964f
feat: add missing commands
ogzhanolguncu 1f5b476
fix: code rabbit comments
ogzhanolguncu 74f407a
refactor: fix redundancy
ogzhanolguncu 2a8f0ed
Merge branch 'main' into ENG-1903
ogzhanolguncu 20068a1
refactor: improve sub spinner
ogzhanolguncu 02d2597
refactor: move duplicated spinner loop
ogzhanolguncu b1fd291
refactor: remove some commands for later
ogzhanolguncu 8c960a1
Merge branch 'main' into ENG-1903
ogzhanolguncu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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 | ||
| } | ||
|
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) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
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 | ||
| } | ||
|
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" | ||
| } | ||
|
ogzhanolguncu marked this conversation as resolved.
|
||
|
|
||
| func isDockerAvailable() bool { | ||
| cmd := exec.Command("docker", "--version") | ||
| return cmd.Run() == nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.