-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathskipgen.go
151 lines (131 loc) · 3.8 KB
/
skipgen.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
// skipgen is a program that will generate a skiplist given a yaml file
// and optionally, board name, branch name, and environment name.
package main
import (
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"sort"
)
// This variable is overwritten during build by goreleaser
var version = "master"
func check(e error) {
if e != nil {
panic(e)
}
}
// StringArray is a custom type that can be used for parsing
// yaml fields that can be either a string, or an array.
// See https://github.com/go-yaml/yaml/issues/100
type StringArray []string
// UnmarshalYAML is a custom version of UnmarshalYAML that implements
// StringArray. See https://github.com/go-yaml/yaml/issues/100
func (a *StringArray) UnmarshalYAML(unmarshal func(interface{}) error) error {
var multi []string
err := unmarshal(&multi)
if err != nil {
var single string
err := unmarshal(&single)
if err != nil {
return err
}
*a = []string{single}
} else {
*a = multi
}
return nil
}
// stringInSlice searches for a particular string
// in a slice of strings. If "all" is contained in
// the slice, then true is always returned.
func stringInSlice(a string, list []string) bool {
if a == "all" {
return true
}
for _, b := range list {
if b == a || b == "all" {
return true
}
}
return false
}
// Skipfile is a map of the structure in the yaml skipfile
type Skipfile struct {
Skiplist []struct {
Reason string
URL string
Environments StringArray
Boards StringArray
Branches StringArray
Tests StringArray
}
}
// parseSkipfile parses a given buf and returns a Skipfile
// struct and err, if any.
func parseSkipfile(buf []byte) (Skipfile, error){
var skips Skipfile
err := yaml.Unmarshal(buf, &skips)
return skips, err
}
// getSkipfileContents returns a string containing a skipfile
// given a board, environment, branch, and Skipfile struct.
func getSkipfileContents(board string, branch string, environment string, skips Skipfile) (string){
var skiplist []string
for _, skip := range skips.Skiplist {
if stringInSlice(board, skip.Boards) &&
stringInSlice(branch, skip.Branches) &&
stringInSlice(environment, skip.Environments) {
for _, test := range skip.Tests {
if ! stringInSlice(test, skiplist) {
skiplist = append(skiplist, test)
}
}
}
}
sort.Strings(skiplist)
var buf string
for _, test := range skiplist {
buf = buf + fmt.Sprintf("%s\n", test)
}
return buf
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n %s [--board <boardname>] [--branch <branchname>] [--environment <environmentname>] <skipfile.yaml> \n", os.Args[0])
os.Exit(1)
}
func main() {
boardPtr := flag.String("board", "all", "Board name. If not specified, skips for all boards will be returned.")
branchPtr := flag.String("branch", "all", "Branch name. If not specified, skips for all branches will be returned.")
environmentPtr := flag.String("environment", "all", "Environment name. If not specified, skips for all environments will be returned.")
versionPtr := flag.Bool("version", false, "Print skipgen version and exit.")
flag.Parse()
if (*versionPtr) {
fmt.Fprintf(os.Stderr, "skipgen %s\n", version)
os.Exit(0)
}
if len(flag.Args()) < 1 {
fmt.Fprintf(os.Stderr, "Error: skipfile not provided\n\n")
usage()
}
if len(flag.Args()) > 1 {
fmt.Fprintf(os.Stderr, "Error: unprocessed argument: '%s'\n", flag.Arg(1))
fmt.Fprintf(os.Stderr, "Put the path to skipfile last on the command line.\n\n")
usage()
}
skipfile := flag.Arg(0)
_, err := os.Stat(skipfile)
if os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: skipfile '%s' not found\n\n", skipfile)
usage()
}
check(err)
// Read skipfile.yaml
buf, err := ioutil.ReadFile(skipfile)
check(err)
// Parse skipfile
skips, err := parseSkipfile(buf)
check(err)
fmt.Printf(getSkipfileContents(*boardPtr, *branchPtr, *environmentPtr, skips))
}