diff --git a/client/cmd/link.go b/client/cmd/link.go index 9fb1388..21aa579 100644 --- a/client/cmd/link.go +++ b/client/cmd/link.go @@ -3,6 +3,8 @@ package cmd import ( "fmt" "os" + "path/filepath" + "strings" "github.com/spf13/cobra" "source.quilibrium.com/quilibrium/monorepo/client/utils" @@ -31,6 +33,33 @@ Example: qclient link`, os.Exit(1) } + // Check if the current executable is in the expected location + expectedPrefix := utils.ClientDataPath + + // Check if the current executable is in the expected location + if !strings.HasPrefix(execPath, expectedPrefix) { + fmt.Printf("Current executable is not in the expected location: %s\n", execPath) + fmt.Printf("Expected location should start with: %s\n", expectedPrefix) + + // Ask user if they want to move it + fmt.Print("Would you like to move the executable to the standard location? (y/n): ") + var response string + fmt.Scanln(&response) + + if strings.ToLower(response) == "y" || strings.ToLower(response) == "yes" { + if err := moveExecutableToStandardLocation(execPath); err != nil { + return fmt.Errorf("failed to move executable: %w", err) + } + // Update execPath to the new location + execPath, err = os.Executable() + if err != nil { + return fmt.Errorf("failed to get new executable path: %w", err) + } + } else { + fmt.Println("Continuing with current location...") + } + } + // Create the symlink (handles existing symlinks) if err := utils.CreateSymlink(execPath, symlinkPath); err != nil { return err @@ -41,6 +70,31 @@ Example: qclient link`, }, } +func moveExecutableToStandardLocation(execPath string) error { + // Get the directory of the current executable + version, err := GetVersionInfo(false) + if err != nil { + return fmt.Errorf("failed to get version info: %w", err) + } + destDir := filepath.Join(utils.ClientDataPath, "bin", version.Version) + + // Create the standard location directory if it doesn't exist + currentUser, err := utils.GetCurrentSudoUser() + if err != nil { + return fmt.Errorf("failed to get current user: %w", err) + } + if err := utils.ValidateAndCreateDir(destDir, currentUser); err != nil { + return fmt.Errorf("failed to create directory: %w", err) + } + + // Move the executable to the standard location + if err := os.Rename(execPath, filepath.Join(destDir, StandardizedQClientFileName)); err != nil { + return fmt.Errorf("failed to move executable: %w", err) + } + + return nil +} + func init() { rootCmd.AddCommand(linkCmd) } diff --git a/client/cmd/node/config/config.go b/client/cmd/node/config/config.go index 6a7d655..0c1fcd8 100644 --- a/client/cmd/node/config/config.go +++ b/client/cmd/node/config/config.go @@ -14,6 +14,7 @@ var ( NodeUser *user.User ConfigDirs string NodeConfigToRun string + SetDefault bool ) // ConfigCmd represents the node config command @@ -47,7 +48,12 @@ This command provides utilities for configuring your Quilibrium node, such as: } func init() { + importCmd.Flags().BoolVarP(&SetDefault, "default", "d", false, "Select this config as the default") ConfigCmd.AddCommand(importCmd) + ConfigCmd.AddCommand(SwitchConfigCmd) + + createCmd.Flags().BoolVarP(&SetDefault, "default", "d", false, "Select this config as the default") ConfigCmd.AddCommand(createCmd) + ConfigCmd.AddCommand(setCmd) } diff --git a/client/cmd/node/config/create.go b/client/cmd/node/config/create.go index 079d106..7b617ec 100644 --- a/client/cmd/node/config/create.go +++ b/client/cmd/node/config/create.go @@ -10,8 +10,6 @@ import ( "source.quilibrium.com/quilibrium/monorepo/client/utils" ) -var setDefault bool - var createCmd = &cobra.Command{ Use: "create [name]", Short: "Create a default configuration file set for a node", @@ -87,7 +85,7 @@ The third example will create a new configuration at %s/myconfig and symlink it os.Exit(1) } - if setDefault { + if SetDefault { // Create the symlink if err := utils.CreateSymlink(configDir, NodeConfigToRun); err != nil { fmt.Printf("Failed to create symlink: %s\n", err) @@ -101,7 +99,3 @@ The third example will create a new configuration at %s/myconfig and symlink it fmt.Println("The keys.yml file will only contain 'null:' until the node is started.") }, } - -func init() { - createCmd.Flags().BoolVarP(&setDefault, "default", "d", false, "Select this config as the default") -} diff --git a/client/cmd/node/config/import.go b/client/cmd/node/config/import.go index a1fdeba..0f3cc39 100644 --- a/client/cmd/node/config/import.go +++ b/client/cmd/node/config/import.go @@ -59,6 +59,20 @@ This will copy config.yml and keys.yml from /path/to/source to /home/quilibrium/ os.Exit(1) } - fmt.Printf("Successfully imported config files to %s\n", targetDir) + if SetDefault { + // Create the symlink + if err := utils.CreateSymlink(targetDir, NodeConfigToRun); err != nil { + fmt.Printf("Failed to create symlink: %s\n", err) + os.Exit(1) + } + + fmt.Printf("Successfully imported config files to %s and symlinked to default\n", name) + } else { + fmt.Printf("Successfully imported config files to %s\n", targetDir) + } }, } + +func init() { + +} diff --git a/client/cmd/node/config/set.go b/client/cmd/node/config/set.go index fc0fd06..3ffc5cc 100644 --- a/client/cmd/node/config/set.go +++ b/client/cmd/node/config/set.go @@ -80,7 +80,3 @@ Example: fmt.Printf("Successfully updated %s to %s in %s\n", key, value, configFile) }, } - -func init() { - ConfigCmd.AddCommand(setCmd) -} diff --git a/client/cmd/node/install.go b/client/cmd/node/install.go index 4c6a761..4c36f07 100644 --- a/client/cmd/node/install.go +++ b/client/cmd/node/install.go @@ -10,7 +10,7 @@ import ( ) // installCmd represents the command to install the Quilibrium node -var installCmd = &cobra.Command{ +var InstallCmd = &cobra.Command{ Use: "install [version]", Short: "Install Quilibrium node", Long: `Install Quilibrium node binary and create a service to run it. @@ -136,17 +136,12 @@ Examples: fmt.Fprintf(os.Stdout, "Installing Quilibrium node for %s-%s, version: %s\n", osType, arch, version) // Install the node - installNode(version) + InstallNode(version) }, } -func init() { - // Add the install command to the node command - NodeCmd.AddCommand(installCmd) -} - // installNode installs the Quilibrium node -func installNode(version string) { +func InstallNode(version string) { // Create installation directory if err := utils.ValidateAndCreateDir(utils.NodeDataPath, NodeUser); err != nil { fmt.Fprintf(os.Stderr, "Error creating installation directory: %v\n", err) @@ -158,7 +153,7 @@ func installNode(version string) { os.Exit(1) } - if err := installByVersion(version); err != nil { + if err := InstallByVersion(version); err != nil { fmt.Fprintf(os.Stderr, "Error installing specific version: %v\n", err) os.Exit(1) } @@ -169,7 +164,7 @@ func installNode(version string) { } // installByVersion installs a specific version of the Quilibrium node -func installByVersion(version string) error { +func InstallByVersion(version string) error { versionDir := filepath.Join(utils.NodeDataPath, version) if err := utils.ValidateAndCreateDir(versionDir, NodeUser); err != nil { diff --git a/client/cmd/node/node.go b/client/cmd/node/node.go index 120f610..1ba9144 100644 --- a/client/cmd/node/node.go +++ b/client/cmd/node/node.go @@ -66,7 +66,7 @@ func init() { NodeCmd.PersistentFlags().StringVar(&configDirectory, "config", ".config", "config directory (default is .config/)") // Add subcommands - NodeCmd.AddCommand(installCmd) + NodeCmd.AddCommand(InstallCmd) NodeCmd.AddCommand(clientNodeConfig.ConfigCmd) NodeCmd.AddCommand(updateNodeCmd) NodeCmd.AddCommand(nodeServiceCmd) diff --git a/client/cmd/node/update.go b/client/cmd/node/update.go index 5929e34..177f110 100644 --- a/client/cmd/node/update.go +++ b/client/cmd/node/update.go @@ -3,8 +3,6 @@ package node import ( "fmt" "os" - "path/filepath" - "strings" "github.com/spf13/cobra" "source.quilibrium.com/quilibrium/monorepo/client/utils" @@ -30,6 +28,23 @@ Examples: // Determine version to install version := determineVersion(args) + // Download and install the node + if version == "latest" { + latestVersion, err := utils.GetLatestVersion(utils.ReleaseTypeNode) + if err != nil { + fmt.Fprintf(os.Stderr, "Error getting latest version: %v\n", err) + return + } + + version = latestVersion + fmt.Fprintf(os.Stdout, "Found latest version: %s\n", version) + } + + if IsExistingNodeVersion(version) { + fmt.Fprintf(os.Stderr, "Error: Node version %s already exists\n", version) + os.Exit(1) + } + fmt.Fprintf(os.Stdout, "Updating Quilibrium node for %s-%s, version: %s\n", OsType, Arch, version) // Update the node @@ -49,43 +64,5 @@ func updateNode(version string) { return } - // Create new binary version directory - versionDataDir := filepath.Join(utils.NodeDataPath, version) - if err := utils.ValidateAndCreateDir(versionDataDir, nil); err != nil { - fmt.Fprintf(os.Stderr, "Error creating data directory: %v\n", err) - return - } - - // Construct the expected filename for the specified version - // Remove 'v' prefix if present for filename construction - versionWithoutV := strings.TrimPrefix(version, "v") - - if IsExistingNodeVersion(versionWithoutV) { - fmt.Fprintf(os.Stderr, "Error: Node version %s already exists\n", versionWithoutV) - os.Exit(1) - } - - // Download the release directly - err := utils.DownloadRelease(utils.ReleaseTypeNode, versionWithoutV) - if err != nil { - fmt.Fprintf(os.Stderr, "Error downloading version %s: %v\n", version, err) - fmt.Fprintf(os.Stderr, "The specified version %s does not exist for %s-%s\n", version, OsType, Arch) - // Clean up the created directories since installation failed - os.RemoveAll(versionDataDir) - return - } - - // Download signature files - if err := utils.DownloadReleaseSignatures(utils.ReleaseTypeNode, versionWithoutV); err != nil { - fmt.Fprintf(os.Stderr, "Warning: Failed to download signature files: %v\n", err) - fmt.Fprintf(os.Stdout, "Continuing with installation...\n") - } - - // Ensure log rotation is set up - if err := setupLogRotation(); err != nil { - fmt.Fprintf(os.Stderr, "Warning: Failed to set up log rotation: %v\n", err) - } - - // Successfully downloaded the specific version - finishInstallation(version) + InstallNode(version) } diff --git a/client/cmd/version.go b/client/cmd/version.go index e2fe747..8d03beb 100644 --- a/client/cmd/version.go +++ b/client/cmd/version.go @@ -72,11 +72,13 @@ var versionCmd = &cobra.Command{ return } - fmt.Printf("qclient %s\n", info.Version) + fmt.Printf("%s\n", info.Version) - if info.SHA256 != "" && info.MD5 != "" { - fmt.Printf("SHA256: %s\n", info.SHA256) - fmt.Printf("MD5: %s\n", info.MD5) + if showChecksum { + if info.SHA256 != "" && info.MD5 != "" { + fmt.Printf("SHA256: %s\n", info.SHA256) + fmt.Printf("MD5: %s\n", info.MD5) + } } }, } diff --git a/client/utils/fileUtils.go b/client/utils/fileUtils.go index 61c076d..62887d3 100644 --- a/client/utils/fileUtils.go +++ b/client/utils/fileUtils.go @@ -13,9 +13,6 @@ import ( "runtime" ) -// DefaultNodeUser is the default user name for node operations -var DefaultNodeUser = "quilibrium" - var ClientInstallPath = filepath.Join("/opt/quilibrium/", string(ReleaseTypeQClient)) var RootQuilibriumPath = filepath.Join("/var/quilibrium/") var BinaryPath = filepath.Join(RootQuilibriumPath, "bin")