Skip to content

Commit

Permalink
Add new settings to cli tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Jan 26, 2021
1 parent 109cd93 commit ad81b41
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 36 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ go get -u github.com/tetafro/godot/cmd/godot

or download binary from [releases page](https://github.com/tetafro/godot/releases).

## Config

You can specify options using config file. If no config provided the following
defaults are used:

```yaml
# Which comments to check:
# declarations - for top level declaration comments (default);
# toplevel - for top level comments;
# all - for all comments.
scope: toplevel

# List pf regexps for excluding particular comment lines from check.
exclude:

# Check periods at the end of sentences.
period: true

# Check that first letter of each sentence is capital.
capital: false
```
## Run
```sh
Expand Down
90 changes: 54 additions & 36 deletions cmd/godot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,55 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/tetafro/godot"
yaml "gopkg.in/yaml.v2"
)

// version is the application version. It is set to the latest git tag in CI.
var version = "master"

const defaultConfigFile = "config.yaml"

var defaultSettings = godot.Settings{
Scope: godot.TopLevelScope,
Period: true,
Capital: false,
}

// nolint: lll
const usage = `Usage:
godot [OPTION] [FILES]
Options:
-c, --capital check that sentences start with a capital letter
-s, --scope set scope for check
declarations - for top level declaration comments (default)
toplevel - for top level comments
all - for all comments
-c, --config path to config file
-f, --fix fix issues, and print fixed version to stdout
-w, --write fix issues, and write result to original file
-h, --help show this message
-v, --version show version
-w, --write fix issues, and write result to original file`
-v, --version show version`

// nolint:maligned
type arguments struct {
help bool
version bool
config string
fix bool
write bool
scope string
files []string
capital bool
help bool
version bool
}

// nolint: funlen
func main() {
// Read command line arguments
args, err := readArgs()
if err != nil {
fatalf("Error: %v", err)
}

// Info messages
if args.help {
fmt.Println(usage)
os.Exit(0)
Expand All @@ -55,16 +63,16 @@ func main() {
os.Exit(0)
}

settings := godot.Settings{
Scope: godot.Scope(args.scope),
Period: true,
Capital: args.capital,
// Get settings from file or get defaults
settings, err := getSettings(args.config)
if err != nil {
fatalf("Error: %v", err)
}

// Parse files
var paths []string
var files []*ast.File
fset := token.NewFileSet()

for _, path := range args.files {
if _, err := os.Stat(path); os.IsNotExist(err) {
fatalf("Path '%s' does not exist", path)
Expand All @@ -79,6 +87,7 @@ func main() {
}
}

// Run linter
for i := range files {
switch {
case args.fix:
Expand All @@ -103,7 +112,6 @@ func main() {
}
}

// nolint: funlen
func readArgs() (args arguments, err error) {
if len(os.Args) < 2 { // nolint: gomnd
return arguments{}, fmt.Errorf("not enough arguments")
Expand Down Expand Up @@ -131,44 +139,54 @@ func readArgs() (args arguments, err error) {
args.help = true
case "-v", "--version":
args.version = true
case "-s", "--scope":
// Next argument must be scope value
case "-c", "--config":
// Next argument must be config file value
if len(input) < i+2 {
return arguments{}, fmt.Errorf("empty scope")
return arguments{}, fmt.Errorf("empty config file")
}
arg = input[i+1]
args.config = input[i+1]
i++

switch arg {
case string(godot.DeclScope),
string(godot.TopLevelScope),
string(godot.AllScope):
args.scope = arg
default:
return arguments{}, fmt.Errorf("unknown scope '%s'", arg)
}
case "-f", "--fix":
args.fix = true
case "-w", "--write":
args.write = true
case "-c", "--capital":
args.capital = true
default:
return arguments{}, fmt.Errorf("unknown flag '%s'", arg)
}
}

if args.scope == "" {
args.scope = string(godot.DeclScope)
}

if !args.help && !args.version && len(args.files) == 0 {
return arguments{}, fmt.Errorf("files list is empty")
}

return args, nil
}

func getSettings(file string) (godot.Settings, error) {
settings := defaultSettings

if file == "" {
// Check default config file
if _, err := os.Stat(defaultConfigFile); os.IsNotExist(err) {
return settings, nil
}
file = defaultConfigFile
}

data, err := ioutil.ReadFile(file) // nolint: gosec
if err != nil {
return godot.Settings{}, fmt.Errorf(
"read config file %s: %v", defaultConfigFile, err,
)
}
if err := yaml.Unmarshal(data, &settings); err != nil {
return godot.Settings{}, fmt.Errorf(
"parse config file %s: %v", defaultConfigFile, err,
)
}
return settings, nil
}

func findFiles(root string) chan string {
out := make(chan string)

Expand Down
14 changes: 14 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Which comments to check:
# declarations - for top level declaration comments (default);
# toplevel - for top level comments;
# all - for all comments.
scope: toplevel

# List pf regexps for excluding particular comment lines from check.
exclude:

# Check periods at the end of sentences.
period: true

# Check that first letter of each sentence is capital.
capital: false
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/tetafro/godot

go 1.15

require gopkg.in/yaml.v2 v2.4.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 comments on commit ad81b41

Please sign in to comment.