diff --git a/cmd/root.go b/cmd/root.go index 54e7525..d64abfd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,8 @@ package cmd import ( + "fmt" + "github.com/pterm/pterm" "github.com/spf13/cobra" "os" ) @@ -17,6 +19,8 @@ helps install and configure your specified relay.`, func Execute() { err := rootCmd.Execute() if err != nil { + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to add child commands to the root command: %v", err)) os.Exit(1) } } diff --git a/pkg/manager/apt.go b/pkg/manager/apt.go index 8d38da4..1f19117 100644 --- a/pkg/manager/apt.go +++ b/pkg/manager/apt.go @@ -3,7 +3,7 @@ package manager import ( "fmt" "github.com/pterm/pterm" - "log" + "os" "os/exec" ) @@ -18,7 +18,8 @@ func IsPackageInstalled(packageName string) bool { if errorCode == 1 { return false } else { - log.Fatalf("Error checking if package is installed: %v", err) + pterm.Error.Println(fmt.Sprintf("Failed to check if package is installed: %v", err)) + os.Exit(1) } } } diff --git a/pkg/network/certbot.go b/pkg/network/certbot.go index c220e85..284544a 100644 --- a/pkg/network/certbot.go +++ b/pkg/network/certbot.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/nodetec/rwz/pkg/utils/files" "github.com/pterm/pterm" - "log" + "os" "os/exec" ) @@ -58,13 +58,15 @@ func GetCertificates(domainName string) bool { cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("/var/www/%s", domainName), "-d", domainName, "--agree-tos", "--no-eff-email", "-q", "--register-unsafely-without-email") err := cmd.Run() if err != nil { - log.Fatalf("Certbot failed to obtain the certificate for %s: %v", domainName, err) + pterm.Error.Println(fmt.Sprintf("Certbot failed to obtain the certificate for %s: %v", domainName, err)) + os.Exit(1) } } else { cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("/var/www/%s", domainName), "-d", domainName, "--email", email, "--agree-tos", "--no-eff-email", "-q") err := cmd.Run() if err != nil { - log.Fatalf("Certbot failed to obtain the certificate for %s: %v", domainName, err) + pterm.Error.Println(fmt.Sprintf("Certbot failed to obtain the certificate for %s: %v", domainName, err)) + os.Exit(1) } } diff --git a/pkg/network/firewall.go b/pkg/network/firewall.go index 015b20e..449010f 100644 --- a/pkg/network/firewall.go +++ b/pkg/network/firewall.go @@ -1,8 +1,9 @@ package network import ( + "fmt" "github.com/pterm/pterm" - "log" + "os" "os/exec" ) @@ -14,13 +15,17 @@ func ConfigureFirewall() { // Allow HTTP and HTTPS traffic err := exec.Command("ufw", "allow", "Nginx Full").Run() if err != nil { - log.Fatalf("Error allowing Nginx Full: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to allow Nginx Full: %v", err)) + os.Exit(1) } // Reload the firewall to apply the changes err = exec.Command("ufw", "reload").Run() if err != nil { - log.Fatalf("Error reloading firewall: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to reload firewall: %v", err)) + os.Exit(1) } spinner.Success("Firewall configured successfully.") diff --git a/pkg/utils/directories/utils.go b/pkg/utils/directories/utils.go index 7ce20f0..e90ffd3 100644 --- a/pkg/utils/directories/utils.go +++ b/pkg/utils/directories/utils.go @@ -2,8 +2,8 @@ package directories import ( "fmt" + "github.com/pterm/pterm" "io/fs" - "log" "os" "os/exec" ) @@ -14,7 +14,9 @@ type FileMode = fs.FileMode func RemoveDirectory(path string) { err := os.RemoveAll(path) if err != nil && !os.IsNotExist(err) { - log.Fatalf("Error removing %s directory: %v", path, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to remove %s directory: %v", path, err)) + os.Exit(1) } } @@ -22,7 +24,9 @@ func RemoveDirectory(path string) { func CreateDirectory(path string, permissions FileMode) { err := os.MkdirAll(path, permissions) if err != nil { - log.Fatalf("Error creating %s directory: %v", path, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to create %s directory: %v", path, err)) + os.Exit(1) } } @@ -30,6 +34,8 @@ func CreateDirectory(path string, permissions FileMode) { func SetOwnerAndGroup(owner, group, dir string) { err := exec.Command("chown", "-R", fmt.Sprintf("%s:%s", owner, group), dir).Run() if err != nil { - log.Fatalf("Error setting ownership of the %s directory: %v", dir, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to set ownership of the %s directory: %v", dir, err)) + os.Exit(1) } } diff --git a/pkg/utils/files/utils.go b/pkg/utils/files/utils.go index a7e97d2..3a181dd 100644 --- a/pkg/utils/files/utils.go +++ b/pkg/utils/files/utils.go @@ -2,9 +2,9 @@ package files import ( "fmt" + "github.com/pterm/pterm" "io" "io/fs" - "log" "net/http" "os" "os/exec" @@ -23,7 +23,9 @@ func RemoveFile(path string) { if _, err := os.Stat(path); err == nil { err = os.Remove(path) if err != nil { - log.Fatalf("Error removing %s file: %v", path, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to remove %s file: %v", path, err)) + os.Exit(1) } } } @@ -32,7 +34,9 @@ func RemoveFile(path string) { func CopyFile(fileToCopy, destDir string) { err := exec.Command("cp", fileToCopy, destDir).Run() if err != nil { - log.Fatalf("Error copying %s file: %v", fileToCopy, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to copy %s file: %v", fileToCopy, err)) + os.Exit(1) } } @@ -40,7 +44,9 @@ func CopyFile(fileToCopy, destDir string) { func SetOwnerAndGroup(owner, group, file string) { err := exec.Command("chown", fmt.Sprintf("%s:%s", owner, group), file).Run() if err != nil { - log.Fatalf("Error setting ownership of %s file: %v", file, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to set ownership of %s file: %v", file, err)) + os.Exit(1) } } @@ -48,7 +54,9 @@ func SetOwnerAndGroup(owner, group, file string) { func SetPermissions(path string, mode FileMode) { err := os.Chmod(path, mode) if err != nil { - log.Fatalf("Error setting %s file permissions: %v", path, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to set %s file permissions: %v", path, err)) + os.Exit(1) } } @@ -56,7 +64,9 @@ func SetPermissions(path string, mode FileMode) { func WriteFile(path, content string, permissions FileMode) { err := os.WriteFile(path, []byte(content), permissions) if err != nil { - log.Fatalf("Error writing content to %s file: %v", path, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to write content to %s file: %v", path, err)) + os.Exit(1) } } @@ -66,7 +76,9 @@ func InPlaceEdit(command, path string) { // Execute the command if err := cmd.Run(); err != nil { - log.Fatalf("Error editing %s in-place: %v", path, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to edit %s file in-place: %v", path, err)) + os.Exit(1) } } @@ -75,26 +87,34 @@ func DownloadAndCopyFile(tmpFilePath, downloadURL string) { // Create a temporary file out, err := os.Create(tmpFilePath) if err != nil { - log.Fatalf("Error creating %s file: %v", tmpFilePath, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to create %s file: %v", tmpFilePath, err)) + os.Exit(1) } defer out.Close() // Download the file resp, err := http.Get(downloadURL) if err != nil { - log.Fatalf("Error downloading file: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to download file: %v", err)) + os.Exit(1) } defer resp.Body.Close() // Check server response if resp.StatusCode != http.StatusOK { - log.Fatalf("Bad status: %s", resp.Status) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Bad repsonse status code: %s", resp.Status)) + os.Exit(1) } // Write the body to the temporary file _, err = io.Copy(out, resp.Body) if err != nil { - log.Fatalf("Error writing to temporary file: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to write to temporary file: %v", err)) + os.Exit(1) } } @@ -102,6 +122,8 @@ func DownloadAndCopyFile(tmpFilePath, downloadURL string) { func ExtractFile(tmpFilePath, destDir string) { err := exec.Command("tar", "-xf", tmpFilePath, "-C", destDir).Run() if err != nil { - log.Fatalf("Error extracting binary to %s: %v", destDir, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to extract binary to %s: %v", destDir, err)) + os.Exit(1) } } diff --git a/pkg/utils/git/utils.go b/pkg/utils/git/utils.go index d94378b..6886679 100644 --- a/pkg/utils/git/utils.go +++ b/pkg/utils/git/utils.go @@ -1,8 +1,10 @@ package git import ( + "fmt" + "github.com/pterm/pterm" "io/fs" - "log" + "os" "os/exec" ) @@ -12,6 +14,8 @@ type FileMode = fs.FileMode func Clone(branch, url, destDir string) { err := exec.Command("git", "clone", "-b", branch, url, destDir).Run() if err != nil { - log.Fatalf("Error downloading repository: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to download repository: %v", err)) + os.Exit(1) } } diff --git a/pkg/utils/plugins/utils.go b/pkg/utils/plugins/utils.go index 829b48e..c0371cd 100644 --- a/pkg/utils/plugins/utils.go +++ b/pkg/utils/plugins/utils.go @@ -1,7 +1,8 @@ package plugins import ( - "log" + "fmt" + "github.com/pterm/pterm" "os" "text/template" ) @@ -14,17 +15,23 @@ type PluginFileParams struct { func CreatePluginFile(pluginFilePath, pluginTemplate string, pluginFileParams *PluginFileParams) { pluginFile, err := os.Create(pluginFilePath) if err != nil { - log.Fatalf("Error creating plugin file: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to create plugin file: %v", err)) + os.Exit(1) } defer pluginFile.Close() pluginTmpl, err := template.New("plugin").Parse(pluginTemplate) if err != nil { - log.Fatalf("Error parsing plugin template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to parse plugin template: %v", err)) + os.Exit(1) } err = pluginTmpl.Execute(pluginFile, struct{ Domain, RelaySecretKey string }{Domain: pluginFileParams.Domain, RelaySecretKey: pluginFileParams.RelaySecretKey}) if err != nil { - log.Fatalf("Error executing plugin template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to execute plugin template: %v", err)) + os.Exit(1) } } diff --git a/pkg/utils/systemd/utils.go b/pkg/utils/systemd/utils.go index e5c7b9c..41bcf01 100644 --- a/pkg/utils/systemd/utils.go +++ b/pkg/utils/systemd/utils.go @@ -1,6 +1,8 @@ package systemd import ( + "fmt" + "github.com/pterm/pterm" "log" "os" "os/exec" @@ -18,13 +20,17 @@ type EnvFileParams struct { func CreateEnvFile(envFilePath, envTemplate string, envFileParams *EnvFileParams) { envFile, err := os.Create(envFilePath) if err != nil { - log.Fatalf("Error creating environment file: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to create environment file: %v", err)) + os.Exit(1) } defer envFile.Close() envTmpl, err := template.New("env").Parse(envTemplate) if err != nil { - log.Fatalf("Error parsing environment template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to parse environment template: %v", err)) + os.Exit(1) } var WSProtocol string @@ -36,25 +42,33 @@ func CreateEnvFile(envFilePath, envTemplate string, envFileParams *EnvFileParams err = envTmpl.Execute(envFile, struct{ Domain, WSProtocol, PrivKey, PubKey, RelayContact string }{Domain: envFileParams.Domain, WSProtocol: WSProtocol, PrivKey: envFileParams.PrivKey, PubKey: envFileParams.PubKey, RelayContact: envFileParams.RelayContact}) if err != nil { - log.Fatalf("Error executing environment template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to execute environment template: %v", err)) + os.Exit(1) } } func CreateServiceFile(serviceFilePath, serviceTemplate string) { serviceFile, err := os.Create(serviceFilePath) if err != nil { - log.Fatalf("Error creating service file: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to create service file: %v", err)) + os.Exit(1) } defer serviceFile.Close() tmpl, err := template.New("service").Parse(serviceTemplate) if err != nil { - log.Fatalf("Error parsing service template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to parse service template: %v", err)) + os.Exit(1) } err = tmpl.Execute(serviceFile, struct{}{}) if err != nil { - log.Fatalf("Error executing service template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to execute service template: %v", err)) + os.Exit(1) } } @@ -62,33 +76,44 @@ func Reload() { err := exec.Command("systemctl", "daemon-reload").Run() if err != nil { log.Fatalf("Error reloading systemd daemon: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to execute service template: %v", err)) + os.Exit(1) } } func EnableService(name string) { err := exec.Command("systemctl", "enable", name).Run() if err != nil { - log.Fatalf("Error enabling %s service: %v", name, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to enable %s service: %v", name, err)) + os.Exit(1) } } func StartService(name string) { err := exec.Command("systemctl", "start", name).Run() if err != nil { - log.Fatalf("Error starting %s service: %v", name, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to start %s service: %v", name, err)) + os.Exit(1) } } func ReloadService(name string) { err := exec.Command("systemctl", "reload", name).Run() if err != nil { - log.Fatalf("Error reloading %s service: %v", name, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to reload %s service: %v", name, err)) + os.Exit(1) } } func RestartService(name string) { err := exec.Command("systemctl", "restart", name).Run() if err != nil { - log.Fatalf("Error restarting %s service: %v", name, err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to restart %s service: %v", name, err)) + os.Exit(1) } } diff --git a/pkg/utils/templates/utils.go b/pkg/utils/templates/utils.go index e3d2831..fde8fe7 100644 --- a/pkg/utils/templates/utils.go +++ b/pkg/utils/templates/utils.go @@ -1,7 +1,8 @@ package templates import ( - "log" + "fmt" + "github.com/pterm/pterm" "os" "text/template" ) @@ -15,13 +16,17 @@ type IndexFileParams struct { func CreateIndexFile(indexFilePath, indexTemplate string, indexFileParams *IndexFileParams) { indexFile, err := os.Create(indexFilePath) if err != nil { - log.Fatalf("Error creating index.html file: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to create index.html file: %v", err)) + os.Exit(1) } defer indexFile.Close() indexTmpl, err := template.New("index").Parse(indexTemplate) if err != nil { - log.Fatalf("Error parsing index.html template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to parse index.html template: %v", err)) + os.Exit(1) } var HTTPProtocol string @@ -33,6 +38,8 @@ func CreateIndexFile(indexFilePath, indexTemplate string, indexFileParams *Index err = indexTmpl.Execute(indexFile, struct{ Domain, HTTPProtocol, PubKey string }{Domain: indexFileParams.Domain, HTTPProtocol: HTTPProtocol, PubKey: indexFileParams.PubKey}) if err != nil { - log.Fatalf("Error executing index.html template: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to execute index.html template: %v", err)) + os.Exit(1) } } diff --git a/pkg/utils/users/utils.go b/pkg/utils/users/utils.go index 554c330..4ce8542 100644 --- a/pkg/utils/users/utils.go +++ b/pkg/utils/users/utils.go @@ -1,7 +1,9 @@ package users import ( - "log" + "fmt" + "github.com/pterm/pterm" + "os" "os/exec" ) @@ -15,7 +17,9 @@ func CreateUser(username string, disableLogin bool) { if disableLogin { err := exec.Command("adduser", "--disabled-login", "--gecos", "", username).Run() if err != nil { - log.Fatalf("Error creating user: %v", err) + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to create user: %v", err)) + os.Exit(1) } } }