-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (99 loc) · 3.16 KB
/
main.go
File metadata and controls
116 lines (99 loc) · 3.16 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
111
112
113
114
115
116
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
func fileExists(filePath string) bool {
_, err := os.Stat(filePath) // Attempt to get file information
if err != nil {
// If an error occurred, check if it indicates the file doesn't exist
return !errors.Is(err, os.ErrNotExist)
}
// If no error, the file exists
return true
}
func main() {
flags := parseFlags()
config := parseConfig(flags)
if flags.ShowColors {
for i := range config.Colors {
fmt.Println(i)
}
os.Exit(0)
}
if flags.ShowConfigs {
fmt.Println("All Configs:")
for _, v := range config.ConfigFiles {
fmt.Println("----------------")
fmt.Printf("Name: %s\n", v.Name)
fmt.Printf("Location (palettro): ~/.config/palettro/%s\n", strings.ToLower(v.Name))
fmt.Printf("Location: %s\n", v.Path)
fmt.Printf("Restart service on color change: %t\n", v.Restart != "")
if v.Restart != "" {
fmt.Printf("Service to kill on color change: %v\n", v.Restart)
}
}
os.Exit(0)
}
color, colorExists := config.Colors[flags.Color]
if flags.Color == "N/A" {
log.Fatalln("The \"-color\" flag must be set.")
} else if !colorExists {
log.Fatalf("The Color \"%s\" does not exist in your config (%s)", flags.Color, flags.ConfigPath)
}
for _, v := range config.ConfigFiles {
path := expandPath("~/.config/palettro/" + strings.ToLower(v.Name))
dir, err := os.ReadDir(path)
if err != nil {
log.Fatalf("[ENOENT]: Unable to read directory at %v\n", path)
}
for _, f := range dir {
filePath := filepath.Join(path, f.Name())
file, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("[ENOENT]: Unable to read file at \"%s\"\n", filePath)
}
var fileStr string
fileStr = strings.ReplaceAll(string(file), "((PALETTRO.HEX))", color.Hex)
fileStr = strings.ReplaceAll(fileStr, "((PALETTRO.HSL))", color.HSL)
fileStr = strings.ReplaceAll(fileStr, "((PALETTRO.RGB))", color.RGB)
fileStr = strings.ReplaceAll(fileStr, "((PALETTRO.RGBA))", color.RGBA)
fileStr = strings.ReplaceAll(fileStr, "((PALETTRO.HEXTRANS))", color.HexTrans)
newFilePath := expandPath(filepath.Join(v.Path, f.Name()))
if fileExists(newFilePath) && !flags.Autoconfirm {
fmt.Printf("Warning: File '%s' already exists! Continuing will overwrite it. Continue? [y/N]: ", filePath)
var response string
fmt.Scan(&response)
response = strings.ToLower(strings.TrimSpace(response))
if !(response == "y" || response == "yes") {
os.Exit(1)
}
}
err = os.WriteFile(newFilePath, []byte(fileStr), 0644)
if err != nil {
log.Fatalf("Failed to write file '%s': %v", newFilePath, err)
}
}
if v.Restart != "" {
// Kill the process by name
killCmd := exec.Command("pkill", "-f", v.Restart)
if err := killCmd.Run(); err != nil {
log.Printf("Warning: Failed to kill process %s: %v", v.Restart, err)
}
// Restart the process detached from this program
restartCmd := exec.Command("nohup", v.Restart)
restartCmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if err := restartCmd.Start(); err != nil {
log.Printf("Warning: Failed to restart process %s: %v", v.Restart, err)
}
}
}
}