Skip to content

Commit

Permalink
feat: style error logs #52 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchiarulli authored Oct 4, 2024
1 parent 727d3a9 commit 5749bf8
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 46 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"os"
)
Expand All @@ -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)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/manager/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package manager
import (
"fmt"
"github.com/pterm/pterm"
"log"
"os"
"os/exec"
)

Expand All @@ -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)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/network/certbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/pterm/pterm"
"log"
"os"
"os/exec"
)

Expand Down Expand Up @@ -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)
}
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/network/firewall.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package network

import (
"fmt"
"github.com/pterm/pterm"
"log"
"os"
"os/exec"
)

Expand All @@ -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.")
Expand Down
14 changes: 10 additions & 4 deletions pkg/utils/directories/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package directories

import (
"fmt"
"github.com/pterm/pterm"
"io/fs"
"log"
"os"
"os/exec"
)
Expand All @@ -14,22 +14,28 @@ 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)
}
}

// Function to ensure directory and path to directory exists and sets permissions
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)
}
}

// Function to set owner and group of a directory
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)
}
}
46 changes: 34 additions & 12 deletions pkg/utils/files/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package files

import (
"fmt"
"github.com/pterm/pterm"
"io"
"io/fs"
"log"
"net/http"
"os"
"os/exec"
Expand All @@ -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)
}
}
}
Expand All @@ -32,31 +34,39 @@ 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)
}
}

// Function to set owner and group of a file
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)
}
}

// Function to set permissions of a file
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)
}
}

// Function to write content to a file
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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -75,33 +87,43 @@ 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)
}
}

// Function to extract a file
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)
}
}
8 changes: 6 additions & 2 deletions pkg/utils/git/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package git

import (
"fmt"
"github.com/pterm/pterm"
"io/fs"
"log"
"os"
"os/exec"
)

Expand All @@ -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)
}
}
15 changes: 11 additions & 4 deletions pkg/utils/plugins/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package plugins

import (
"log"
"fmt"
"github.com/pterm/pterm"
"os"
"text/template"
)
Expand All @@ -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)
}
}
Loading

0 comments on commit 5749bf8

Please sign in to comment.