Skip to content

Commit

Permalink
Merge pull request #63 from k1LoW/ls-files
Browse files Browse the repository at this point in the history
Add `octocov ls-files`
  • Loading branch information
k1LoW authored Sep 15, 2021
2 parents 176d170 + 31cbc7e commit 3f839f0
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
142 changes: 142 additions & 0 deletions cmd/lsFiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright © 2021 Ken'ichiro Oyama <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/fatih/color"
"github.com/k1LoW/octocov/config"
"github.com/k1LoW/octocov/report"
"github.com/lucasb-eyer/go-colorful"
"github.com/spf13/cobra"
)

// lsFilesCmd represents the lsFiles command
var lsFilesCmd = &cobra.Command{
Use: "ls-files",
Short: "list files logged in code coverage report",
Long: `list files logged in code coverage report.`,
RunE: func(cmd *cobra.Command, args []string) error {
c := config.New()
if err := c.Load(configPath); err != nil {
return err
}
c.Build()
if !c.CoverageConfigReady() {
return errors.New("invalid .octocov.yml")
}
r, err := report.New()
if err != nil {
return err
}
path := c.Coverage.Path
if reportPath != "" {
path = reportPath
}
if err := r.MeasureCoverage(path); err != nil {
return err
}
t := 0
sort.Slice(r.Coverage.Files, func(i int, j int) bool {
if r.Coverage.Files[i].Total > t {
t = r.Coverage.Files[i].Total
}
return r.Coverage.Files[i].File < r.Coverage.Files[j].File
})

if len(r.Coverage.Files) == 0 {
return nil
}
wd, err := os.Getwd()
if err != nil {
return err
}
prefix := generatePrefix(wd, r.Coverage.Files[0].File)
for _, f := range r.Coverage.Files {
cover := float64(f.Covered) / float64(f.Total) * 100
cl := c.CoverageColor(cover)
c, err := detectTermColor(cl)
if err != nil {
return err
}
w := len(strconv.Itoa(t))*2 + 1
cmd.Printf("%s [%s] %s\n", c.Sprint(fmt.Sprintf("%5s%%", fmt.Sprintf("%.1f", cover))), fmt.Sprintf(fmt.Sprintf("%%%ds", w), fmt.Sprintf("%d/%d", f.Covered, f.Total)), strings.TrimPrefix(f.File, prefix))
}

return nil
},
}

func generatePrefix(wd, p string) string {
prefix := p
for {
if strings.HasSuffix(wd, prefix) {
prefix += "/"
break
}
if prefix == "." || prefix == "/" {
prefix = "/"
break
}
prefix = filepath.Dir(prefix)
}
return prefix
}

func detectTermColor(cl string) (*color.Color, error) {
termGreen, _ := colorful.Hex("#4e9a06")
termYellow, _ := colorful.Hex("#c4a000")
termRed, _ := colorful.Hex("#cc0000")
tc, err := colorful.Hex(cl)
if err != nil {
return nil, err
}
dg := tc.DistanceLab(termGreen)
dy := tc.DistanceLab(termYellow)
dr := tc.DistanceLab(termRed)
switch {
case dg <= dy && dg <= dr:
c := color.New(color.FgGreen)
c.EnableColor()
return c, nil
case dy <= dg && dy <= dr:
c := color.New(color.FgYellow)
c.EnableColor()
return c, nil
default:
c := color.New(color.FgRed)
c.EnableColor()
return c, nil
}
}

func init() {
rootCmd.AddCommand(lsFilesCmd)
lsFilesCmd.Flags().StringVarP(&reportPath, "report", "r", "", "coverage report file path")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/k1LoW/duration v1.1.0
github.com/k1LoW/osfs v0.1.1
github.com/lestrrat-go/backoff/v2 v2.0.8
github.com/lucasb-eyer/go-colorful v1.0.3
github.com/mauri870/gcsfs v0.0.0-20210217184550-8b539458430a
github.com/oklog/ulid/v2 v2.0.2
github.com/olekukonko/tablewriter v0.0.5
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBB
github.com/lestrrat-go/option v1.0.0 h1:WqAWL8kh8VcSoD6xjSH34/1m8yxluXQbDeKNfvFeEO4=
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down

0 comments on commit 3f839f0

Please sign in to comment.