remove completions from reshctl, minor fixes and changes

pull/184/head
Simon Let 3 years ago
parent 1a584c78f6
commit dbbc4478f8
  1. 45
      cmd/control/cmd/completion.go
  2. 6
      cmd/control/cmd/root.go
  3. 5
      cmd/install-utils/device.go
  4. 2
      cmd/install-utils/main.go
  5. 38
      internal/device/device.go

@ -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)
},
}

@ -16,7 +16,7 @@ var out *output.Output
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "reshctl", 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 // Execute reshctl
@ -32,10 +32,6 @@ func Execute(ver, com, development string) {
out.Error("Error while getting configuration", errCfg) out.Error("Error while getting configuration", errCfg)
} }
rootCmd.AddCommand(completionCmd)
completionCmd.AddCommand(completionBashCmd)
completionCmd.AddCommand(completionZshCmd)
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
updateCmd.Flags().BoolVar(&betaFlag, "beta", false, "Update to latest version even if it's beta.") updateCmd.Flags().BoolVar(&betaFlag, "beta", false, "Update to latest version even if it's beta.")

@ -6,15 +6,16 @@ import (
"github.com/curusarn/resh/internal/datadir" "github.com/curusarn/resh/internal/datadir"
"github.com/curusarn/resh/internal/device" "github.com/curusarn/resh/internal/device"
"github.com/curusarn/resh/internal/output"
) )
func setupDevice() { func setupDevice(out *output.Output) {
dataDir, err := datadir.MakePath() dataDir, err := datadir.MakePath()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Failed to get/setup data directory: %v\n", err) fmt.Fprintf(os.Stderr, "ERROR: Failed to get/setup data directory: %v\n", err)
os.Exit(1) os.Exit(1)
} }
err = device.SetupName(dataDir) err = device.SetupName(out, dataDir)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Failed to check/setup device name: %v\n", err) fmt.Fprintf(os.Stderr, "ERROR: Failed to check/setup device name: %v\n", err)
os.Exit(1) os.Exit(1)

@ -48,7 +48,7 @@ func main() {
case "migrate-history": case "migrate-history":
migrateHistory(out) migrateHistory(out)
case "setup-device": case "setup-device":
setupDevice() setupDevice(out)
case "help": case "help":
printUsage(os.Stdout) printUsage(os.Stdout)
default: default:

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/curusarn/resh/internal/futil" "github.com/curusarn/resh/internal/futil"
"github.com/curusarn/resh/internal/output"
"github.com/google/uuid" "github.com/google/uuid"
isatty "github.com/mattn/go-isatty" isatty "github.com/mattn/go-isatty"
) )
@ -14,6 +15,8 @@ import (
const fnameID = "device-id" const fnameID = "device-id"
const fnameName = "device-name" const fnameName = "device-name"
const fpathIDLegacy = ".resh/resh-uuid"
const filePerm = 0644 const filePerm = 0644
// Getters // Getters
@ -29,11 +32,11 @@ func GetName(dataDir string) (string, error) {
// Install helpers // Install helpers
func SetupID(dataDir string) error { func SetupID(dataDir string) error {
return generateIDIfUnset(dataDir) return setIDIfUnset(dataDir)
} }
func SetupName(dataDir string) error { func SetupName(out *output.Output, dataDir string) error {
return promptAndWriteNameIfUnset(dataDir) return promptAndWriteNameIfUnset(out, dataDir)
} }
func readValue(dataDir, fname string) (string, error) { func readValue(dataDir, fname string) (string, error) {
@ -46,7 +49,7 @@ func readValue(dataDir, fname string) (string, error) {
return val, nil return val, nil
} }
func generateIDIfUnset(dataDir string) error { func setIDIfUnset(dataDir string) error {
fpath := path.Join(dataDir, fnameID) fpath := path.Join(dataDir, fnameID)
exists, err := futil.FileExists(fpath) exists, err := futil.FileExists(fpath)
if err != nil { if err != nil {
@ -56,6 +59,25 @@ func generateIDIfUnset(dataDir string) error {
return nil 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() rnd, err := uuid.NewRandom()
if err != nil { if err != nil {
return fmt.Errorf("could not get new random source: %w", err) return fmt.Errorf("could not get new random source: %w", err)
@ -71,7 +93,7 @@ func generateIDIfUnset(dataDir string) error {
return nil return nil
} }
func promptAndWriteNameIfUnset(dataDir string) error { func promptAndWriteNameIfUnset(out *output.Output, dataDir string) error {
fpath := path.Join(dataDir, fnameName) fpath := path.Join(dataDir, fnameName)
exists, err := futil.FileExists(fpath) exists, err := futil.FileExists(fpath)
if err != nil { if err != nil {
@ -81,7 +103,7 @@ func promptAndWriteNameIfUnset(dataDir string) error {
return nil return nil
} }
name, err := promptForName(fpath) name, err := promptForName(out, fpath)
if err != nil { if err != nil {
return fmt.Errorf("error while prompting for input: %w", err) return fmt.Errorf("error while prompting for input: %w", err)
} }
@ -92,7 +114,7 @@ func promptAndWriteNameIfUnset(dataDir string) error {
return nil 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 // This function should be only ran from install-utils with attached terminal
if !isatty.IsTerminal(os.Stdout.Fd()) { 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) 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 { if err != nil {
return "", fmt.Errorf("scanln error: %w", err) 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) fmt.Printf("You can change the device name at any time by editing '%s' file\n", fpath)
return input, nil return input, nil
} }

Loading…
Cancel
Save