/
set.go
236 lines (203 loc) · 7.47 KB
/
set.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright © 2018 Sebastien Bastide
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"errors"
"fmt"
"os"
"runtime"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// flags vars
var age int
// autoStart defines if TEMPest should start automatically or not.
// -> Default is nope.
var autoStart string
// LinuxAutoStartP is the path to $HOME/.config/autostart/tempest.desktop
// which is initialized in init()
var LinuxAutoStartP string
// WindowsAutoStartSL is the path to "%AppData%\Microsoft\Windows\Start Menu\Programs\Startup\tempest".
// which is a Symlink to the scripts/startup.bat file, initialized in init()
var WindowsAutoStartSL string
// setCmd represents the set command
var setCmd = &cobra.Command{
Use: "set",
Short: "set is used to set variables, content, config on TEMPest",
Long: `set is used to set variables, content, config on TEMPest. For example:
tempest set --age 4
# This sets the maximum "age" of the target directories content
More features might come next. Stay tuned!
`,
Run: func(cmd *cobra.Command, args []string) {
if age != -1 {
if errAge := setAge(); errAge != nil {
fmt.Println(errAge)
}
// color.HiCyan("Sorry! No working at the moment! -\\('o')/-")
}
if autoStart != "" {
if err := setAutoStart(); err != nil {
fmt.Println(redB("::"), color.HiRedString(err.Error()))
}
}
if age == -1 && autoStart == "" {
if err := cmd.Help(); err != nil {
color.HiRed("You must provide some flags or whatever! Just do something!")
}
}
// fmt.Println("set called")
/* errWC := viper.MergeInConfig()
if errWC != nil {
log.Println(errWC)
} */
},
}
func init() {
RootCmd.AddCommand(setCmd)
// $HOME/.config/autostart/tempest.desktop
LinuxAutoStartP = conf.Home + string(os.PathSeparator) + ".config" + string(os.PathSeparator) + "autostart" + string(os.PathSeparator) + "tempest.desktop"
WindowsAutoStartSL = `%AppData%\Microsoft\Windows\"Start Menu"\Programs\Startup\TEMPest-startup`
// setCmd.PersistentFlags().String("foo", "", "A help for foo")
// setCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
setCmd.Flags().IntVarP(&age, "age", "a", -1, "Set the maximum age (in days) of the targets' content")
setCmd.Flags().StringVarP(&autoStart, "auto-start", "s", "", "Set to 'on' to activate or 'off' to deactivate. (not activated by default)")
}
// setAge sets the "duration" value in the config file (~/.tempest/.tempest.yaml)
// - ``duration`` being the maximum age of the target's content
func setAge() error {
if age > 0 {
// ageStr := fmt.Sprintf("%d", age)
viper.Set("duration", age)
viper.Set("auto-mode", viper.GetBool("auto-mode"))
//* Save config
errS := viper.WriteConfigAs(viper.ConfigFileUsed())
if errS != nil {
color.Red(errS.Error())
}
fmt.Println(greenB("::"), color.HiGreenString("New age set at:"), viper.GetInt("duration"))
return nil
}
color.Red("::Age must be greater than 0, genius . . .")
return errors.New("Error while setting the age")
}
// setAutoStart sets the "auto-mode" value in the config file (~/.tempest/.tempest.yaml).
// - ``auto-mode`` being called sometimes in the code "auto-start"
func setAutoStart() error {
if autoStart == "" {
return errors.New(fmt.Sprint("Empty autoStart", autoStart, ";"))
}
// set up
var boolAutoMode bool
switch autoStart {
case "on":
boolAutoMode = true
case "off":
boolAutoMode = false
default:
return errors.New("Wrong parameter, only 'on' or 'off' are accepted for this flag (quote excluded)")
}
// Define config
viper.Set("duration", viper.GetInt("duration"))
viper.Set("auto-mode", boolAutoMode)
//* actual shit
switch runtime.GOOS {
case "linux":
if err := autoStartLinux(boolAutoMode); err != nil {
return err
}
case "windows":
fmt.Println(cyanB(":: [NOTE]"), color.HiCyanString("This functionality hasn't been fully tested for Windows."))
// case "darwin":
// err = exec.Command("open", url).Start()
default:
return errors.New(fmt.Sprint(redB(":: [ERROR]"), color.HiRedString("Unsupported platform")))
}
// Save config
if err := viper.WriteConfigAs(viper.ConfigFileUsed()); err != nil {
return err
}
fmt.Println(greenB("::"), color.HiGreenString("Auto start is now"), greenB(autoStart))
return nil
}
// autoStartWindows set the auto start for TEMPest (for Windows - only tested on W10).
// - ``{ should = true }``: activate
// - ``{ should = false }``: deactivate
//
// This func creates a link of the bash file ``$HOME/.config/autostart/tempest.desktop``.
// If the file does not exist, it gets created when autoStart is set to "on".
func autoStartWindows(should bool) error {
// var ctntAutoF string
// if exists,and should not exist, we delete
if is, err := IsDirectory(WindowsAutoStartSL); (!is || err == nil) && !should {
if errRm := os.Remove(WindowsAutoStartSL); errRm != nil {
return errRm
}
} else if should {
// We create a symlink to the startup folder
os.Symlink(conf.Gopath+string(os.PathSeparator)+"scripts"+string(os.PathSeparator)+"startup.bat", WindowsAutoStartSL)
}
return nil
}
// autoStartLinux set the auto start for TEMPest.
// - ``{ should = true }``: activate
// - ``{ should = false }``: deactivate
//
// This func writes in the file ``$HOME/.config/autostart/tempest.desktop``.
// If the file does not exist, it gets created when autoStart is set to "on".
func autoStartLinux(should bool) error {
var ctntAutoF string
// Remove if already existed so we create a fresh one
_ = os.Remove(LinuxAutoStartP)
autoF, errCreate := os.OpenFile(LinuxAutoStartP, os.O_RDWR|os.O_CREATE, 0644)
if errCreate != nil {
fmt.Println(redB(":: [ERROR]"), color.HiRedString("No file create, but we tried !! #sadface:\n\t->", LinuxAutoStartP, "\n\t->"))
return errCreate
}
defer autoF.Close()
// Adapt the content to be written according to the mode selected
if should {
ctntAutoF = `[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=TEMPest
Comment=Autorun of TEMPest
Exec=` + conf.Gopath + `/src/github.com/ChacaS0/tempest/scripts/autostart.sh
`
} else {
// If exists
// Keep the file but set ``Hidden`` to ``true``
ctntAutoF = `[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=TEMPest
Comment=Autorun of TEMPest
Exec=` + conf.Gopath + `/src/github.com/ChacaS0/tempest/scripts/autostart.sh
Hidden=true
`
}
// then write to it the config
if _, errW := autoF.WriteString(ctntAutoF); errW != nil {
fmt.Println(redB(":: [ERROR]"), color.HiRedString("Can't write to the file, WTF!?\n\t->"))
return errW
}
return nil
}