-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65c4188
commit 9ede248
Showing
6 changed files
with
123 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
ccInput string | ||
check float32 | ||
func NewCheckCoverageCmd() *cobra.Command { | ||
var coverProfile string | ||
var threshold float64 | ||
var doNotFail bool | ||
|
||
checkCoverageCmd = &cobra.Command{ | ||
checkCoverageCmd := &cobra.Command{ | ||
Use: "check-coverage", | ||
Short: "Checks total coverage against thresholds", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
in := viper.GetString("in") | ||
check := cmd.Flags().Lookup("check").Value | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
result, err := exec.Command("go", "tool", "cover", "-func", coverProfile).Output() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
content := string(result) | ||
index := strings.Index(content, "total") | ||
line := content[index:] | ||
re := regexp.MustCompile(`([0-9]*\.?[0-9]*)\s*%`) | ||
match := re.FindStringSubmatch(line) | ||
totalCoverage, err := strconv.ParseFloat(match[1], 32) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if totalCoverage < threshold { | ||
msg := fmt.Sprintf( | ||
"total coverage %.2f is lower than threshold %.2f", | ||
totalCoverage, | ||
threshold, | ||
) | ||
if doNotFail { | ||
log.Warn(msg) | ||
} else { | ||
log.Fatalf(msg) | ||
} | ||
} | ||
|
||
log.Infof("Input:\t%s", in) | ||
log.Infof("Check:\t%s%%", check) | ||
return nil | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
checkCoverageCmd.Flags().StringVar(&ccInput, "rInput", "", "rInput") | ||
checkCoverageCmd.Flags().Float32VarP(&check, "check", "c", 100, "check") | ||
checkCoverageCmd.MarkFlagRequired("rInput") | ||
f := checkCoverageCmd.Flags() | ||
f.StringVarP(&coverProfile, "coverprofile", "c", "coverage.out", "coverprofile") | ||
f.Float64VarP(&threshold, "threshold", "t", 100, "threshold") | ||
f.BoolVarP(&doNotFail, "do-not-fail", "d", false, "do-not-fail") | ||
|
||
return checkCoverageCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
package main | ||
|
||
import "github.com/farbodsalimi/dokimi/cmd" | ||
import ( | ||
"github.com/farbodsalimi/dokimi/cmd" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Version is the version of this binary. It'll be overridden as part of the build process through linker flags. | ||
var Version = "development" | ||
|
||
func init() { | ||
log.SetFormatter(&log.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
} | ||
|
||
func main() { | ||
cmd.Execute() | ||
err := cmd.Execute(Version) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} |