Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
added shellcheck-exclude option
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jul 3, 2017
1 parent 319de25 commit fbc0de5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
18 changes: 16 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/spf13/cobra"
)

var ignores []string
var (
ignores []string
shellcheckExcludes []string
)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down Expand Up @@ -40,8 +43,13 @@ func check(file string) error {
status.Ignore(file)
return nil
}
var options = sh.Options{
Shellcheck: sh.ShellcheckOptions{
Exclude: shellcheckExcludes,
},
}
var errors []error
for _, check := range sh.Checkers() {
for _, check := range sh.Checkers(options) {
if err := check.Check(file); err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -73,6 +81,12 @@ func init() {
[]string{},
"ignore specific folder of file patterns",
)
RootCmd.PersistentFlags().StringSliceVar(
&shellcheckExcludes,
"shellcheck-exclude",
[]string{},
"pass arguments to shellcheck --exclude option",
)
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand Down
14 changes: 13 additions & 1 deletion sh/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import (
"fmt"
"os/exec"
"runtime"
"strings"
)

const shellcheckPath = "/tmp/shellcheck"

// ShellcheckOptions pass down options to the shellcheck binary
type ShellcheckOptions struct {
Exclude []string
}

type shellcheck struct {
options ShellcheckOptions
}

// Check a file with shellcheck
Expand All @@ -17,7 +24,12 @@ func (s *shellcheck) Check(file string) error {
if err != nil {
return err
}
out, err := exec.Command(bin, "-x", file).CombinedOutput()
var args = []string{"--external-sources"}
if len(s.options.Exclude) != 0 {
args = append(args, "--exclude", strings.Join(s.options.Exclude, ","))
}
args = append(args, file)
out, err := exec.Command(bin, args...).CombinedOutput()
if err == nil {
return nil
}
Expand Down
9 changes: 7 additions & 2 deletions sh/sh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ type Checker interface {
Install() (string, error)
}

// Options provides options to the underline checkers
type Options struct {
Shellcheck ShellcheckOptions
}

// Checkers all checkers
func Checkers() []Checker {
func Checkers(opts Options) []Checker {
return []Checker{
&shellcheck{},
&shellcheck{opts.Shellcheck},
&shfmt{},
}
}
Expand Down

0 comments on commit fbc0de5

Please sign in to comment.