Skip to content

Commit

Permalink
refactor root command
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeo committed Dec 10, 2024
1 parent 1b4cc57 commit 6152999
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 184 deletions.
182 changes: 0 additions & 182 deletions cmd/root.go

This file was deleted.

94 changes: 94 additions & 0 deletions cmd/root/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package root

import (
"errors"
"log/slog"
"os"

"github.com/spf13/cobra"
"numerous.com/cli/cmd/app"
"numerous.com/cli/cmd/config"
"numerous.com/cli/cmd/deletecmd"
"numerous.com/cli/cmd/deploy"
"numerous.com/cli/cmd/download"
"numerous.com/cli/cmd/errorhandling"
cmdinit "numerous.com/cli/cmd/init"
"numerous.com/cli/cmd/legacy"
"numerous.com/cli/cmd/login"
"numerous.com/cli/cmd/logout"
"numerous.com/cli/cmd/logs"
"numerous.com/cli/cmd/organization"
"numerous.com/cli/cmd/output"
"numerous.com/cli/cmd/token"
cmdversion "numerous.com/cli/cmd/version"
"numerous.com/cli/internal/logging"
)

var args = struct {
logLevel logging.Level
}{
logLevel: logging.LevelError,
}

var cmd = &cobra.Command{
Use: "numerous",
Long: logo,
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: prerun,
}

func Execute() {
executedCmd, err := cmd.ExecuteC()
if err != nil {
if !errors.Is(err, errorhandling.ErrAlreadyPrinted) {
output.PrintError("Error: %s", "", err.Error())
println()
executedCmd.Usage() // nolint: errcheck
}
os.Exit(1)
}
}

func init() {
cmd.PersistentFlags().VarP(&args.logLevel, "log-level", "l", "The log level, one of \"debug\", \"info\", \"warning\", or \"error\". Defaults to \"error\".")

cmd.AddGroup(&cobra.Group{
Title: "Numerous App Commands:",
ID: "app-cmds",
})
cmd.AddGroup(&cobra.Group{
Title: "Additional Numerous Commands:",
ID: "additional-cmds",
})

cmd.AddCommand(
cmdinit.Cmd,
login.Cmd,
logout.Cmd,
organization.Cmd,
legacy.Cmd,
deletecmd.Cmd,
deploy.Cmd,
logs.Cmd,
download.Cmd,
token.Cmd,
cmdversion.Cmd,
app.Cmd,
config.Cmd,

// dummy commands to display helpful messages for legacy commands
dummyLegacyCmd("push"),
dummyLegacyCmd("publish"),
dummyLegacyCmd("unpublish"),
dummyLegacyCmd("list"),
dummyLegacyCmd("log"),
)

cobra.OnInitialize(func() {
logOpts := &slog.HandlerOptions{Level: args.logLevel.ToSlogLevel()}
logHandler := slog.NewTextHandler(os.Stderr, logOpts)
logger := slog.New(logHandler)
slog.SetDefault(logger)
})
}
18 changes: 18 additions & 0 deletions cmd/root/logo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package root

const logo = `
~~~
--- ~~~~~~~
° ------- ~~~~~~~~~~
°°°° ----------- ~~~~~~~~~
°°°°°°° ----------- ~~~~~~~~ _ _
°°°°°°°°°° ------- ~~~~~~~~ | \ | |
°°°°°°°°°°°°° ----- ~~~~~~~ | \| |_ _ _ __ ___ ___ _ __ ___ _ _ ___
°°°°°°°°°°°°° ----- ~~~~~~~ | . ` + "`" + ` | | | | '_ ` + "`" + ` _ \ / _ \ '__/ _ \| | | / __|
°°°°°°°°°°°°° ----- ~~~~ | |\ | |_| | | | | | | __/ | | (_) | |_| \__ \
°°°°°°°°°°°°° ---- ~~ |_| \_|\__,_|_| |_| |_|\___|_| \___/ \__,_|___/
°°°°°°°°°° --
°°°°°°°°
°°°°°
°°
`
87 changes: 87 additions & 0 deletions cmd/root/prerun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package root

import (
"errors"
"net/http"
"os"

"numerous.com/cli/cmd/errorhandling"
"numerous.com/cli/cmd/output"
cmdversion "numerous.com/cli/cmd/version"
"numerous.com/cli/internal/auth"
"numerous.com/cli/internal/gql"
"numerous.com/cli/internal/version"

"github.com/spf13/cobra"
)

var (
ErrNotAuthorized = errors.New("not authorized")
ErrIncompatibleVersion = errors.New("incompatible version")
)

func prerun(cmd *cobra.Command, args []string) error {
output.NotifyFeedbackMaybe()

if !cmdversion.Check(version.NewService(gql.NewClient())) {
return errorhandling.ErrorAlreadyPrinted(ErrIncompatibleVersion)
}

if !commandRequiresAuthentication(cmd.CommandPath()) {
return nil
}

if os.Getenv("NUMEROUS_ACCESS_TOKEN") != "" {
return nil
}

user := auth.NumerousTenantAuthenticator.GetLoggedInUserFromKeyring()
if user.CheckAuthenticationStatus() == auth.ErrUserNotLoggedIn {
output.PrintErrorLoginForCommand(cmd)
return ErrNotAuthorized
}

if err := user.RefreshAccessToken(http.DefaultClient, auth.NumerousTenantAuthenticator); err != nil {
return err
}

return nil
}

func commandRequiresAuthentication(invokedCommandName string) bool {
commandsWithAuthRequired := []string{
"numerous legacy list",
"numerous legacy push",
"numerous legacy log",
"numerous organization create",
"numerous organization list",
"numerous deploy",
"numerous delete",
"numerous download",
"numerous logs",
"numerous token create",
"numerous token list",
"numerous token revoke",
"numerous app list",
"numerous app share",
"numerous app unshare",
}

for _, cmd := range commandsWithAuthRequired {
if cmd == invokedCommandName {
return true
}
}

return false
}

func dummyLegacyCmd(cmd string) *cobra.Command {
return &cobra.Command{
Hidden: true,
Use: cmd,
Run: func(*cobra.Command, []string) {
output.NotifyCmdMoved("numerous "+cmd, "numerous legacy "+cmd)
},
}
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import "numerous.com/cli/cmd"
import "numerous.com/cli/cmd/root"

func main() {
cmd.Execute()
root.Execute()
}

0 comments on commit 6152999

Please sign in to comment.