Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Integrated the staticcheck command
Browse files Browse the repository at this point in the history
Integrate staticcheck as an extra command

Fixes #91
  • Loading branch information
pieterclaerhout committed May 22, 2020
1 parent 1343c5a commit 4f2ffd1
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 12 deletions.
9 changes: 9 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"$go"
]
},
{
"label": "go-james | staticcheck",
"command": "./build/go-james staticcheck",
"type": "shell",
"group": "build",
"problemMatcher": [
"$go"
]
},
{
"label": "go-james | purge github readme",
"command": "hub-purge pieterclaerhout/go-james",
Expand Down
2 changes: 2 additions & 0 deletions cmd/go-james/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/pieterclaerhout/go-james/cmd/dockerimage"
"github.com/pieterclaerhout/go-james/cmd/staticcheck"

"github.com/pieterclaerhout/go-james/cmd/builder"
"github.com/pieterclaerhout/go-james/cmd/cleaner"
Expand Down Expand Up @@ -72,6 +73,7 @@ func main() {
app.AddCommand(installer.InstallCmd)
app.AddCommand(packager.PackageCmd)
app.AddCommand(runner.RunCmd)
app.AddCommand(staticcheck.StaticcheckCmd)
app.AddCommand(tester.TestCmd)
app.AddCommand(uninstaller.UninstallCmd)
app.AddCommand(version.VersionCmd)
Expand Down
23 changes: 23 additions & 0 deletions cmd/staticcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package staticcheck

import (
"github.com/pieterclaerhout/go-james/internal"
"github.com/pieterclaerhout/go-james/internal/staticchecker"
"github.com/tucnak/climax"
)

// StaticcheckCmd defines the build command
var StaticcheckCmd = climax.Command{
Name: "staticcheck",
Brief: "Perform a static analysis of the code using staticcheck",
Help: "Perform a static analysis of the code using staticcheck",
Flags: []climax.Flag{},
Handle: func(ctx climax.Context) int {

tool := staticchecker.StaticChecker{}

executor := internal.NewExecutor("")
return executor.RunTool(tool, true)

},
}
3 changes: 3 additions & 0 deletions go-james.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
},
"test": {
"extra_args": []
},
"staticcheck": {
"checks": ["all", "-ST1005", "-ST1000"]
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirkon/goproxy v1.4.8
github.com/tucnak/climax v0.0.0-20180716104603-da4c02f3b1f8
golang.org/x/tools v0.0.0-20200521211927-2b542361a4fc // indirect
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
)
30 changes: 30 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
Expand All @@ -22,6 +29,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rotisserie/eris v0.4.0 h1:wfZW5hp90Y386s54DoJDK2Th3ycZotiBGM7b5b5aIHI=
github.com/rotisserie/eris v0.4.0/go.mod h1:lODN/gtqebxPHRbCcWeCYOE350FC2M3V/oAPT2wKxAU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
Expand All @@ -44,18 +52,40 @@ github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tucnak/climax v0.0.0-20180716104603-da4c02f3b1f8 h1:RcJG0MV43QdBqPJq6ChadXDSGCyulb4jCrSTKEMX6fk=
github.com/tucnak/climax v0.0.0-20180716104603-da4c02f3b1f8/go.mod h1:aN8AHR3MaHF61SaAxoSwsOS/AjZrRJeYAlrs4mg3ugk=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d h1:/iIZNFGxc/a7C3yWjGcnboV+Tkc7mxr+p6fDztwoxuM=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200521211927-2b542361a4fc h1:6m2YO+AmBApbUOmhsghW+IfRyZOY4My4UYvQQrEpHfY=
golang.org/x/tools v0.0.0-20200521211927-2b542361a4fc/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2020.1.4 h1:UoveltGrhghAA7ePc+e+QYDHXrBps2PqFZiHkGR/xK8=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
2 changes: 1 addition & 1 deletion internal/common/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func (e Encoding) ToFormattedString(data interface{}) string {

var out bytes.Buffer
json.Indent(&out, b, "", " ")
return string(out.Bytes())
return out.String()

}
20 changes: 11 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const FileName = "go-james.json"

// Config defines the project configuration
type Config struct {
Project ProjectConfig `json:"project"` // Project contains the general project variables
Build BuildConfig `json:"build"` // Build contains the build specific project variables
Package PackageConfig `json:"package"` // Build contains the package specific project variables
Run RunConfig `json:"run"` // Build contains the run specific project variables
Test TestConfig `json:"test"` // Build contains the test specific project variables
Project ProjectConfig `json:"project"` // Project contains the general project variables
Build BuildConfig `json:"build"` // Build contains the build specific project variables
Package PackageConfig `json:"package"` // Build contains the package specific project variables
Run RunConfig `json:"run"` // Build contains the run specific project variables
Test TestConfig `json:"test"` // Build contains the test specific project variables
Staticcheck StaticcheckConfig `json:"staticcheck"` // Staticcheck contains the settings for staticcheck
}

// ProjectConfig contains the general project variables
Expand Down Expand Up @@ -52,6 +53,11 @@ type PackageConfig struct {
IncludeReadme bool `json:"include_readme"` // Include the readme when packaging or not
}

// StaticcheckConfig contains the settings for staticcheck
type StaticcheckConfig struct {
Checks []string `json:"checks"` // The checks which should be executed
}

// TestConfig contains the test specific configuration settings
type TestConfig struct {
ExtraArgs []string `json:"extra_args"` // The extra arguments to pass to the test command
Expand Down Expand Up @@ -87,10 +93,6 @@ func NewConfigFromPath(path string) (Config, error) {
return config, errors.New("Config setting Project.Version should not be empty")
}

// if config.Project.Package == "" {
// return config, errors.New("Config setting Project.Package should not be empty")
// }

if config.Project.MainPackage == "" {
return config, errors.New("Config setting Project.MainPackage should not be empty")
}
Expand Down
3 changes: 3 additions & 0 deletions internal/creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func (creator Creator) createConfig(project common.Project, cfg config.Config) e
Test: config.TestConfig{
ExtraArgs: []string{},
},
Staticcheck: config.StaticcheckConfig{
Checks: []string{"all", "-ST1005", "-ST1000"},
},
}

return creator.WriteJSONFileIfNotExists(configPath, cfg)
Expand Down
3 changes: 2 additions & 1 deletion internal/creator/vscode_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ func newVisualStudioCodeTaskList(cfg config.Config, createGitRepo bool) *visualS
newVisualStudioCodeTask(cfg, "clean", "go-james clean"),
newVisualStudioCodeTask(cfg, "tests", "go-james test"),
newVisualStudioCodeTask(cfg, "run", "go-james run"),
newVisualStudioCodeTask(cfg, "staticcheck", "go-james staticcheck"),
newVisualStudioCodeTask(cfg, "docker-image", "go-james docker-image"),
newVisualStudioCodeTask(cfg, "install", "go-james install"),
newVisualStudioCodeTask(cfg, "uninstall", "go-james uninstall"),
newVisualStudioCodeTask(cfg, "run (debug)", "DEBUG=1 go-james run"),
},
}
if createGitRepo == true {
if createGitRepo {
result.Tasks = append(result.Tasks,
newVisualStudioCodeTask(cfg, "push to github", "git push --set-upstream origin"),
)
Expand Down
2 changes: 1 addition & 1 deletion internal/dockerimager/dockerimager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (dockerImager DockerImager) Execute(project common.Project, cfg config.Conf
dockerFilePath := project.RelPath(dockerFileName)

if !dockerImager.FileExists(dockerFilePath) {
return errors.New("No \"" + dockerFileName + "\" found in the project root")
return errors.New(dockerFileName + "\" was not found in the project root")
}

runCmd := []string{"docker", "build", "-t", cfg.Project.Name, "."}
Expand Down
57 changes: 57 additions & 0 deletions internal/staticchecker/staticchecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package staticchecker

import (
"strings"

"github.com/kballard/go-shellquote"
"github.com/pieterclaerhout/go-james/internal/common"
"github.com/pieterclaerhout/go-james/internal/config"
)

const staticcheckPackagePath = "honnef.co/go/tools/cmd/staticcheck"

// StaticChecker implements the "staticcheck" command
type StaticChecker struct {
common.CommandRunner
common.FileSystem
common.Golang
}

// Execute executes the command
func (staticChecker StaticChecker) Execute(project common.Project, cfg config.Config) error {

staticcheckCmdPath := staticChecker.GoBin("staticcheck")

if !staticChecker.FileExists(staticcheckCmdPath) {
staticChecker.LogPathCreation("Installing:", staticcheckCmdPath)
}

env := map[string]string{}
installCmd := []string{"go", "get", "-u", staticcheckPackagePath}
if output, err := staticChecker.RunReturnOutput(installCmd, project.Path, env); err != nil {
staticChecker.LogError(output)
return err
}

staticcheckCmd := []string{staticcheckCmdPath}
if len(cfg.Staticcheck.Checks) > 0 {
staticcheckCmd = append(staticcheckCmd, "-checks")
staticcheckCmd = append(staticcheckCmd, strings.Join(cfg.Staticcheck.Checks, ","))
}
staticcheckCmd = append(staticcheckCmd, "./...")

staticChecker.LogInfo("> Running:", shellquote.Join(staticcheckCmd...))

err := staticChecker.RunToStdout(staticcheckCmd, project.Path, map[string]string{})
if err != nil && err.Error() != "exit status 1" {
return err
}

return nil

}

// RequiresBuild indicates if a build is required before running the command
func (staticChecker StaticChecker) RequiresBuild() bool {
return false
}

0 comments on commit 4f2ffd1

Please sign in to comment.