-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (58 loc) · 1.45 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/billychappell/regselect"
)
var (
filename = flag.String("config", "config.json", "json config file to use for editing the registry")
overwrite = flag.Bool("saveprev", false, "whether to overwrite config file to include previous values")
)
func confirm(filename *string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(`\n WARNING: Changing registry values can break your computer. \n Are you sure you want to proceed? (yes/no)
\n Config File: %s`, filename)
answer, _ := reader.ReadString('\n')
switch answer {
case "yes", "y", "Y", "YES":
return true
case "n", "no", "N", "NO":
return false
default:
fmt.Println("can't understand answer '%s', please answer 'yes' or 'no'.")
confirm(filename)
}
return false
}
func main() {
flag.Parse()
cfg, err := regselect.Unmarshal(*filename)
if err != nil {
log.Fatalf("error unmarshaling json config file: \n %s \n", err)
}
check := confirm(filename)
if check == false {
err = fmt.Errorf("failed to confirm changes with user")
panic(err)
}
err = cfg.Set()
if err != nil {
log.Fatal(err)
}
if *overwrite == true {
err = cfg.Write(*filename)
if err != nil {
log.Fatal(err)
}
}
update := fmt.Sprintf("%s.%s.save", filename, time.Now().String())
err = cfg.Write(update)
if err != nil {
log.Fatalf("can't write updated file, fatal error: %s", err)
}
fmt.Println("Finished! Write successful.")
}