-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (133 loc) · 3.04 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
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/keaising/auto-mouse-keyboard/command"
"github.com/keaising/auto-mouse-keyboard/model"
)
func main() {
if len(os.Args) > 1 {
log.Println("specific file", os.Args[1])
readFileAndRun(os.Args[1])
return
}
file, err := os.Open(".")
if err != nil {
log.Fatalln("fail opening directory")
}
list, err := file.Readdirnames(0)
if err != nil {
log.Fatalln("fail read directory")
}
for _, name := range list {
if strings.HasSuffix(name, ".conf") {
readFileAndRun(name)
}
}
}
func readFileAndRun(fileName string) {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var sources []string
for scanner.Scan() {
sources = append(sources, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
common, err := getCommon(sources)
if err != nil {
log.Println("Get common value error, please check and retry")
return
}
commands, err := command.ParseCommands(sources)
if err != nil {
log.Println("ERROR! correct amk.conf and run again")
return
}
_ = command.RunCommand(commands, common)
}
var commonItems = []model.CommonItem{
{
Name: "SHIM",
Type: model.CommonItemTypeInt,
},
{
Name: "SCALE",
Type: model.CommonItemTypeFloat64,
},
}
func getCommon(sources []string) (*model.Common, error) {
var (
common model.Common
kv = make(map[string]interface{})
)
for _, source := range sources {
source = strings.TrimSpace(source)
if source == "" || strings.HasPrefix(source, "#") {
continue
}
for _, item := range commonItems {
if strings.HasPrefix(source, item.Name) {
if len(source) < len(item.Name)+1 {
log.Println("no value of", item.Name)
return nil, fmt.Errorf("no value of %s", item.Name)
}
v, err := getValueOfConfig(source, item)
if err != nil {
return nil, err
}
kv[item.Name] = v
}
}
// Convert map to struct
data, err := json.Marshal(kv)
if err != nil {
log.Println("marshal kv failed", err)
return nil, fmt.Errorf("marshal kv failed %v", err)
}
err = json.Unmarshal(data, &common)
if err != nil {
log.Println("unmarshal kv failed", err)
return nil, fmt.Errorf("unmarshal kv failed %v", err)
}
}
return &common, nil
}
// Convert config into config
func getValueOfConfig(source string, item model.CommonItem) (interface{}, error) {
rawValue := source[(len(item.Name) + 1):]
switch item.Type {
case model.CommonItemTypeInt:
{
v, err := strconv.Atoi(rawValue)
if err != nil {
log.Println(item.Name, "value not int", err)
return nil, fmt.Errorf("%s value not int %v", item.Name, err)
}
return v, nil
}
case model.CommonItemTypeFloat64:
{
v, err := strconv.ParseFloat(rawValue, 64)
if err != nil {
log.Println(item.Name, "value not float64", err)
return nil, fmt.Errorf("%s value not float64 %v", item.Name, err)
}
return v, nil
}
case model.CommonItemTypeString:
fallthrough
default:
return rawValue, nil
}
}