From fb24357d38411a373c725f4d6f8fd0ba1be2ede2 Mon Sep 17 00:00:00 2001 From: cellarspoon Date: Tue, 28 Dec 2021 01:26:40 +0100 Subject: [PATCH] refactor: merge top-level into one file --- cli/autocomplete.go | 97 ----------------------------------- cli/cli.go | 120 ++++++++++++++++++++++++++++++++++++++++++++ cli/upgrade.go | 45 ----------------- 3 files changed, 120 insertions(+), 142 deletions(-) delete mode 100644 cli/autocomplete.go delete mode 100644 cli/upgrade.go diff --git a/cli/autocomplete.go b/cli/autocomplete.go deleted file mode 100644 index 076023dc..00000000 --- a/cli/autocomplete.go +++ /dev/null @@ -1,97 +0,0 @@ -package cli - -import ( - "errors" - "fmt" - "os" - "path" - - "coopcloud.tech/abra/cli/internal" - "coopcloud.tech/abra/pkg/config" - "coopcloud.tech/abra/pkg/web" - "github.com/sirupsen/logrus" - "github.com/urfave/cli/v2" -) - -// AutoCompleteCommand helps people set up auto-complete in their shells -var AutoCompleteCommand = &cli.Command{ - Name: "autocomplete", - Usage: "Configure shell autocompletion (recommended)", - Aliases: []string{"ac"}, - Description: ` -This command helps set up autocompletion in your shell by downloading the -relevant autocompletion files and laying out what additional information must -be loaded. - -Example: - - abra autocomplete bash - -Supported shells are as follows: - - fizsh - zsh - bash - -`, - ArgsUsage: "", - Action: func(c *cli.Context) error { - shellType := c.Args().First() - - if shellType == "" { - internal.ShowSubcommandHelpAndError(c, errors.New("no shell provided")) - } - - supportedShells := map[string]bool{ - "bash": true, - "zsh": true, - "fizsh": true, - } - - if _, ok := supportedShells[shellType]; !ok { - logrus.Fatalf("%s is not a supported shell right now, sorry", shellType) - } - - if shellType == "fizsh" { - shellType = "zsh" // handled the same on the autocompletion side - } - - autocompletionDir := path.Join(config.ABRA_DIR, "autocompletion") - if err := os.Mkdir(autocompletionDir, 0764); err != nil { - if !os.IsExist(err) { - logrus.Fatal(err) - } - logrus.Debugf("%s already created", autocompletionDir) - } - - autocompletionFile := path.Join(config.ABRA_DIR, "autocompletion", shellType) - if _, err := os.Stat(autocompletionFile); err != nil && os.IsNotExist(err) { - url := fmt.Sprintf("https://git.coopcloud.tech/coop-cloud/abra/raw/branch/main/scripts/autocomplete/%s", shellType) - logrus.Infof("fetching %s", url) - if err := web.GetFile(autocompletionFile, url); err != nil { - logrus.Fatal(err) - } - } - - switch shellType { - case "bash": - fmt.Println(fmt.Sprintf(` -# Run the following commands to install autocompletion -sudo mkdir /etc/bash_completion.d/ -sudo cp %s /etc/bash_completion.d/abra -echo "source /etc/bash_completion.d/abra" >> ~/.bashrc -# And finally run "abra app ps " to test things are working, you should see app names listed! -`, autocompletionFile)) - case "zsh": - fmt.Println(fmt.Sprintf(` -# Run the following commands to install autocompletion -sudo mkdir /etc/zsh/completion.d/ -sudo cp %s /etc/zsh/completion.d/abra -echo "PROG=abra\n_CLI_ZSH_AUTOCOMPLETE_HACK=1\nsource /etc/zsh/completion.d/abra" >> ~/.zshrc -# And finally run "abra app ps " to test things are working, you should see app names listed! -`, autocompletionFile)) - } - - return nil - }, -} diff --git a/cli/cli.go b/cli/cli.go index 95c95c87..195a8ca9 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -2,8 +2,10 @@ package cli import ( + "errors" "fmt" "os" + "os/exec" "path" "coopcloud.tech/abra/cli/app" @@ -13,11 +15,129 @@ import ( "coopcloud.tech/abra/cli/record" "coopcloud.tech/abra/cli/server" "coopcloud.tech/abra/pkg/config" + "coopcloud.tech/abra/pkg/web" logrusStack "github.com/Gurpartap/logrus-stack" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) +// AutoCompleteCommand helps people set up auto-complete in their shells +var AutoCompleteCommand = &cli.Command{ + Name: "autocomplete", + Usage: "Configure shell autocompletion (recommended)", + Aliases: []string{"ac"}, + Description: ` +This command helps set up autocompletion in your shell by downloading the +relevant autocompletion files and laying out what additional information must +be loaded. + +Example: + + abra autocomplete bash + +Supported shells are as follows: + + fizsh + zsh + bash + +`, + ArgsUsage: "", + Action: func(c *cli.Context) error { + shellType := c.Args().First() + + if shellType == "" { + internal.ShowSubcommandHelpAndError(c, errors.New("no shell provided")) + } + + supportedShells := map[string]bool{ + "bash": true, + "zsh": true, + "fizsh": true, + } + + if _, ok := supportedShells[shellType]; !ok { + logrus.Fatalf("%s is not a supported shell right now, sorry", shellType) + } + + if shellType == "fizsh" { + shellType = "zsh" // handled the same on the autocompletion side + } + + autocompletionDir := path.Join(config.ABRA_DIR, "autocompletion") + if err := os.Mkdir(autocompletionDir, 0764); err != nil { + if !os.IsExist(err) { + logrus.Fatal(err) + } + logrus.Debugf("%s already created", autocompletionDir) + } + + autocompletionFile := path.Join(config.ABRA_DIR, "autocompletion", shellType) + if _, err := os.Stat(autocompletionFile); err != nil && os.IsNotExist(err) { + url := fmt.Sprintf("https://git.coopcloud.tech/coop-cloud/abra/raw/branch/main/scripts/autocomplete/%s", shellType) + logrus.Infof("fetching %s", url) + if err := web.GetFile(autocompletionFile, url); err != nil { + logrus.Fatal(err) + } + } + + switch shellType { + case "bash": + fmt.Println(fmt.Sprintf(` +# Run the following commands to install autocompletion +sudo mkdir /etc/bash_completion.d/ +sudo cp %s /etc/bash_completion.d/abra +echo "source /etc/bash_completion.d/abra" >> ~/.bashrc +# And finally run "abra app ps " to test things are working, you should see app names listed! +`, autocompletionFile)) + case "zsh": + fmt.Println(fmt.Sprintf(` +# Run the following commands to install autocompletion +sudo mkdir /etc/zsh/completion.d/ +sudo cp %s /etc/zsh/completion.d/abra +echo "PROG=abra\n_CLI_ZSH_AUTOCOMPLETE_HACK=1\nsource /etc/zsh/completion.d/abra" >> ~/.zshrc +# And finally run "abra app ps " to test things are working, you should see app names listed! +`, autocompletionFile)) + } + + return nil + }, +} + +// UpgradeCommand upgrades abra in-place. +var UpgradeCommand = &cli.Command{ + Name: "upgrade", + Usage: "Upgrade Abra itself", + Aliases: []string{"u"}, + Description: ` +This command allows you to upgrade Abra in-place with the latest stable or +release candidate. + +If you would like to install the latest release candidate, please pass the +"--rc" option. Please bear in mind that the latest release candidate may have +some catastrophic bugs contained in it. In any case, thank you very much for +testing efforts! +`, + Flags: []cli.Flag{internal.RCFlag}, + Action: func(c *cli.Context) error { + mainURL := "https://install.abra.coopcloud.tech" + cmd := exec.Command("bash", "-c", fmt.Sprintf("curl -s %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("curl -s %s | bash -s -- --rc", releaseCandidateURL)) + } + + logrus.Debugf("attempting to run %s", cmd) + + if err := internal.RunCmd(cmd); err != nil { + logrus.Fatal(err) + } + + return nil + }, +} + func newAbraApp(version, commit string) *cli.App { app := &cli.App{ Name: "abra", diff --git a/cli/upgrade.go b/cli/upgrade.go deleted file mode 100644 index aaf4f716..00000000 --- a/cli/upgrade.go +++ /dev/null @@ -1,45 +0,0 @@ -package cli - -import ( - "fmt" - "os/exec" - - "coopcloud.tech/abra/cli/internal" - "github.com/sirupsen/logrus" - "github.com/urfave/cli/v2" -) - -var mainURL = "https://install.abra.coopcloud.tech" - -var releaseCandidateURL = "https://git.coopcloud.tech/coop-cloud/abra/raw/branch/main/scripts/installer/installer" - -// UpgradeCommand upgrades abra in-place. -var UpgradeCommand = &cli.Command{ - Name: "upgrade", - Usage: "Upgrade Abra itself", - Aliases: []string{"u"}, - Description: ` -This command allows you to upgrade Abra in-place with the latest stable or -release candidate. - -If you would like to install the latest release candidate, please pass the -"--rc" option. Please bear in mind that the latest release candidate may have -some catastrophic bugs contained in it. In any case, thank you very much for -testing efforts! -`, - Flags: []cli.Flag{internal.RCFlag}, - Action: func(c *cli.Context) error { - cmd := exec.Command("bash", "-c", fmt.Sprintf("curl -s %s | bash", mainURL)) - if internal.RC { - cmd = exec.Command("bash", "-c", fmt.Sprintf("curl -s %s | bash -s -- --rc", releaseCandidateURL)) - } - - logrus.Debugf("attempting to run %s", cmd) - - if err := internal.RunCmd(cmd); err != nil { - logrus.Fatal(err) - } - - return nil - }, -}