Skip to content

Commit

Permalink
Add init command
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Sep 19, 2023
1 parent b8d6c71 commit a82ec51
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gostyle.yml.init
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
analyzers:
disable:
- mixedcaps # disable mixedcaps analyzer. because the underscores analyzer is more detailed.
# analyzers-settings:
# varnames:
# small-varname-max: 4 # max length of variable name for small scope (default: -1)
# medium-varname-max: 8 # max length of variable name for medium scope (default: -1)
# large-varname-max: 16 # max length of variable name for large scope (default: -1)
# very-large-varname-max: 32 # max length of variable name for very large scope (default: -1)
44 changes: 44 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import (
_ "embed"
"fmt"
"os"
"path/filepath"

"github.com/k1LoW/gostyle/analyzer/decisions/getters"
"github.com/k1LoW/gostyle/analyzer/decisions/nilslices"
"github.com/k1LoW/gostyle/analyzer/decisions/pkgnames"
Expand All @@ -17,7 +22,22 @@ import (
"golang.org/x/tools/go/analysis/unitchecker"
)

//go:embed .gostyle.yml.init
var defaultConfig []byte

func main() {
if len(os.Args) == 1 {
usage()
os.Exit(0)
}
if len(os.Args) == 2 && os.Args[1] == "init" {
if err := generateConfig(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
os.Exit(0)
}

unitchecker.Main(
config.Loader,
getters.AnalyzerWithConfig,
Expand All @@ -34,3 +54,27 @@ func main() {
varnames.AnalyzerWithConfig,
)
}

func usage() {
progname := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, `%[1]s is a set of analyzers for coding styles.
Usage of %[1]s:
%.16[1]s init # generate .gostyle.yml
%.16[1]s unit.cfg # execute analysis specified by config file
%.16[1]s help # general help, including listing analyzers and flags
%.16[1]s help name # help on specific analyzer and its flags
`, progname)
}

func generateConfig() error {
const name = ".gostyle.yml"
if _, err := os.Stat(name); err == nil {
return fmt.Errorf("%s already exists", name)
}
if err := os.WriteFile(name, defaultConfig, os.ModePerm); err != nil {
return err
}
_, _ = fmt.Fprintf(os.Stderr, "%s is generated\n", name)
return nil
}

0 comments on commit a82ec51

Please sign in to comment.