Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var (
)

func main() {
os.Exit(run())
}

func run() int {
factory := initFactory()

rootCmd, err := root.NewCmdRoot(factory, version, commit, date)
Expand All @@ -33,14 +37,15 @@ func main() {
os.Args = append([]string{os.Args[0], "deploy"}, os.Args[1:]...)
}

// log errors
if err := rootCmd.Execute(); err != nil {
// when some errors occur(such as args dis-match), the log may not be initialized
if factory.Log == nil {
factory.Log = log.NewInfoLevel()
}
factory.Log.Error(err)
return 1
}
return 0
}

// init factory, including config, auth, etc.
Expand Down
28 changes: 28 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"os"
"testing"
)

func TestRun_UnknownSubcommandReturnsNonZero(t *testing.T) {
oldArgs := os.Args
t.Cleanup(func() { os.Args = oldArgs })

os.Args = []string{"zeabur", "this-subcommand-does-not-exist-zeabur-cli-test"}

if code := run(); code == 0 {
t.Fatalf("run() = %d, want non-zero when Cobra returns an error", code)
}
}

func TestRun_VersionReturnsZero(t *testing.T) {
oldArgs := os.Args
t.Cleanup(func() { os.Args = oldArgs })

os.Args = []string{"zeabur", "version"}

if code := run(); code != 0 {
t.Fatalf("run() = %d, want 0 for zeabur version", code)
}
}