Skip to content

Commit 5749bf8

Browse files
authored
feat: style error logs #52 (#57)
1 parent 727d3a9 commit 5749bf8

File tree

11 files changed

+133
-46
lines changed

11 files changed

+133
-46
lines changed

cmd/root.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
"github.com/pterm/pterm"
46
"github.com/spf13/cobra"
57
"os"
68
)
@@ -17,6 +19,8 @@ helps install and configure your specified relay.`,
1719
func Execute() {
1820
err := rootCmd.Execute()
1921
if err != nil {
22+
pterm.Println()
23+
pterm.Error.Println(fmt.Sprintf("Failed to add child commands to the root command: %v", err))
2024
os.Exit(1)
2125
}
2226
}

pkg/manager/apt.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package manager
33
import (
44
"fmt"
55
"github.com/pterm/pterm"
6-
"log"
6+
"os"
77
"os/exec"
88
)
99

@@ -18,7 +18,8 @@ func IsPackageInstalled(packageName string) bool {
1818
if errorCode == 1 {
1919
return false
2020
} else {
21-
log.Fatalf("Error checking if package is installed: %v", err)
21+
pterm.Error.Println(fmt.Sprintf("Failed to check if package is installed: %v", err))
22+
os.Exit(1)
2223
}
2324
}
2425
}

pkg/network/certbot.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"github.com/nodetec/rwz/pkg/utils/files"
66
"github.com/pterm/pterm"
7-
"log"
7+
"os"
88
"os/exec"
99
)
1010

@@ -58,13 +58,15 @@ func GetCertificates(domainName string) bool {
5858
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")
5959
err := cmd.Run()
6060
if err != nil {
61-
log.Fatalf("Certbot failed to obtain the certificate for %s: %v", domainName, err)
61+
pterm.Error.Println(fmt.Sprintf("Certbot failed to obtain the certificate for %s: %v", domainName, err))
62+
os.Exit(1)
6263
}
6364
} else {
6465
cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("/var/www/%s", domainName), "-d", domainName, "--email", email, "--agree-tos", "--no-eff-email", "-q")
6566
err := cmd.Run()
6667
if err != nil {
67-
log.Fatalf("Certbot failed to obtain the certificate for %s: %v", domainName, err)
68+
pterm.Error.Println(fmt.Sprintf("Certbot failed to obtain the certificate for %s: %v", domainName, err))
69+
os.Exit(1)
6870
}
6971
}
7072

pkg/network/firewall.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package network
22

33
import (
4+
"fmt"
45
"github.com/pterm/pterm"
5-
"log"
6+
"os"
67
"os/exec"
78
)
89

@@ -14,13 +15,17 @@ func ConfigureFirewall() {
1415
// Allow HTTP and HTTPS traffic
1516
err := exec.Command("ufw", "allow", "Nginx Full").Run()
1617
if err != nil {
17-
log.Fatalf("Error allowing Nginx Full: %v", err)
18+
pterm.Println()
19+
pterm.Error.Println(fmt.Sprintf("Failed to allow Nginx Full: %v", err))
20+
os.Exit(1)
1821
}
1922

2023
// Reload the firewall to apply the changes
2124
err = exec.Command("ufw", "reload").Run()
2225
if err != nil {
23-
log.Fatalf("Error reloading firewall: %v", err)
26+
pterm.Println()
27+
pterm.Error.Println(fmt.Sprintf("Failed to reload firewall: %v", err))
28+
os.Exit(1)
2429
}
2530

2631
spinner.Success("Firewall configured successfully.")

pkg/utils/directories/utils.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package directories
22

33
import (
44
"fmt"
5+
"github.com/pterm/pterm"
56
"io/fs"
6-
"log"
77
"os"
88
"os/exec"
99
)
@@ -14,22 +14,28 @@ type FileMode = fs.FileMode
1414
func RemoveDirectory(path string) {
1515
err := os.RemoveAll(path)
1616
if err != nil && !os.IsNotExist(err) {
17-
log.Fatalf("Error removing %s directory: %v", path, err)
17+
pterm.Println()
18+
pterm.Error.Println(fmt.Sprintf("Failed to remove %s directory: %v", path, err))
19+
os.Exit(1)
1820
}
1921
}
2022

2123
// Function to ensure directory and path to directory exists and sets permissions
2224
func CreateDirectory(path string, permissions FileMode) {
2325
err := os.MkdirAll(path, permissions)
2426
if err != nil {
25-
log.Fatalf("Error creating %s directory: %v", path, err)
27+
pterm.Println()
28+
pterm.Error.Println(fmt.Sprintf("Failed to create %s directory: %v", path, err))
29+
os.Exit(1)
2630
}
2731
}
2832

2933
// Function to set owner and group of a directory
3034
func SetOwnerAndGroup(owner, group, dir string) {
3135
err := exec.Command("chown", "-R", fmt.Sprintf("%s:%s", owner, group), dir).Run()
3236
if err != nil {
33-
log.Fatalf("Error setting ownership of the %s directory: %v", dir, err)
37+
pterm.Println()
38+
pterm.Error.Println(fmt.Sprintf("Failed to set ownership of the %s directory: %v", dir, err))
39+
os.Exit(1)
3440
}
3541
}

pkg/utils/files/utils.go

+34-12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package files
22

33
import (
44
"fmt"
5+
"github.com/pterm/pterm"
56
"io"
67
"io/fs"
7-
"log"
88
"net/http"
99
"os"
1010
"os/exec"
@@ -23,7 +23,9 @@ func RemoveFile(path string) {
2323
if _, err := os.Stat(path); err == nil {
2424
err = os.Remove(path)
2525
if err != nil {
26-
log.Fatalf("Error removing %s file: %v", path, err)
26+
pterm.Println()
27+
pterm.Error.Println(fmt.Sprintf("Failed to remove %s file: %v", path, err))
28+
os.Exit(1)
2729
}
2830
}
2931
}
@@ -32,31 +34,39 @@ func RemoveFile(path string) {
3234
func CopyFile(fileToCopy, destDir string) {
3335
err := exec.Command("cp", fileToCopy, destDir).Run()
3436
if err != nil {
35-
log.Fatalf("Error copying %s file: %v", fileToCopy, err)
37+
pterm.Println()
38+
pterm.Error.Println(fmt.Sprintf("Failed to copy %s file: %v", fileToCopy, err))
39+
os.Exit(1)
3640
}
3741
}
3842

3943
// Function to set owner and group of a file
4044
func SetOwnerAndGroup(owner, group, file string) {
4145
err := exec.Command("chown", fmt.Sprintf("%s:%s", owner, group), file).Run()
4246
if err != nil {
43-
log.Fatalf("Error setting ownership of %s file: %v", file, err)
47+
pterm.Println()
48+
pterm.Error.Println(fmt.Sprintf("Failed to set ownership of %s file: %v", file, err))
49+
os.Exit(1)
4450
}
4551
}
4652

4753
// Function to set permissions of a file
4854
func SetPermissions(path string, mode FileMode) {
4955
err := os.Chmod(path, mode)
5056
if err != nil {
51-
log.Fatalf("Error setting %s file permissions: %v", path, err)
57+
pterm.Println()
58+
pterm.Error.Println(fmt.Sprintf("Failed to set %s file permissions: %v", path, err))
59+
os.Exit(1)
5260
}
5361
}
5462

5563
// Function to write content to a file
5664
func WriteFile(path, content string, permissions FileMode) {
5765
err := os.WriteFile(path, []byte(content), permissions)
5866
if err != nil {
59-
log.Fatalf("Error writing content to %s file: %v", path, err)
67+
pterm.Println()
68+
pterm.Error.Println(fmt.Sprintf("Failed to write content to %s file: %v", path, err))
69+
os.Exit(1)
6070
}
6171
}
6272

@@ -66,7 +76,9 @@ func InPlaceEdit(command, path string) {
6676

6777
// Execute the command
6878
if err := cmd.Run(); err != nil {
69-
log.Fatalf("Error editing %s in-place: %v", path, err)
79+
pterm.Println()
80+
pterm.Error.Println(fmt.Sprintf("Failed to edit %s file in-place: %v", path, err))
81+
os.Exit(1)
7082
}
7183
}
7284

@@ -75,33 +87,43 @@ func DownloadAndCopyFile(tmpFilePath, downloadURL string) {
7587
// Create a temporary file
7688
out, err := os.Create(tmpFilePath)
7789
if err != nil {
78-
log.Fatalf("Error creating %s file: %v", tmpFilePath, err)
90+
pterm.Println()
91+
pterm.Error.Println(fmt.Sprintf("Failed to create %s file: %v", tmpFilePath, err))
92+
os.Exit(1)
7993
}
8094
defer out.Close()
8195

8296
// Download the file
8397
resp, err := http.Get(downloadURL)
8498
if err != nil {
85-
log.Fatalf("Error downloading file: %v", err)
99+
pterm.Println()
100+
pterm.Error.Println(fmt.Sprintf("Failed to download file: %v", err))
101+
os.Exit(1)
86102
}
87103
defer resp.Body.Close()
88104

89105
// Check server response
90106
if resp.StatusCode != http.StatusOK {
91-
log.Fatalf("Bad status: %s", resp.Status)
107+
pterm.Println()
108+
pterm.Error.Println(fmt.Sprintf("Bad repsonse status code: %s", resp.Status))
109+
os.Exit(1)
92110
}
93111

94112
// Write the body to the temporary file
95113
_, err = io.Copy(out, resp.Body)
96114
if err != nil {
97-
log.Fatalf("Error writing to temporary file: %v", err)
115+
pterm.Println()
116+
pterm.Error.Println(fmt.Sprintf("Failed to write to temporary file: %v", err))
117+
os.Exit(1)
98118
}
99119
}
100120

101121
// Function to extract a file
102122
func ExtractFile(tmpFilePath, destDir string) {
103123
err := exec.Command("tar", "-xf", tmpFilePath, "-C", destDir).Run()
104124
if err != nil {
105-
log.Fatalf("Error extracting binary to %s: %v", destDir, err)
125+
pterm.Println()
126+
pterm.Error.Println(fmt.Sprintf("Failed to extract binary to %s: %v", destDir, err))
127+
os.Exit(1)
106128
}
107129
}

pkg/utils/git/utils.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package git
22

33
import (
4+
"fmt"
5+
"github.com/pterm/pterm"
46
"io/fs"
5-
"log"
7+
"os"
68
"os/exec"
79
)
810

@@ -12,6 +14,8 @@ type FileMode = fs.FileMode
1214
func Clone(branch, url, destDir string) {
1315
err := exec.Command("git", "clone", "-b", branch, url, destDir).Run()
1416
if err != nil {
15-
log.Fatalf("Error downloading repository: %v", err)
17+
pterm.Println()
18+
pterm.Error.Println(fmt.Sprintf("Failed to download repository: %v", err))
19+
os.Exit(1)
1620
}
1721
}

pkg/utils/plugins/utils.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package plugins
22

33
import (
4-
"log"
4+
"fmt"
5+
"github.com/pterm/pterm"
56
"os"
67
"text/template"
78
)
@@ -14,17 +15,23 @@ type PluginFileParams struct {
1415
func CreatePluginFile(pluginFilePath, pluginTemplate string, pluginFileParams *PluginFileParams) {
1516
pluginFile, err := os.Create(pluginFilePath)
1617
if err != nil {
17-
log.Fatalf("Error creating plugin file: %v", err)
18+
pterm.Println()
19+
pterm.Error.Println(fmt.Sprintf("Failed to create plugin file: %v", err))
20+
os.Exit(1)
1821
}
1922
defer pluginFile.Close()
2023

2124
pluginTmpl, err := template.New("plugin").Parse(pluginTemplate)
2225
if err != nil {
23-
log.Fatalf("Error parsing plugin template: %v", err)
26+
pterm.Println()
27+
pterm.Error.Println(fmt.Sprintf("Failed to parse plugin template: %v", err))
28+
os.Exit(1)
2429
}
2530

2631
err = pluginTmpl.Execute(pluginFile, struct{ Domain, RelaySecretKey string }{Domain: pluginFileParams.Domain, RelaySecretKey: pluginFileParams.RelaySecretKey})
2732
if err != nil {
28-
log.Fatalf("Error executing plugin template: %v", err)
33+
pterm.Println()
34+
pterm.Error.Println(fmt.Sprintf("Failed to execute plugin template: %v", err))
35+
os.Exit(1)
2936
}
3037
}

0 commit comments

Comments
 (0)