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
  6. 2
      scripts/install.sh

@ -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{
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.")

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

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

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

@ -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

Loading…
Cancel
Save