Add namespaces for commands

This commit is contained in:
kolaente 2020-09-02 14:08:22 +02:00
parent c081e40d02
commit 3fadc1108f
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 90 additions and 86 deletions

View File

@ -53,16 +53,12 @@ var (
// Aliases are mage aliases of targets
Aliases = map[string]interface{}{
"integration-test": IntegrationTest,
"fmt-check": FmtCheck,
"do-the-swag": DoTheSwag,
"got-swag": GotSwag,
"misspell-check": MisspellCheck,
"ineffassign-check": IneffassignCheck,
"gocyclo-check": GocycloCheck,
"static-check": StaticCheck,
"gosec-check": GoSecCheck,
"goconst-check": GoconstCheck,
"do-the-swag": DoTheSwag,
"check:go-sec": Check.GoSec,
"check:got-swag": Check.GotSwag,
"release:build-deb": Release.BuildDeb,
"release:compress-build": Release.BuildDeb,
"release:os-package": Release.OsPackage,
}
)
@ -214,50 +210,43 @@ func calculateHashOfFile(path string) (hash string, err error) {
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
// Cleans all build, executable and bindata files
func Clean() error {
if err := exec.Command("go", "clean", "./...").Run(); err != nil {
return err
}
if err := os.Remove(Executable); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.RemoveAll(DIST); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.RemoveAll(BinLocation); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// Runs all tests except integration tests
func Test() {
// We run everything sequentially and not in parallel to prevent issues with real test databases
args := append([]string{"test", Goflags[0], "-p", "1"}, ApiPackages...)
runAndStreamOutput("go", args...)
}
// Runs the tests and builds the coverage html file from coverage output
func TestCoverage() {
mg.Deps(Test)
runAndStreamOutput("go", "tool", "cover", "-html=cover.out", "-o", "cover.html")
}
// Runs the integration tests
func IntegrationTest() {
// We run everything sequentially and not in parallel to prevent issues with real test databases
runAndStreamOutput("go", "test", Goflags[0], "-p", "1", PACKAGE+"/pkg/integrations")
}
// Formats the code using go fmt
func Fmt() {
args := append([]string{"-s", "-w"}, GoFiles...)
runAndStreamOutput("gofmt", args...)
}
// Generates the swagger docs from the code annotations
func DoTheSwag() {
checkAndInstallGoTool("swag", "github.com/swaggo/swag/cmd/swag")
runAndStreamOutput("swag", "init", "-g", "./pkg/routes/routes.go", "--parseDependency", "-d", RootPath, "-o", RootPath+"/pkg/swagger")
}
type Test mg.Namespace
// Runs all tests except integration tests
func (Test) Unit() {
// We run everything sequentially and not in parallel to prevent issues with real test databases
args := append([]string{"test", Goflags[0], "-p", "1"}, ApiPackages...)
runAndStreamOutput("go", args...)
}
// Runs the tests and builds the coverage html file from coverage output
func (Test) Coverage() {
mg.Deps(Test.Unit)
runAndStreamOutput("go", "tool", "cover", "-html=cover.out", "-o", "cover.html")
}
// Runs the integration tests
func (Test) Integration() {
// We run everything sequentially and not in parallel to prevent issues with real test databases
runAndStreamOutput("go", "test", Goflags[0], "-p", "1", PACKAGE+"/pkg/integrations")
}
type Check mg.Namespace
// Checks if the code is properly formatted with go fmt
func FmtCheck() error {
func (Check) Fmt() error {
args := append([]string{"-s", "-d"}, GoFiles...)
c := exec.Command("gofmt", args...)
out, err := c.Output()
@ -276,20 +265,14 @@ func FmtCheck() error {
}
// Runs golint on all packages
func Lint() {
func (Check) Lint() {
checkAndInstallGoTool("golint", "golang.org/x/lint/golint")
args := append([]string{"-set_exit_status"}, ApiPackages...)
runAndStreamOutput("golint", args...)
}
// Generates the swagger docs from the code annotations
func DoTheSwag() {
checkAndInstallGoTool("swag", "github.com/swaggo/swag/cmd/swag")
runAndStreamOutput("swag", "init", "-g", "./pkg/routes/routes.go", "--parseDependency", "-d", RootPath, "-o", RootPath+"/pkg/swagger")
}
// Checks if the swagger docs need to be re-generated from the code annotations
func GotSwag() {
func (Check) GotSwag() {
// The check is pretty cheaply done: We take the hash of the swagger.json file, generate the docs,
// hash the file again and compare the two hashes to see if anything changed. If that's the case,
// regenerating the docs is necessary.
@ -318,31 +301,31 @@ func GotSwag() {
}
// Checks the source code for misspellings
func MisspellCheck() {
func (Check) Misspell() {
checkAndInstallGoTool("misspell", "github.com/client9/misspell/cmd/misspell")
runAndStreamOutput("misspell", append([]string{"-error"}, GoFiles...)...)
}
// Checks the source code for ineffectual assigns
func IneffassignCheck() {
func (Check) Ineffassign() {
checkAndInstallGoTool("ineffassign", "github.com/gordonklaus/ineffassign")
runAndStreamOutput("ineffassign", GoFiles...)
}
// Checks for the cyclomatic complexity of the source code
func GocycloCheck() {
func (Check) Gocyclo() {
checkAndInstallGoTool("gocyclo", "github.com/fzipp/gocyclo")
runAndStreamOutput("gocyclo", append([]string{"-over", "49"}, GoFiles...)...)
}
// Statically analyzes the source code about a range of different problems
func StaticCheck() {
func (Check) Static() {
checkAndInstallGoTool("staticcheck", "honnef.co/go/tools/cmd/staticcheck")
runAndStreamOutput("staticcheck", ApiPackages...)
}
// Checks the source code for potential security issues
func GoSecCheck() {
func (Check) GoSec() {
if err := exec.Command("gosec").Run(); err != nil && strings.Contains(err.Error(), "executable file not found") {
fmt.Println("Please manually install gosec by running")
fmt.Println("curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | bash -s -- -b $GOPATH/bin v2.2.0")
@ -352,81 +335,102 @@ func GoSecCheck() {
}
// Checks for repeated strings that could be replaced by a constant
func GoconstCheck() {
func (Check) Goconst() {
checkAndInstallGoTool("goconst", "github.com/jgautheron/goconst/cmd/goconst")
runAndStreamOutput("goconst", ApiPackages...)
}
// Runs fmt-check, lint, got-swag, misspell-check, ineffasign-check, gocyclo-check, static-check, gosec-check, goconst-check all in parallel
func Checks() {
func (Check) All() {
mg.Deps(
FmtCheck,
Lint,
GotSwag,
MisspellCheck,
IneffassignCheck,
GocycloCheck,
StaticCheck,
GoSecCheck,
GoconstCheck,
Check.Fmt,
Check.Lint,
Check.GotSwag,
Check.Misspell,
Check.Ineffassign,
Check.Gocyclo,
Check.Static,
Check.GoSec,
Check.Goconst,
)
}
type Build mg.Namespace
// Cleans all build, executable and bindata files
func (Build) Clean() error {
if err := exec.Command("go", "clean", "./...").Run(); err != nil {
return err
}
if err := os.Remove(Executable); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.RemoveAll(DIST); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.RemoveAll(BinLocation); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// Generates static content into the final binary
func Generate() {
func (Build) Generate() {
runAndStreamOutput("go", "generate", PACKAGE+"/pkg/static")
}
// Builds a vikunja binary, ready to run
func Build() {
mg.Deps(Generate)
func (Build) Build() {
mg.Deps(Build.Generate)
runAndStreamOutput("go", "build", Goflags[0], "-tags", Tags, "-ldflags", "-s -w "+Ldflags, "-o", Executable)
}
func Release() {
type Release mg.Namespace
func (Release) Release() {
}
func ReleaseDirs() {
func (Release) Dirs() {
}
func ReleaseWindows() {
func (Release) Windows() {
}
func ReleaseLinux() {
func (Release) Linux() {
}
func ReleaseDarwin() {
func (Release) Darwin() {
}
func ReleaseCompress() {
func (Release) Compress() {
}
func CompressBuild() {
func (Release) CompressBuild() {
}
func ReleaseCopy() {
func (Release) Copy() {
}
func ReleaseOsPackage() {
func (Release) OsPackage() {
}
func ReleaseZip() {
func (Release) Zip() {
}
func BuildDeb() {
func (Release) BuildDeb() {
}
func Reprepro() {
func (Release) Reprepro() {
}