Skip to content

Commit

Permalink
chore: get cmd passing new CI config
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Aug 28, 2023
1 parent 3595145 commit af20855
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
6 changes: 4 additions & 2 deletions cmd/vale/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ func init() {

func fetch(src, dst string) error {
// Fetch the resource from the web:
resp, err := http.Get(src)
resp, err := http.Get(src) //nolint:gosec,noctx

if err != nil {
return err
} else if resp.StatusCode != 200 {
return fmt.Errorf("could not fetch '%s' (status code '%d').", src, resp.StatusCode)
return fmt.Errorf("could not fetch '%s' (status code '%d')", src, resp.StatusCode)
}

// Create a temp file to represent the archive locally:
Expand All @@ -78,6 +79,7 @@ func fetch(src, dst string) error {
return err
}

resp.Body.Close()
return archiver.Unarchive(tmpfile.Name(), dst)
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/vale/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ func printVerboseAlert(f *core.File, wrap bool) (int, int, int) {

fmt.Printf("\n %s", pterm.Underscore.Sprintf(f.Path))
for _, a := range alerts {
if a.Severity == "suggestion" {
switch a.Severity {
case "suggestion":
level = pterm.Blue(a.Severity)
notifications++
} else if a.Severity == "warning" {
case "warning":
level = pterm.Yellow(a.Severity)
warnings++
} else {
case "error":
level = pterm.Red(a.Severity)
errors++
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/vale/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func fix(args []string, flags *core.CLIFlags) error {
return printJSON(resp)
}

func sync(args []string, flags *core.CLIFlags) error {
func sync(_ []string, flags *core.CLIFlags) error {
cfg, err := core.ReadPipeline("ini", flags, true)
if err != nil {
return err
} else if err := initPath(cfg); err != nil {
} else if err = initPath(cfg); err != nil {
return err
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func sync(args []string, flags *core.CLIFlags) error {
return nil
}

func printConfig(args []string, flags *core.CLIFlags) error {
func printConfig(_ []string, flags *core.CLIFlags) error {
cfg, err := core.ReadPipeline("ini", flags, false)
if err != nil {
return err
Expand All @@ -114,7 +114,7 @@ func printConfig(args []string, flags *core.CLIFlags) error {
return nil
}

func printMetrics(args []string, flags *core.CLIFlags) error {
func printMetrics(args []string, _ *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("ls-metrics", errors.New("one argument expected"))
} else if !core.FileExists(args[0]) {
Expand Down Expand Up @@ -144,7 +144,7 @@ func printMetrics(args []string, flags *core.CLIFlags) error {
return printJSON(computed)
}

func runTag(args []string, flags *core.CLIFlags) error {
func runTag(args []string, _ *core.CLIFlags) error {
if len(args) != 3 {
return core.NewE100("tag", errors.New("three arguments expected"))
}
Expand All @@ -160,7 +160,7 @@ func runTag(args []string, flags *core.CLIFlags) error {
return printJSON(out)
}

func compileRule(args []string, flags *core.CLIFlags) error {
func compileRule(args []string, _ *core.CLIFlags) error {
if len(args) != 1 {
return core.NewE100("compile", errors.New("one argument expected"))
}
Expand All @@ -187,7 +187,7 @@ func compileRule(args []string, flags *core.CLIFlags) error {
return printJSON(rule)
}

func runRule(args []string, flags *core.CLIFlags) error {
func runRule(args []string, _ *core.CLIFlags) error {
if len(args) != 2 {
return core.NewE100("run", errors.New("two arguments expected"))
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/vale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func doLint(args []string, l *lint.Linter, glob string) ([]*core.File, error) {
var err error

length := len(args)
if length == 1 && looksLikeStdin(args[0]) == 1 {
if length == 1 && looksLikeStdin(args[0]) == 1 { //nolint:gocritic
// Case 1:
//
// $ vale "some text in a string"
Expand All @@ -62,9 +62,9 @@ func doLint(args []string, l *lint.Linter, glob string) ([]*core.File, error) {
// Case 3:
//
// $ cat file.md | vale
stdin, err := io.ReadAll(os.Stdin)
if err != nil {
return linted, core.NewE100("doLint", err)
stdin, readErr := io.ReadAll(os.Stdin)
if readErr != nil {
return linted, core.NewE100("doLint", readErr)
}
linted, err = l.LintString(string(stdin))
if err != nil {
Expand All @@ -86,7 +86,7 @@ func main() {
args := pflag.Args()
argc := len(args)

if Flags.Version {
if Flags.Version { //nolint:gocritic
fmt.Println("vale version " + version)
os.Exit(0)
} else if Flags.Help {
Expand Down
21 changes: 9 additions & 12 deletions cmd/vale/pkg_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -10,7 +9,7 @@ import (
"github.com/errata-ai/vale/v2/internal/core"
)

var TEST_DATA = "../../testdata/pkg"
var TestData = "../../testdata/pkg"

func mockPath() (string, error) {
cfg, err := core.NewConfig(&core.CLIFlags{})
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestLocalZip(t *testing.T) {
t.Fatal(err)
}

zip, err := filepath.Abs(filepath.Join(TEST_DATA, "write-good.zip"))
zip, err := filepath.Abs(filepath.Join(TestData, "write-good.zip"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -78,7 +77,7 @@ func TestLocalDir(t *testing.T) {
t.Fatal(err)
}

zip, err := filepath.Abs(filepath.Join(TEST_DATA, "write-good"))
zip, err := filepath.Abs(filepath.Join(TestData, "write-good"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -97,13 +96,13 @@ func TestLocalDir(t *testing.T) {
}
}

func TestLocalComplete(t *testing.T) {
func TestLocalComplete(t *testing.T) { //nolint:dupl
path, err := mockPath()
if err != nil {
t.Fatal(err)
}

zip, err := filepath.Abs(filepath.Join(TEST_DATA, "ISC.zip"))
zip, err := filepath.Abs(filepath.Join(TestData, "ISC.zip"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -129,18 +128,17 @@ func TestLocalComplete(t *testing.T) {
lines := strings.Split(string(b), "\n")

if !core.StringInSlice("bar", lines) {
t.Fatal(fmt.Sprintf("unable to find 'bar' in %v", lines))
t.Fatalf("unable to find 'bar' in %v", lines)
}

}

func TestLocalOnlyStyles(t *testing.T) {
func TestLocalOnlyStyles(t *testing.T) { //nolint:dupl
path, err := mockPath()
if err != nil {
t.Fatal(err)
}

zip, err := filepath.Abs(filepath.Join(TEST_DATA, "OnlyStyles.zip"))
zip, err := filepath.Abs(filepath.Join(TestData, "OnlyStyles.zip"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -166,9 +164,8 @@ func TestLocalOnlyStyles(t *testing.T) {
lines := strings.Split(string(b), "\n")

if !core.StringInSlice("bar", lines) {
t.Fatal(fmt.Sprintf("unable to find 'bar' in %v", lines))
t.Fatalf("unable to find 'bar' in %v", lines)
}

}

func TestNoPkgFound(t *testing.T) {
Expand Down
17 changes: 6 additions & 11 deletions cmd/vale/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func newVocab(path, name string) error {
}

for _, f := range []string{"accept.txt", "reject.txt"} {
ferr = os.WriteFile(filepath.Join(root, f), []byte{}, 0644)
ferr = os.WriteFile(filepath.Join(root, f), []byte{}, os.ModePerm)
}

return ferr
Expand Down Expand Up @@ -76,9 +76,8 @@ func loadPkg(name, urlOrPath, styles string, index int) error {
if fileInfo, err := os.Stat(urlOrPath); err == nil {
if fileInfo.IsDir() {
return loadLocalPkg(name, urlOrPath, styles, index)
} else {
return loadLocalZipPkg(name, urlOrPath, styles, index)
}
return loadLocalZipPkg(name, urlOrPath, styles, index)
}
return download(name, urlOrPath, styles, index)
}
Expand Down Expand Up @@ -163,7 +162,7 @@ func installPkg(dir, name, styles string, index int) error {
return nil
}

func moveDir(old, new string, isVocab bool) error {
func moveDir(old, new string, isVocab bool) error { //nolint:predeclared
files, err := os.ReadDir(old)
if err != nil {
return err
Expand All @@ -180,7 +179,7 @@ func moveDir(old, new string, isVocab bool) error {
return nil
}

func moveAsset(name, old, new string) error {
func moveAsset(name, old, new string) error { //nolint:predeclared
src := filepath.Join(old, name)
dst := filepath.Join(new, name)

Expand All @@ -195,14 +194,10 @@ func moveAsset(name, old, new string) error {
return err
}

if err := cp.Copy(src, dst); err != nil {
return err
}

return nil
return cp.Copy(src, dst)
}

func getLibrary(path string) ([]Style, error) {
func getLibrary(_ string) ([]Style, error) {
styles := []Style{}

resp, err := fetchJSON(library)
Expand Down
3 changes: 2 additions & 1 deletion cmd/vale/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ func getJSON(data interface{}) string {
}

func fetchJSON(url string) ([]byte, error) {
resp, err := http.Get(url)
resp, err := http.Get(url) //nolint:gosec,noctx
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}

Expand Down

0 comments on commit af20855

Please sign in to comment.