forked from palantir/godel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
168 lines (146 loc) · 4.77 KB
/
check.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2016 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"io"
"strings"
"github.com/nmiyake/pkg/dirs"
"github.com/palantir/amalgomate/amalgomated"
"github.com/palantir/pkg/cli"
"github.com/palantir/pkg/cli/cfgcli"
"github.com/palantir/pkg/cli/flag"
"github.com/palantir/pkg/pkgpath"
"github.com/pkg/errors"
"github.com/palantir/godel/apps/okgo/checkoutput"
"github.com/palantir/godel/apps/okgo/checks"
"github.com/palantir/godel/apps/okgo/cmd/cmdlib"
"github.com/palantir/godel/apps/okgo/config"
"github.com/palantir/godel/apps/okgo/params"
)
const packagesFlagName = "packages"
var packagesFlag = flag.StringSlice{
Name: packagesFlagName,
Usage: "Packages to check",
Optional: true,
}
func RunAllCommand(supplier amalgomated.CmderSupplier) cli.Command {
return cli.Command{
Name: "runAll",
Usage: "Run all checks",
Flags: []flag.Flag{
packagesFlag,
},
Action: func(ctx cli.Context) error {
cfg, err := config.Load(cfgcli.ConfigPath, cfgcli.ConfigJSON)
if err != nil {
return err
}
wd, err := dirs.GetwdEvalSymLinks()
if err != nil {
return err
}
return DoRunAll(ctx.Slice(packagesFlagName), cfg, supplier, wd, ctx.App.Stdout)
},
}
}
func DoRunAll(pkgs []string, cfg params.OKGo, supplier amalgomated.CmderSupplier, wd string, stdout io.Writer) error {
var checksWithOutput []amalgomated.Cmd
for _, cmd := range cmdlib.Instance().Cmds() {
// if "omit" is true, skip the check
if cmdCfg, ok := cfg.Checks[cmd]; ok && cmdCfg.Skip {
continue
}
cmder, err := supplier(cmd)
if err != nil {
return errors.Wrapf(err, "%s is not a valid command", cmd.Name())
}
producedOutput, err := executeSingleCheckWithOutput(cmd, cmder, cfg, pkgs, wd, stdout)
if err != nil {
// indicates unexpected hard failure -- check returning non-0 exit code will not trigger
return errors.Wrapf(err, "check %s failed", cmd.Name())
}
if producedOutput {
checksWithOutput = append(checksWithOutput, cmd)
}
}
if len(checksWithOutput) != 0 {
return errors.Errorf("Checks produced output: %v", checksWithOutput)
}
return nil
}
func SingleCheckCommand(cmd amalgomated.Cmd, supplier amalgomated.CmderSupplier) cli.Command {
return cli.Command{
Name: cmd.Name(),
Usage: "Run " + cmd.Name(),
Flags: []flag.Flag{
packagesFlag,
},
Action: func(ctx cli.Context) error {
cfg, err := config.Load(cfgcli.ConfigPath, cfgcli.ConfigJSON)
if err != nil {
return err
}
wd, err := dirs.GetwdEvalSymLinks()
if err != nil {
return err
}
cmder, err := supplier(cmd)
if err != nil {
return errors.Wrapf(err, "failed to create Cmder for %s", cmd.Name())
}
if producedOutput, err := executeSingleCheckWithOutput(cmd, cmder, cfg, ctx.Slice(packagesFlagName), wd, ctx.App.Stdout); producedOutput {
return fmt.Errorf("")
} else if err != nil {
return err
}
return nil
},
}
}
// executeSingleCheckWithOutput runs the specified check and outputs the result to stdOut. Returns true if the check
// produced any output, false otherwise.
func executeSingleCheckWithOutput(cmd amalgomated.Cmd, cmder amalgomated.Cmder, cfg params.OKGo, pkgs []string, wd string, stdout io.Writer) (bool, error) {
output, err := singleCheck(cmd, cmder, cfg, pkgs, wd, stdout)
if err != nil {
return false, err
}
producedOutput := len(output) != 0
if producedOutput {
outputLines := make([]string, len(output))
for i, currLine := range output {
outputLines[i] = currLine.String()
}
fmt.Fprintln(stdout, strings.Join(outputLines, "\n"))
}
return producedOutput, nil
}
func singleCheck(cmd amalgomated.Cmd, cmder amalgomated.Cmder, cfg params.OKGo, pkgs []string, cmdWd string, stdout io.Writer) ([]checkoutput.Issue, error) {
checker, err := checks.GetChecker(cmd)
if err != nil {
return nil, err
}
fmt.Fprintf(stdout, "Running %v...\n", cmd.Name())
if len(pkgs) == 0 {
// if no arguments were provided, run check on "all"
return checker.Check(cmder, cmdWd, cfg)
}
// convert arguments to packages
packages, err := pkgpath.PackagesFromPaths(cmdWd, pkgs)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert arguments to packages: %v", pkgs)
}
// run check on specified packages
return checker.CheckPackages(cmder, packages, cfg)
}