-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
169 lines (137 loc) · 4.45 KB
/
Copy pathmain.go
File metadata and controls
169 lines (137 loc) · 4.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// the flag's logic (cobra) are located here
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
)
var version = "0.2.5 🍭"
func main() {
// custom help template, replace cobra's help
const customHelpTemplate = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
{{end}}Usage:
{{.UseLine}}
Flags:
{{.Flags.FlagUsages | trimTrailingWhitespaces}}
`
// Define the root command for the CLI app using Cobra
var rootCmd = &cobra.Command{
// users will type `wall` on the Command Line
Use: "wallah",
Short: "A fast and simple CLI to change your macOS wallpaper and elegantly hide that ugly notch.", // A short description
Long: "\nHi, I'm Wallah. I will help you change your wallpaper to your desired color and get rid of that notch.\n\nWith Wallah, you can easily apply wallpapers featuring solid colors and rounded borders that blend seamlessly around the notch area.\n\n----- HOW TO USE -------\n\nExample:\n wallah [color] Set a wallpaper color\n wallah random Randomly sets a wallpaper\n wallah -l list available colors",
// validation only in args
Args: func(cmd *cobra.Command, args []string) error {
listFlag, err := cmd.Flags().GetBool("list")
if err != nil {
return err
}
versionFlag, err := cmd.Flags().GetBool("version")
if err != nil {
return err
}
randomFlag, err := cmd.Flags().GetBool("random")
if err != nil {
return err
}
if listFlag || versionFlag || randomFlag {
if len(args) != 0 {
// printErrorMessage("No arguments allowed when using a flag.")
return fmt.Errorf("no arguments allowed when using a flag.")
}
return nil
}
// Allow zero or one argument (for color or "random")
if len(args) > 1 {
// printErrorMessage("Requires one or more arguments")
return fmt.Errorf("Requires one or more arguments")
}
if len(args) == 1 && args[0] == "random" {
return nil
}
return nil
},
// The function to run when the command is executed, logic only
Run: func(cmd *cobra.Command, args []string) {
// Check if --list flag is set
listFlag, err := cmd.Flags().GetBool("list")
if err != nil {
printErrorMessage("problem for reading flags:")
printErr(err)
os.Exit(1)
}
// Check if --version flag is set
versionFlag, err := cmd.Flags().GetBool("version")
if err != nil {
printErrorMessage("problem for reading flags:")
printErr(err)
os.Exit(1)
}
// Check if --random flag is set
randomFlag, err := cmd.Flags().GetBool("random")
if err != nil {
printErrorMessage("problem for reading flags:")
printErr(err)
os.Exit(1)
}
if versionFlag {
versionStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("#ff7e23")) //orange
versionLine := "version " + version
fmt.Println(versionStyle.Render(versionLine))
return
}
if listFlag {
printList()
return
}
if randomFlag || (len(args) > 0 && args[0] == "random") {
rand.Seed(time.Now().UnixNano())
randomColor := wallpaperOrder[rand.Intn(len(wallpaperOrder))]
err := changeWallpaper(randomColor)
if err != nil {
fullMessage := fmt.Sprintf("Error changing the wallah to '%s'.", randomColor)
printErrorMessage(fullMessage)
printErr(err)
os.Exit(1)
}
printWall(randomColor)
return
}
//////////////// INPUT CHECK //////////////
// Expect exactly one positional argument (one color)
if len(args) != 1 {
printErrorMessage("You must specify one color.")
cmd.Help()
os.Exit(1)
}
// retrieve the first argument (the color name)
colorName := args[0]
// Call the func to change the wallpaper using the provided color
err = changeWallpaper(colorName)
if err != nil {
fullMessage := fmt.Sprintf("Error changing the wallah to '%s'.", colorName)
printErrorMessage(fullMessage)
printErr(err)
os.Exit(1)
}
// If successful, print
printWall(colorName)
},
}
// Add flags
rootCmd.Flags().BoolP("list", "l", false, "list available colors")
rootCmd.Flags().BoolP("version", "v", false, "current version")
rootCmd.Flags().BoolP("random", "r", false, "set a random wallpaper color")
// Set custom help template
rootCmd.SetHelpTemplate(customHelpTemplate)
// Execute the root command, which parses arguments and runs the Run function
if err := rootCmd.Execute(); err != nil {
printErrorMessage("Error executing command: ")
printErr(err)
os.Exit(1)
}
}