From 1bdf1fcfaedfb7ab43711df7eaece42a6ef54448 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 18 Jul 2024 21:37:09 +0200 Subject: [PATCH] wip: refactor!: migrate to cobra --- cli/cli.go | 34 +++++++++++++++------------------- cli/newcli.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/abra/main.go | 2 +- go.mod | 2 +- 4 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 cli/newcli.go diff --git a/cli/cli.go b/cli/cli.go index e3ed5728..7a334ca6 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -18,6 +18,7 @@ import ( "coopcloud.tech/abra/pkg/log" "coopcloud.tech/abra/pkg/web" charmLog "github.com/charmbracelet/log" + "github.com/spf13/cobra" "github.com/urfave/cli" ) @@ -112,38 +113,30 @@ source /etc/fish/completions/abra } // UpgradeCommand upgrades abra in-place. -var UpgradeCommand = cli.Command{ - Name: "upgrade", +var UpgradeCommand = &cobra.Command{ + Use: "upgrade", Aliases: []string{"u"}, - Usage: "Upgrade abra", - Description: ` -Upgrade abra in-place with the latest stable or release candidate. + Short: "Upgrade abra", + Example: "abra upgrade", + Long: `Upgrade abra in-place with the latest stable or release candidate. Use "-r/--rc" to install the latest release candidate. Please bear in mind that it may contain absolutely catastrophic deal-breaker bugs. Thank you very much -for the testing efforts 💗 - -EXAMPLE: - - abra upgrade - abra upgrade --rc`, - Flags: []cli.Flag{internal.RCFlag}, - Action: func(c *cli.Context) error { +for the testing efforts 💗`, + Run: func(cmd *cobra.Command, args []string) { mainURL := "https://install.abra.coopcloud.tech" - cmd := exec.Command("bash", "-c", fmt.Sprintf("wget -q -O- %s | bash", mainURL)) + comm := exec.Command("bash", "-c", fmt.Sprintf("wget -q -O- %s | bash", mainURL)) if internal.RC { releaseCandidateURL := "https://git.coopcloud.tech/coop-cloud/abra/raw/branch/main/scripts/installer/installer" - cmd = exec.Command("bash", "-c", fmt.Sprintf("wget -q -O- %s | bash -s -- --rc", releaseCandidateURL)) + comm = exec.Command("bash", "-c", fmt.Sprintf("wget -q -O- %s | bash -s -- --rc", releaseCandidateURL)) } log.Debugf("attempting to run %s", cmd) - if err := internal.RunCmd(cmd); err != nil { + if err := internal.RunCmd(comm); err != nil { log.Fatal(err) } - - return nil }, } @@ -164,7 +157,6 @@ func newAbraApp(version, commit string) *cli.App { server.ServerCommand, recipe.RecipeCommand, catalogue.CatalogueCommand, - UpgradeCommand, AutoCompleteCommand, }, BashComplete: autocomplete.SubcommandComplete, @@ -209,3 +201,7 @@ func RunApp(version, commit string) { log.Fatal(err) } } + +func init() { + UpgradeCommand.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cli/newcli.go b/cli/newcli.go new file mode 100644 index 00000000..56aa7950 --- /dev/null +++ b/cli/newcli.go @@ -0,0 +1,46 @@ +package cli + +import ( + "fmt" + + "coopcloud.tech/abra/pkg/log" + "github.com/spf13/cobra" +) + +var ( + Debug bool + Offline bool + NoInput bool +) + +func RunApp2(version, commit string) { + rootCmd := &cobra.Command{ + Use: "abra", + Short: "The Co-op Cloud command-line utility belt 🎩🐇", + Version: fmt.Sprintf("%s-%s", version, commit[:7]), + PreRun: func(cmd *cobra.Command, args []string) { + log.Info("HELLO LOGGING") + }, + } + + rootCmd.PersistentFlags().BoolVarP( + &Debug, "debug", "d", false, + "show debug messages", + ) + + rootCmd.PersistentFlags().BoolVarP( + &NoInput, "no-input", "n", false, + "toggle non-interactive mode", + ) + + rootCmd.PersistentFlags().BoolVarP( + &Offline, "offline", "o", false, + "prefer offline & filesystem access", + ) + + rootCmd.AddCommand(UpgradeCommand) + + if err := rootCmd.Execute(); err != nil { + log.Fatal(err) + } +} diff --git a/cmd/abra/main.go b/cmd/abra/main.go index de4e4318..5955243b 100644 --- a/cmd/abra/main.go +++ b/cmd/abra/main.go @@ -19,5 +19,5 @@ func main() { Commit = " " } - cli.RunApp(Version, Commit) + cli.RunApp2(Version, Commit) } diff --git a/go.mod b/go.mod index b49de930..0be9028d 100644 --- a/go.mod +++ b/go.mod @@ -130,7 +130,7 @@ require ( github.com/opencontainers/image-spec v1.1.0 // indirect github.com/prometheus/client_golang v1.19.1 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect - github.com/spf13/cobra v1.8.1 // indirect + github.com/spf13/cobra v1.8.1 github.com/stretchr/testify v1.9.0 github.com/theupdateframework/notary v0.7.0 // indirect github.com/urfave/cli v1.22.15