-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
77 lines (66 loc) · 1.52 KB
/
command.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
package commands
import (
"bufio"
"errors"
"os"
"strings"
)
// GenerateIndv generates individual command list
func GenerateIndv(a []string) []string {
ret := []string{}
seen := map[string]bool{}
for _, val := range a {
if _, old := seen[val]; !old {
ret = append(ret, val)
seen[val] = true
}
}
return ret
}
// GeneratePairs generates a pair of commands to be used with co-scheduling
func GeneratePairs(commands []string) [][2]string {
seen := map[string]bool{}
var ret [][2]string
for i, c0 := range commands {
for j, c1 := range commands {
if i >= j {
continue
}
if _, old := seen[c0+c1]; !old {
ret = append(ret, [2]string{c0, c1})
seen[c0+c1] = true
seen[c1+c0] = true
}
}
}
return ret
}
// Read reads commands from files
func Read(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, errors.New("Error opening file " + filename + ": " + err.Error())
}
defer file.Close()
var commands []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
temp := scanner.Text()
temp = strings.TrimSpace(temp)
if len(temp) > 0 && temp[0] != '#' {
commands = append(commands, scanner.Text())
}
}
if err := scanner.Err(); err != nil {
return nil, errors.New("Error scanning commands: " + err.Error())
}
return commands, nil
}
func Pretty(app string) string {
slash := strings.LastIndex(app, "/")
space := strings.Index(app[slash+1:], " ")
if space == -1 {
space = len(app[slash+1:])
}
return app[slash+1 : slash+1+space]
}