-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
100 lines (79 loc) · 2.33 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"reflect"
"strings"
"github.com/Azure/moby-packaging/pkg/archive"
)
type allArgs struct {
pkg string
tag string
revision string
}
func main() {
args := allArgs{}
flag.StringVar(&args.pkg, "project", "", "name of project")
flag.StringVar(&args.tag, "tag", "", "tag for build set")
flag.StringVar(&args.revision, "revision", "", "revision for build set")
flag.Parse()
if args.pkg == "" || args.tag == "" || args.revision == "" {
flag.Usage()
os.Exit(1)
}
if err := validate(args); err != nil {
fmt.Fprintf(os.Stderr, "##vso[task.logissue type=error;]%s\n", err)
os.Exit(1)
}
}
func validate(args allArgs) error {
if args.pkg == "" || args.tag == "" || args.revision == "" {
return fmt.Errorf("all arguments are required: project, tag, and revision")
}
j, err := io.ReadAll(os.Stdin)
if err != nil && err != io.EOF {
return fmt.Errorf("error: %s\n", err)
}
specs := []archive.Spec{}
if err := json.Unmarshal(j, &specs); err != nil {
return err
}
if len(specs) == 0 {
return nil
}
lastCommit := specs[0].Commit
lastRepo := specs[0].Repo
for i := range specs {
if project := specs[i].Pkg; project != args.pkg {
return fmt.Errorf("package name does not match: was '%s', should be '%s'", args.pkg, project)
}
if tag := specs[i].Tag; tag != args.tag {
return fmt.Errorf("package tag does not match: was '%s', should be '%s'", tag, args.tag)
}
if revision := specs[i].Revision; revision != args.revision {
return fmt.Errorf("package revision does not match: was '%s', should be '%s'", revision, args.revision)
}
if commit := specs[i].Commit; commit != lastCommit {
return fmt.Errorf("all builds should have the same commit hash: '%s' vs '%s'", commit, lastCommit)
}
if repo := specs[i].Repo; repo != lastRepo {
return fmt.Errorf("all builds should have the same repo: '%s' vs '%s'", repo, lastCommit)
}
lastCommit = specs[i].Commit
lastRepo = specs[i].Repo
v := reflect.ValueOf(&specs[i]).Elem()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i).Interface().(string)
if f == "" {
return fmt.Errorf("blank value: %s", v.Type().Field(i).Name)
}
if strings.Contains(f, "'") {
return fmt.Errorf(`illegal character in field '%s': "%s"`, v.Type().Field(i).Name, f)
}
}
}
return nil
}