-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessTask.go
More file actions
189 lines (162 loc) · 4.63 KB
/
processTask.go
File metadata and controls
189 lines (162 loc) · 4.63 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/gob"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)
func processTask(commandMap map[string]string) {
taskUUID, containsTaskUUID := commandMap["u"]
temporaryPositionalNumber, containsTemporaryPositionalNumber := commandMap["n"]
_, useImmediatePosition := commandMap["N"]
if !containsTaskUUID && !containsTemporaryPositionalNumber && !useImmediatePosition {
fmt.Println("Task processing needs a task UUID or temporary positional number!")
return
}
toState, containsToState := commandMap["t"]
if !containsToState {
fmt.Println("Task processing needs a task to-state!")
return
}
if toState != "create" && toState != "progress" && toState != "suspend" && toState != "cancel" && toState != "complete" {
fmt.Println("Invalid value " + toState + " for task toState!")
}
argsMap := make(map[string]string)
argsMap["toState"] = toState
if containsTaskUUID {
argsMap["taskUUID"] = taskUUID
processFile(changeTaskState, argsMap)
return
}
if containsTemporaryPositionalNumber {
matchingUUID, err := getUUIDFromTemporaryPositionalNumber(temporaryPositionalNumber)
if err != nil {
fmt.Println(err)
}
argsMap["taskUUID"] = matchingUUID
processFile(changeTaskState, argsMap)
return
}
if useImmediatePosition {
matchingUUID, err := getUUIDFromImmediatePosition()
if err != nil {
fmt.Println(err)
}
argsMap["taskUUID"] = matchingUUID
processFile(changeTaskState, argsMap)
return
}
}
func getUUIDFromTemporaryPositionalNumber(temporaryPositionalNumber string) (string, error) {
decodeFile, err := os.Open("log-mapping.bl")
if err != nil {
return "", errors.New("Unable to find mappings file")
}
defer decodeFile.Close()
decoder := gob.NewDecoder(decodeFile)
positionalMappings := make(map[string]string)
decoder.Decode(&positionalMappings)
uuid, containsUUID := positionalMappings[temporaryPositionalNumber]
if !containsUUID {
return "", errors.New("Unable to find line matching positional number " + temporaryPositionalNumber + "!")
}
return uuid, nil
}
func getUUIDFromImmediatePosition() (string, error) {
filename := "log-immediate-mapping.bl"
input, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
lines := strings.Split(string(input), "\n")
if len(lines) != 1 {
return "", errors.New("File parse error")
}
return lines[0], nil
}
func setImmediatePositionUUID(uuid string) error {
filename := "log-immediate-mapping.bl"
err := ioutil.WriteFile(filename, []byte(uuid), 0644)
if err != nil {
return errors.New("File write error")
}
return nil
}
func changeTaskState(lines []string, argsMap map[string]string) (linesToWrite []string, shouldWriteLines bool) {
taskUUID := argsMap["taskUUID"]
toState := argsMap["toState"]
for i, line := range lines {
if getUUID(line) == taskUUID {
if isDeleted(line) {
fmt.Println("Line has been deleted: " + line)
return
}
changedLine := changeTaskStateAtLine(line, toState)
if changedLine == "" {
panic("Error occured while changing task state!")
}
lines[i] = changedLine
fmt.Println("Task state changed for line:")
fmt.Println("")
fmt.Println(changedLine)
fmt.Println("")
break
}
}
// output := strings.Join(lines, "\n")
// err := ioutil.WriteFile(defaultFilePath, []byte(output), 0644)
// if err != nil {
// fmt.Println("Something went wrong while editing task state!")
// }
setImmediatePositionUUID(taskUUID)
return lines, true
}
func getUUID(line string) string {
if len(line) == 0 {
return ""
}
// Not a metadata line, ignore
if string(line[0]) != "(" {
return ""
}
uuidEndIndex := strings.Index(line, ">")
// this case should not happen
if uuidEndIndex == -1 {
fmt.Println("wow no")
return ""
}
uuidStartIndex := strings.LastIndex(line[:uuidEndIndex], ")") + 1
return line[uuidStartIndex:uuidEndIndex]
}
func changeTaskStateAtLine(line string, toState string) string {
toStateValue := "9"
switch toState {
case "create":
toStateValue = "0"
case "progress":
toStateValue = "1"
case "suspend":
toStateValue = "2"
case "cancel":
toStateValue = "3"
case "complete":
toStateValue = "4"
}
return setMetadataValue(line, "T", toStateValue)
}
func setMetadataValue(line string, key string, value string) string {
// Not a metadata line, ignore
if string(line[0]) != "(" {
return ""
}
uuidEndIndex := strings.Index(line, ">")
// this case should not happen
if uuidEndIndex == -1 {
return ""
}
uuidStartIndex := strings.LastIndex(line[:uuidEndIndex], ")") + 1
metadataStartIndex := strings.Index(line[:uuidStartIndex], "("+key+"-")
return line[:metadataStartIndex+len(key)+2] + value + line[metadataStartIndex+2+len(key)+len(value):]
}