-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapt.go
70 lines (56 loc) · 1.9 KB
/
apt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package manager
import (
"fmt"
"os"
"os/exec"
"github.com/nodetec/rwz/pkg/relays/nostr_rs_relay"
"github.com/nodetec/rwz/pkg/relays/strfry"
"github.com/nodetec/rwz/pkg/relays/strfry29"
"github.com/nodetec/rwz/pkg/relays/wot_relay"
"github.com/pterm/pterm"
)
// Function to check if a package is installed
func IsPackageInstalled(packageName string) bool {
out, err := exec.Command("dpkg-query", "-W", "-f='${Status}'", packageName).Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
errorCode := exitError.ExitCode()
// Package not found
if errorCode == 1 {
return false
} else {
pterm.Error.Println(fmt.Sprintf("Failed to check if package is installed: %v", err))
os.Exit(1)
}
}
}
status := string(out)
if status == "'unknown ok not-installed'" {
return false
} else if status == "'install ok installed'" {
return true
}
return false
}
// Function to install necessary packages
func AptInstallPackages(selectedRelayOption string) {
spinner, _ := pterm.DefaultSpinner.Start("Updating and installing packages...")
exec.Command("apt", "update", "-qq").Run()
packages := []string{"ufw", "fail2ban", "nginx", "certbot", "python3-certbot-nginx"}
if selectedRelayOption == nostr_rs_relay.RelayName || selectedRelayOption == strfry.RelayName || selectedRelayOption == wot_relay.RelayName || selectedRelayOption == strfry29.RelayName {
packages = append(packages, "git")
}
if selectedRelayOption == nostr_rs_relay.RelayName {
packages = append(packages, "sqlite3", "libsqlite3-dev")
}
// Check if package is installed, install if not
for _, p := range packages {
if IsPackageInstalled(p) {
spinner.UpdateText(fmt.Sprintf("%s is already installed.", p))
} else {
spinner.UpdateText(fmt.Sprintf("Installing %s...", p))
exec.Command("apt", "install", "-y", "-qq", p).Run()
}
}
spinner.Success("Packages updated and installed successfully.")
}