github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/cmd/verify/cmd.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package verify 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/nmiyake/pkg/dirs" 22 "github.com/palantir/amalgomate/amalgomated" 23 "github.com/palantir/pkg/cli" 24 "github.com/palantir/pkg/cli/flag" 25 "github.com/pkg/errors" 26 27 "github.com/palantir/godel/cmd" 28 ) 29 30 const ( 31 cmdName = "verify" 32 apply = "apply" 33 skipFormat = "skip-format" 34 skipGenerate = "skip-generate" 35 skipImports = "skip-imports" 36 skipLicense = "skip-license" 37 skipCheck = "skip-check" 38 skipTest = "skip-test" 39 junitOutputPath = "junit-output" 40 ) 41 42 func Command(gödelPath string) cli.Command { 43 return cli.Command{ 44 Name: cmdName, 45 Usage: "Run format, generate, imports, license, check and test tasks", 46 Flags: []flag.Flag{ 47 flag.BoolFlag{Name: apply, Usage: "Apply changes when possible", Value: true}, 48 flag.BoolFlag{Name: skipFormat, Usage: "Skip 'format' task"}, 49 flag.BoolFlag{Name: skipGenerate, Usage: "Skip 'generate' task"}, 50 flag.BoolFlag{Name: skipImports, Usage: "Skip 'imports' task"}, 51 flag.BoolFlag{Name: skipLicense, Usage: "Skip 'license' task"}, 52 flag.BoolFlag{Name: skipCheck, Usage: "Skip 'check' task"}, 53 flag.BoolFlag{Name: skipTest, Usage: "Skip 'test' task"}, 54 flag.StringFlag{Name: junitOutputPath, Usage: "Path to JUnit XML output (only used if 'test' task is run)"}, 55 }, 56 Action: func(ctx cli.Context) error { 57 wd, err := dirs.GetwdEvalSymLinks() 58 if err != nil { 59 return err 60 } 61 62 globalFlags, err := globalFlags(ctx) 63 if err != nil { 64 return err 65 } 66 cmder := amalgomated.PathCmder(gödelPath, globalFlags...) 67 68 var failedChecks []string 69 70 if !ctx.Bool(skipFormat) { 71 args := []string{"format", "-v"} 72 if !ctx.Bool(apply) { 73 args = append(args, "-l") 74 } 75 if err := runCmd(cmder, args, wd, ctx); err != nil { 76 failedChecks = append(failedChecks, strings.Join(args, " ")) 77 } 78 } 79 80 if !ctx.Bool(skipGenerate) { 81 args := []string{"generate"} 82 if !ctx.Bool(apply) { 83 args = append(args, "--verify") 84 } 85 ctx.Println("Running gogenerate...") 86 if err := runCmd(cmder, args, wd, ctx); err != nil { 87 failedChecks = append(failedChecks, strings.Join(args, " ")) 88 } 89 } 90 91 if !ctx.Bool(skipImports) { 92 args := []string{"imports"} 93 if !ctx.Bool(apply) { 94 args = append(args, "--verify") 95 } 96 ctx.Println("Running gocd...") 97 if err := runCmd(cmder, args, wd, ctx); err != nil { 98 failedChecks = append(failedChecks, strings.Join(args, " ")) 99 } 100 } 101 102 if !ctx.Bool(skipLicense) { 103 args := []string{"license"} 104 if !ctx.Bool(apply) { 105 args = append(args, "--verify") 106 } 107 ctx.Println("Running golicense...") 108 if err := runCmd(cmder, args, wd, ctx); err != nil { 109 failedChecks = append(failedChecks, strings.Join(args, " ")) 110 } 111 } 112 113 if !ctx.Bool(skipCheck) { 114 if err := runCmd(cmder, []string{"check"}, wd, ctx); err != nil { 115 failedChecks = append(failedChecks, "check") 116 } 117 } 118 119 if !ctx.Bool(skipTest) { 120 args := []string{"test"} 121 if ctx.Has(junitOutputPath) { 122 args = append(args, "--"+junitOutputPath, ctx.String(junitOutputPath)) 123 } 124 if err := runCmd(cmder, args, wd, ctx); err != nil { 125 failedChecks = append(failedChecks, "test") 126 } 127 } 128 129 if len(failedChecks) != 0 { 130 msgParts := []string{"Failed tasks:"} 131 for _, check := range failedChecks { 132 msgParts = append(msgParts, "\t"+check) 133 } 134 return fmt.Errorf(strings.Join(msgParts, "\n")) 135 } 136 137 return nil 138 }, 139 } 140 } 141 142 func globalFlags(ctx cli.Context) ([]string, error) { 143 var globalArgs []string 144 for _, f := range cmd.GlobalCLIFlags() { 145 if ctx.Has(f.MainName()) { 146 var flagValue string 147 switch f.(type) { 148 case flag.BoolFlag: 149 flagValue = fmt.Sprintf("%v", ctx.Bool(f.MainName())) 150 case flag.StringFlag: 151 flagValue = ctx.String(f.MainName()) 152 default: 153 return nil, errors.Errorf("Unhandled flag type %T for flag %v", f, f) 154 } 155 globalArgs = append(globalArgs, "--"+f.MainName(), flagValue) 156 } 157 } 158 return globalArgs, nil 159 } 160 161 func runCmd(cmder amalgomated.Cmder, args []string, wd string, ctx cli.Context) error { 162 cmd := cmder.Cmd(args, wd) 163 cmd.Stdout = ctx.App.Stdout 164 cmd.Stderr = ctx.App.Stderr 165 return cmd.Run() 166 }