From 29149e9a6782c452074b412926e4f8e3a0d245ad Mon Sep 17 00:00:00 2001 From: Saahil Bhavsar Date: Sun, 7 Apr 2024 22:36:53 +0530 Subject: [PATCH] Modifies configuration according to MacOS Signed-off-by: Saahil Bhavsar --- cli/cmd/configure.go | 48 ++++++++++++++++++++++++++++---- scripts/configure-spok-server.sh | 44 +++++++++++++++++++++-------- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/cli/cmd/configure.go b/cli/cmd/configure.go index adcec39..395032b 100644 --- a/cli/cmd/configure.go +++ b/cli/cmd/configure.go @@ -60,18 +60,53 @@ var configureCmd = &cobra.Command{ for i, dir := range configDirs { configPath = dir + "/scripts/configure-spok-server.sh" - + _, err := os.Stat(configPath) if err == nil { break - } else if i == len(configDirs) - 1 { + } else if i == len(configDirs)-1 { log.Fatal("Configuration script not found.") } } fmt.Println("Configuration script found at:", configPath) - serverConfigPath := "/root/configure-spok-server.sh" + var serverConfigPath string + + var osCommand *exec.Cmd + + if privateKeyAuth && privateKeyPath != "" { + osCommand = exec.Command( + "ssh", + "-i", + privateKeyPath, + userArgs, + "uname", + "-o", + ) + } else { + osCommand = exec.Command( + "ssh", + userArgs, + "uname", + "-o", + ) + } + + output, err := osCommand.Output() + cobra.CheckErr(err) + + if strings.Contains(string(output), "Linux") { + fmt.Println("Remote platform detected: Linux") + + serverConfigPath = "/root/configure-spok-server.sh" + } else if strings.Contains(string(output), "Darwin") { + fmt.Println("Remote platform detected: Mac") + + serverConfigPath = "/var/root/configure-spok-server.sh" + } else { + log.Fatal("Unsupported remote platform.") + } var scpCommandScript []string var sshCommandChmod []string @@ -131,9 +166,12 @@ var configureCmd = &cobra.Command{ scpCommandScript[0], scpCommandScript[1:]..., ) - fmt.Println("Copying configuration script to server...") + fmt.Printf( + "Copying configuration script to server at %s ...\n", + serverConfigPath, + ) - err := scpCmdScript.Run() + err = scpCmdScript.Run() if err != nil { log.Fatal(err) } diff --git a/scripts/configure-spok-server.sh b/scripts/configure-spok-server.sh index 53d34ed..452a56f 100644 --- a/scripts/configure-spok-server.sh +++ b/scripts/configure-spok-server.sh @@ -14,20 +14,31 @@ if [ -z "${EMAIL}" ] || [ -z "${USER}" ]; then exit 1 fi -if sudo -nl &> /dev/null || $(id -u) -ne 0; then +if sudo -nl &> /dev/null || [ "$(id -u)" -ne 0 ]; then echo "User has sudo privileges without a password." + # Check for wget or curl availability + DOWNLOADER="" + if command -v wget &> /dev/null; then + DOWNLOADER="wget" + elif command -v curl &> /dev/null; then + DOWNLOADER="curl -LO" + else + echo "Neither wget nor curl is available. Please install either of them." + exit 1 + fi + if [[ "$OS_VAL" == *"Linux"* && "$ARCH" == *"x86_64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_amd64 + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_amd64 mv verifier_${VERSION}_linux_amd64 verifier elif [[ "$OS_VAL" == *"Linux"* && "$ARCH" == *"arm64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_arm64 + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_arm64 mv verifier_${VERSION}_linux_arm64 verifier elif [[ "$OS_VAL" == *"Darwin"* && "$ARCH" == *"x86_64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_amd64 + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_amd64 mv verifier_${VERSION}_darwin_amd64 verifier - elif [[ "$OS_VAL" == *"Darwin"* && "$ARCH" == *"arm64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_arm64 + elif [[ "$OS_VAL" == *"Darwin"* && "$ARCH" == *"arm64"* ]]; then + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_arm64 mv verifier_${VERSION}_darwin_arm64 verifier else echo "This OS: $OS_VAL and ARCH: $ARCH is not supported please contact the developers for help :)" @@ -66,17 +77,28 @@ else mkdir ~/.spok + # Check for wget or curl availability + DOWNLOADER="" + if command -v wget &> /dev/null; then + DOWNLOADER="wget" + elif command -v curl &> /dev/null; then + DOWNLOADER="curl -LO" + else + echo "Neither wget nor curl is available. Please install either of them." + exit 1 + fi + if [[ "$OS_VAL" == *"Linux"* && "$ARCH" == *"x86_64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_amd64 + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_amd64 mv verifier_${VERSION}_linux_amd64 verifier elif [[ "$OS_VAL" == *"Linux"* && "$ARCH" == *"arm64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_arm64 + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_linux_arm64 mv verifier_${VERSION}_linux_arm64 verifier elif [[ "$OS_VAL" == *"Darwin"* && "$ARCH" == *"x86_64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_amd64 + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_amd64 mv verifier_${VERSION}_darwin_amd64 verifier - elif [[ "$OS_VAL" == *"Darwin"* && "$ARCH" == *"arm64"* ]]; then - wget https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_arm64 + elif [[ "$OS_VAL" == *"Darwin"* && "$ARCH" == *"arm64"* ]]; then + $DOWNLOADER https://github.com/devlup-labs/spok/releases/download/${VERSION}/verifier_${VERSION}_darwin_arm64 mv verifier_${VERSION}_darwin_arm64 verifier else echo "This OS: $OS_VAL and ARCH: $ARCH is not supported please contact the developers for help :)"