Skip to content

Commit

Permalink
Merge pull request #25 from k1LoW/init
Browse files Browse the repository at this point in the history
Add `init` command to generate .gostyle.yml
  • Loading branch information
k1LoW authored Sep 12, 2023
2 parents 9ca4cf2 + e0b98b1 commit 0be2b08
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gostyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
analyzers:
disable:
- mixedcaps # disable mixedcaps analyzer. because the underscores analyzer is more detailed.
45 changes: 45 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/k1LoW/gostyle/analyzer/decisions/pkgnames"
"github.com/k1LoW/gostyle/analyzer/decisions/recvnames"
"github.com/k1LoW/gostyle/analyzer/decisions/underscores"
"github.com/k1LoW/gostyle/analyzer/effective/ifacenames"
"github.com/k1LoW/gostyle/analyzer/guide/mixedcaps"
"github.com/k1LoW/gostyle/config"
"golang.org/x/tools/go/analysis/unitchecker"

_ "embed"
)

const configPath = ".gostyle.yml"

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

func main() {
progname := filepath.Base(os.Args[0])

flag.Usage = func() {
fmt.Fprintf(os.Stderr, `%[1]s is a set of analyzers for coding styles.
Usage of %[1]s:
%.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
%.16[1]s init # generate config file for -gostyle.config flag
`, progname)
os.Exit(1)
}

flag.Parse()
args := flag.Args()
if len(args) == 0 {
flag.Usage()
}

if args[0] == "init" {
if _, err := os.Stat(configPath); err == nil {
_, _ = fmt.Fprintf(os.Stderr, "%s already exists\n", configPath)
os.Exit(1)
}
if err := os.WriteFile(configPath, defaultConfig, os.ModePerm); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("%s is generated\n", configPath)
os.Exit(0)
}

unitchecker.Main(
config.Loader,
ifacenames.Analyzer,
Expand Down

0 comments on commit 0be2b08

Please sign in to comment.