Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fail text #301

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feature: Add fail text
Signed-off-by: Valentin Kiselev <[email protected]>
  • Loading branch information
mrexox committed Jul 17, 2022
commit 1e8622cc1402cc37bd9847a42b7d01f71cfebdf0
6 changes: 4 additions & 2 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type Command struct {
Root string `mapstructure:"root"`
Exclude string `mapstructure:"exclude"`

// DEPRECATED
FailText string `mapstructure:"fail_text"`

// DEPRECATED, will be deleted in 1.2.0
Runner string `mapstructure:"runner"`
}

Expand All @@ -43,7 +45,7 @@ func (c Command) DoSkip(gitState git.State) bool {

type commandRunReplace struct {
Run string `mapstructure:"run"`
Runner string `mapstructure:"runner"` // DEPRECATED
Runner string `mapstructure:"runner"` // DEPRECATED, will be deleted in 1.2.0
}

func mergeCommands(base, extra *viper.Viper) (map[string]*Command, error) {
Expand Down
2 changes: 2 additions & 0 deletions internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Script struct {
Skip interface{} `mapstructure:"skip"`
Tags []string `mapstructure:"tags"`

FailText string `mapstructure:"fail_text"`

// DEPRECATED
Run string `mapstructure:"run"`
}
Expand Down
42 changes: 26 additions & 16 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,30 @@ func (l *Lefthook) Run(hookName string, gitArgs []string) error {
close(resultChan)
}()

var okList, failList []string
var results []runner.Result
for res := range resultChan {
switch res.Status {
case runner.StatusOk:
okList = append(okList, res.Name)
case runner.StatusErr:
failList = append(failList, res.Name)
}
results = append(results, res)
}

if !outputSettings.doSkip(skipSummary) {
printSummary(time.Since(startTime), okList, failList, outputSettings)
printSummary(time.Since(startTime), results, outputSettings)
}

if len(failList) > 0 {
return errors.New("") // No error should be printed
for _, result := range results {
if result.Status == runner.StatusErr {
return errors.New("") // No error should be printed
}
}

return nil
}

func printSummary(
duration time.Duration,
okList, failList []string,
results []runner.Result,
outputSettings skipOutputSettings,
) {
if len(okList) == 0 && len(failList) == 0 {
if len(results) == 0 {
log.Info(log.Cyan("\nSUMMARY: (SKIP EMPTY)"))
return
}
Expand All @@ -143,14 +140,27 @@ func printSummary(
))

if !outputSettings.doSkip(skipSuccess) {
for _, fileName := range okList {
log.Infof("✔️ %s\n", log.Green(fileName))
for _, result := range results {
if result.Status != runner.StatusOk {
continue
}

log.Infof("✔️ %s\n", log.Green(result.Name))
}
}

if !outputSettings.doSkip(skipFailure) {
for _, fileName := range failList {
log.Infof("🥊 %s\n", log.Red(fileName))
for _, result := range results {
if result.Status != runner.StatusErr {
continue
}

var failText string
if len(result.Text) != 0 {
failText = fmt.Sprintf(": %s", result.Text)
}

log.Infof("🥊 %s%s\n", log.Red(result.Name), log.Red(failText))
}
}
}
5 changes: 3 additions & 2 deletions internal/lefthook/runner/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const (

type Result struct {
Name string
Text string
Status status
}

func resultSuccess(name string) Result {
return Result{Name: name, Status: StatusOk}
}

func resultFail(name string) Result {
return Result{Name: name, Status: StatusErr}
func resultFail(name, text string) Result {
return Result{Name: name, Text: text, Status: StatusErr}
}
16 changes: 8 additions & 8 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (r *Runner) RunAll(scriptDirs []string) {
log.StopSpinner()
}

func (r *Runner) fail(name string) {
r.resultChan <- resultFail(name)
func (r *Runner) fail(name, text string) {
r.resultChan <- resultFail(name, text)
r.failed = true
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
if (file.Mode() & executableMask) == 0 {
if err := r.fs.Chmod(path, executableFileMode); err != nil {
log.Errorf("Couldn't change file mode to make file executable: %s", err)
r.fail(file.Name())
r.fail(file.Name(), "")
return
}
}
Expand All @@ -134,7 +134,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
args = append(args, path)
args = append(args, r.args[:]...)

r.run(file.Name(), r.repo.RootPath, args)
r.run(file.Name(), r.repo.RootPath, script.FailText, args)
}

func (r *Runner) runCommands() {
Expand Down Expand Up @@ -178,7 +178,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
}

if err := command.Validate(); err != nil {
r.fail(name)
r.fail(name, "")
return
}

Expand All @@ -189,7 +189,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
}

root := filepath.Join(r.repo.RootPath, command.Root)
r.run(name, root, args)
r.run(name, root, command.FailText, args)
}

func (r *Runner) buildCommandArgs(command *config.Command) []string {
Expand Down Expand Up @@ -269,12 +269,12 @@ func prepareFiles(command *config.Command, files []string) string {
return strings.Join(files, " ")
}

func (r *Runner) run(name string, root string, args []string) {
func (r *Runner) run(name, root, failText string, args []string) {
out, err := Execute(root, args)

var execName string
if err != nil {
r.fail(name)
r.fail(name, failText)
execName = fmt.Sprint(log.Red("\n EXECUTE >"), log.Bold(name))
} else {
r.success(name)
Expand Down