ceremonyclient/client/cmd/node/nodeconfig/create.go
Tyler Sturos 9cfbdef12c
Feat/2.1 qclient refactor and node install (#429)
* initial auto-update

* working link, update, and testing docker container and scripts

* refactor packages/folders

* move files to proper folders

* fix typos

Closes #421

* optimize rpm imports

* optimize channel imports

* Refactor split command to allow testing of split operations

Closes #338

* modify split and test for folder changes

* remove alias

* fix docker warning about FROM and AS being in different letter case

Closes #422

* QClient Account Command

* Display transaction details and confirmation prompts for transfer and merge commands

* build qclient docker improvements

* update build args for mpfr.so.6

* update install and node commands

* remove NodeConfig check for qclient node commands

* udpate

* working node commands

* update commands

* move utils and rename package

---------

Co-authored-by: Vasyl Tretiakov <vasyl.tretiakov@gmail.com>
Co-authored-by: littleblackcloud <163544315+littleblackcloud@users.noreply.github.com>
Co-authored-by: 0xOzgur <29779769+0xOzgur@users.noreply.github.com>
Co-authored-by: Cassandra Heart <7929478+CassOnMars@users.noreply.github.com>
2025-04-11 21:43:20 -05:00

102 lines
3.3 KiB
Go

package nodeconfig
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/spf13/cobra"
"source.quilibrium.com/quilibrium/monorepo/client/utils"
)
var createCmd = &cobra.Command{
Use: "create [name]",
Short: "Create a default configuration file set for a node",
Long: fmt.Sprintf(`Create a default configuration by running quilibrium-node with --peer-id and --config flags, then symlink it to the default configuration.
Example:
qclient node config create
qclient node config create myconfig
qclient node config create myconfig --default
The first example will create a new configuration at %s/default-config.
The second example will create a new configuration at %s/myconfig.
The third example will create a new configuration at %s/myconfig and symlink it so the node will use it.`,
ConfigDirs, ConfigDirs, ConfigDirs),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Determine the config name (default-config or user-provided)
var configName string
if len(args) > 0 {
configName = args[0]
} else {
// Prompt for a name if none provided
fmt.Print("Enter a name for the configuration (cannot be 'default'): ")
fmt.Scanln(&configName)
if configName == "" {
configName = "default-config"
}
}
// Check if trying to use "default" which is reserved for the symlink
if configName == "default" {
fmt.Println("Error: 'default' is reserved for the symlink. Please use a different name.")
os.Exit(1)
}
// Construct the configuration directory path
configDir := filepath.Join(ConfigDirs, configName)
// Create directory if it doesn't exist
if err := utils.ValidateAndCreateDir(configDir, NodeUser); err != nil {
fmt.Printf("Failed to create config directory: %s\n", err)
os.Exit(1)
}
// Run quilibrium-node command to generate config
// this is a hack to get the config files to be created
// TODO: fix this
// to fix this, we need to extrapolate the Node's config and keystore construction
// and reuse it for this command
nodeCmd := exec.Command("quilibrium-node", "--peer-id", "--config", configDir)
nodeCmd.Stdout = os.Stdout
nodeCmd.Stderr = os.Stderr
fmt.Printf("Running quilibrium-node to generate configuration in %s...\n", configName)
if err := nodeCmd.Run(); err != nil {
// Check if the error is due to the command not being found
if exitErr, ok := err.(*exec.ExitError); ok {
fmt.Printf("Error running quilibrium-node: %s\n", exitErr)
} else if _, ok := err.(*exec.Error); ok {
fmt.Printf("Error: quilibrium-node command not found. Please ensure it is installed and in your PATH.\n")
} else {
fmt.Printf("Error: %s\n", err)
}
os.RemoveAll(configDir)
os.Exit(1)
}
// Check if the configuration was created successfully
if !HasConfigFiles(configDir) {
fmt.Printf("Failed to generate configuration files in: %s\n", configDir)
os.Exit(1)
}
if SetDefault {
// Create the symlink
if err := utils.CreateSymlink(configDir, NodeConfigToRun); err != nil {
fmt.Printf("Failed to create symlink: %s\n", err)
os.Exit(1)
}
fmt.Printf("Successfully created %s configuration and symlinked to default\n", configName)
} else {
fmt.Printf("Successfully created %s configuration\n", configName)
}
fmt.Println("The keys.yml file will only contain 'null:' until the node is started.")
},
}