From dbbc4478f8e1aa60c17d3afed151818b97ca4e6a Mon Sep 17 00:00:00 2001 From: Simon Let Date: Thu, 15 Dec 2022 14:57:59 +0100 Subject: [PATCH] remove completions from reshctl, minor fixes and changes --- cmd/control/cmd/completion.go | 45 ----------------------------------- cmd/control/cmd/root.go | 6 +---- cmd/install-utils/device.go | 5 ++-- cmd/install-utils/main.go | 2 +- internal/device/device.go | 38 ++++++++++++++++++++++------- scripts/install.sh | 2 +- 6 files changed, 36 insertions(+), 62 deletions(-) delete mode 100644 cmd/control/cmd/completion.go diff --git a/cmd/control/cmd/completion.go b/cmd/control/cmd/completion.go deleted file mode 100644 index 6c0e450..0000000 --- a/cmd/control/cmd/completion.go +++ /dev/null @@ -1,45 +0,0 @@ -package cmd - -import ( - "os" - - "github.com/spf13/cobra" -) - -// completionCmd represents the completion command -var completionCmd = &cobra.Command{ - Use: "completion", - Short: "generate bash/zsh completion scripts", - Long: `To load completion run - -. <(reshctl completion bash) - -OR - -. <(reshctl completion zsh) && compdef _reshctl reshctl -`, -} - -var completionBashCmd = &cobra.Command{ - Use: "bash", - Short: "generate bash completion scripts", - Long: `To load completion run - -. <(reshctl completion bash) -`, - Run: func(cmd *cobra.Command, args []string) { - rootCmd.GenBashCompletion(os.Stdout) - }, -} - -var completionZshCmd = &cobra.Command{ - Use: "zsh", - Short: "generate zsh completion scripts", - Long: `To load completion run - -. <(reshctl completion zsh) && compdef _reshctl reshctl -`, - Run: func(cmd *cobra.Command, args []string) { - rootCmd.GenZshCompletion(os.Stdout) - }, -} diff --git a/cmd/control/cmd/root.go b/cmd/control/cmd/root.go index 475452c..3f4cfbc 100644 --- a/cmd/control/cmd/root.go +++ b/cmd/control/cmd/root.go @@ -16,7 +16,7 @@ var out *output.Output var rootCmd = &cobra.Command{ Use: "reshctl", - Short: "Reshctl (RESH control) - check status, update, enable/disable features, sanitize history and more.", + Short: "Reshctl (RESH control) - check status, update", } // Execute reshctl @@ -32,10 +32,6 @@ func Execute(ver, com, development string) { out.Error("Error while getting configuration", errCfg) } - rootCmd.AddCommand(completionCmd) - completionCmd.AddCommand(completionBashCmd) - completionCmd.AddCommand(completionZshCmd) - rootCmd.AddCommand(versionCmd) updateCmd.Flags().BoolVar(&betaFlag, "beta", false, "Update to latest version even if it's beta.") diff --git a/cmd/install-utils/device.go b/cmd/install-utils/device.go index 0f51998..903e69f 100644 --- a/cmd/install-utils/device.go +++ b/cmd/install-utils/device.go @@ -6,15 +6,16 @@ import ( "github.com/curusarn/resh/internal/datadir" "github.com/curusarn/resh/internal/device" + "github.com/curusarn/resh/internal/output" ) -func setupDevice() { +func setupDevice(out *output.Output) { dataDir, err := datadir.MakePath() if err != nil { fmt.Fprintf(os.Stderr, "ERROR: Failed to get/setup data directory: %v\n", err) os.Exit(1) } - err = device.SetupName(dataDir) + err = device.SetupName(out, dataDir) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: Failed to check/setup device name: %v\n", err) os.Exit(1) diff --git a/cmd/install-utils/main.go b/cmd/install-utils/main.go index d06d6f5..f19b283 100644 --- a/cmd/install-utils/main.go +++ b/cmd/install-utils/main.go @@ -48,7 +48,7 @@ func main() { case "migrate-history": migrateHistory(out) case "setup-device": - setupDevice() + setupDevice(out) case "help": printUsage(os.Stdout) default: diff --git a/internal/device/device.go b/internal/device/device.go index a952f22..e1857c1 100644 --- a/internal/device/device.go +++ b/internal/device/device.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/curusarn/resh/internal/futil" + "github.com/curusarn/resh/internal/output" "github.com/google/uuid" isatty "github.com/mattn/go-isatty" ) @@ -14,6 +15,8 @@ import ( const fnameID = "device-id" const fnameName = "device-name" +const fpathIDLegacy = ".resh/resh-uuid" + const filePerm = 0644 // Getters @@ -29,11 +32,11 @@ func GetName(dataDir string) (string, error) { // Install helpers func SetupID(dataDir string) error { - return generateIDIfUnset(dataDir) + return setIDIfUnset(dataDir) } -func SetupName(dataDir string) error { - return promptAndWriteNameIfUnset(dataDir) +func SetupName(out *output.Output, dataDir string) error { + return promptAndWriteNameIfUnset(out, dataDir) } func readValue(dataDir, fname string) (string, error) { @@ -46,7 +49,7 @@ func readValue(dataDir, fname string) (string, error) { return val, nil } -func generateIDIfUnset(dataDir string) error { +func setIDIfUnset(dataDir string) error { fpath := path.Join(dataDir, fnameID) exists, err := futil.FileExists(fpath) if err != nil { @@ -56,6 +59,25 @@ func generateIDIfUnset(dataDir string) error { return nil } + // Try copy device ID from legacy location + homeDir, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("could not get user home: %w", err) + } + fpathLegacy := path.Join(homeDir, fpathIDLegacy) + exists, err = futil.FileExists(fpath) + if err != nil { + return err + } + if exists { + futil.CopyFile(fpathLegacy, fpath) + if err != nil { + return fmt.Errorf("could not copy device ID from legacy location: %w", err) + } + return nil + } + + // Generate new device ID rnd, err := uuid.NewRandom() if err != nil { return fmt.Errorf("could not get new random source: %w", err) @@ -71,7 +93,7 @@ func generateIDIfUnset(dataDir string) error { return nil } -func promptAndWriteNameIfUnset(dataDir string) error { +func promptAndWriteNameIfUnset(out *output.Output, dataDir string) error { fpath := path.Join(dataDir, fnameName) exists, err := futil.FileExists(fpath) if err != nil { @@ -81,7 +103,7 @@ func promptAndWriteNameIfUnset(dataDir string) error { return nil } - name, err := promptForName(fpath) + name, err := promptForName(out, fpath) if err != nil { return fmt.Errorf("error while prompting for input: %w", err) } @@ -92,7 +114,7 @@ func promptAndWriteNameIfUnset(dataDir string) error { return nil } -func promptForName(fpath string) (string, error) { +func promptForName(out *output.Output, fpath string) (string, error) { // This function should be only ran from install-utils with attached terminal if !isatty.IsTerminal(os.Stdout.Fd()) { return "", fmt.Errorf("output is not a terminal - write name of this device to '%s' to bypass this error", fpath) @@ -111,7 +133,7 @@ func promptForName(fpath string) (string, error) { if err != nil { return "", fmt.Errorf("scanln error: %w", err) } - fmt.Printf("Input was: %s\n", input) + out.Info(fmt.Sprintf("Device name set to '%s'", input)) fmt.Printf("You can change the device name at any time by editing '%s' file\n", fpath) return input, nil } diff --git a/scripts/install.sh b/scripts/install.sh index 4ee46c4..62efed4 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -113,7 +113,7 @@ echo "Creating/updating config file ..." ./bin/resh-install-utils migrate-config echo "Checking/setting up device files ..." -./bin/resh-install-utils setup-device +./bin/resh-install-utils setup-device echo "Updating format of history file ..." ./bin/resh-install-utils migrate-history