-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.go
More file actions
110 lines (96 loc) · 2.45 KB
/
utilities.go
File metadata and controls
110 lines (96 loc) · 2.45 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"os"
"os/exec"
"strings"
mnt "github.com/moby/sys/mountinfo"
"github.com/rwinkhart/go-boilerplate/front"
"github.com/rwinkhart/go-boilerplate/other"
)
func manageService(action, service string) {
chrootCommandRun([]string{"systemctl", "daemon-reload"})
chrootCommandRun([]string{"systemctl", action, service})
}
func managePackages(action string, targetPackages []string) {
chrootCommandRun(append([]string{"pacman", action, "--noconfirm"}, targetPackages...))
}
func writeFile(path, data, owner string, perms os.FileMode) {
err := os.WriteFile(path, []byte(data), perms)
if err != nil {
other.PrintError("Failed to write to "+path+":"+err.Error(), 1)
}
if owner != "root" {
err = os.Chown(path, 1000, 1000)
if err != nil {
other.PrintError("Failed to set ownership of "+path, 1)
}
}
}
func getUsername() string {
if username == "" {
username = front.Input("Non-root username:")
}
localUsername := username
return localUsername
}
func partitionIsMounted() bool {
if partitionR == "/" {
return true
}
isMounted, err := mnt.Mounted(mountPoint)
if err != nil {
other.PrintError("Failed to verify whether "+partitionR+" is mounted", 1)
}
return isMounted
}
func mountPartition() {
if partitionR == "/" {
return
}
if !partitionIsMounted() {
// root
cmd := exec.Command("mount", partitionR, mountPoint)
err := cmd.Run()
if err != nil {
other.PrintError("Failed to mount "+partitionR+" on "+mountPoint, 1)
}
// boot
cmd = exec.Command("mount", partitionB, mountPoint+"/boot")
err = cmd.Run()
if err != nil {
other.PrintError("Failed to mount "+partitionB+" on "+mountPoint+"/boot", 1)
}
}
}
func chrootCommandRun(args []string) {
var cmd *exec.Cmd
var errorSlice int
if partitionR == "/" {
errorSlice = 0
cmd = exec.Command(args[0], args[1:]...)
} else {
errorSlice = 1
args = append([]string{mountPoint}, args...)
cmd = exec.Command("arch-chroot", args...)
}
err := cmd.Run()
if err != nil {
other.PrintError("Failed to run \""+strings.Join(args[errorSlice:], " ")+"\" within chroot: "+err.Error(), 1)
}
}
func mkdir(path string) {
err := os.MkdirAll(path, 0755)
if err != nil {
other.PrintError("Failed to create \""+path+"/\" directory", 1)
}
err = os.Chown(path, 1000, 1000)
if err != nil {
other.PrintError("Failed to set ownership of \""+path+"\"", 1)
}
}
func allTweaksAnnounce(stage string) {
if performAllTweaks {
fmt.Println("Performing " + stage + "...")
}
}