diff options
author | 6543 <6543@obermui.de> | 2020-08-15 19:13:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-15 13:13:07 -0400 |
commit | ee97e6a66a7bec9547af31c93a5eef7c19a32c54 (patch) | |
tree | 1143ebd8f6abd6027c52e99d71534f141eaecd8e /vendor/code.gitea.io/gitea-vet/checks/migrations.go | |
parent | eb60a5d05478f56bf15220f49ed78f0f72af5698 (diff) | |
download | gitea-ee97e6a66a7bec9547af31c93a5eef7c19a32c54.tar.gz gitea-ee97e6a66a7bec9547af31c93a5eef7c19a32c54.zip |
Update gitea-vet to v0.2.1 (#12282)
* change to new code location
* vendor
* tagged version v0.2.0
* gitea-vet v0.2.1
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'vendor/code.gitea.io/gitea-vet/checks/migrations.go')
-rw-r--r-- | vendor/code.gitea.io/gitea-vet/checks/migrations.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/vendor/code.gitea.io/gitea-vet/checks/migrations.go b/vendor/code.gitea.io/gitea-vet/checks/migrations.go new file mode 100644 index 0000000000..e3fe570f75 --- /dev/null +++ b/vendor/code.gitea.io/gitea-vet/checks/migrations.go @@ -0,0 +1,77 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package checks + +import ( + "errors" + "os/exec" + "strings" + + "golang.org/x/tools/go/analysis" +) + +var Migrations = &analysis.Analyzer{ + Name: "migrations", + Doc: "check migrations for black-listed packages.", + Run: checkMigrations, +} + +var ( + migrationDepBlockList = []string{ + "code.gitea.io/gitea/models", + } + migrationImpBlockList = []string{ + "code.gitea.io/gitea/modules/structs", + } +) + +func checkMigrations(pass *analysis.Pass) (interface{}, error) { + if !strings.EqualFold(pass.Pkg.Path(), "code.gitea.io/gitea/models/migrations") { + return nil, nil + } + + if _, err := exec.LookPath("go"); err != nil { + return nil, errors.New("go was not found in the PATH") + } + + depsCmd := exec.Command("go", "list", "-f", `{{join .Deps "\n"}}`, "code.gitea.io/gitea/models/migrations") + depsOut, err := depsCmd.Output() + if err != nil { + return nil, err + } + + deps := strings.Split(string(depsOut), "\n") + for _, dep := range deps { + if stringInSlice(dep, migrationDepBlockList) { + pass.Reportf(0, "code.gitea.io/gitea/models/migrations cannot depend on the following packages: %s", migrationDepBlockList) + return nil, nil + } + } + + impsCmd := exec.Command("go", "list", "-f", `{{join .Imports "\n"}}`, "code.gitea.io/gitea/models/migrations") + impsOut, err := impsCmd.Output() + if err != nil { + return nil, err + } + + imps := strings.Split(string(impsOut), "\n") + for _, imp := range imps { + if stringInSlice(imp, migrationImpBlockList) { + pass.Reportf(0, "code.gitea.io/gitea/models/migrations cannot import the following packages: %s", migrationImpBlockList) + return nil, nil + } + } + + return nil, nil +} + +func stringInSlice(needle string, haystack []string) bool { + for _, h := range haystack { + if strings.EqualFold(needle, h) { + return true + } + } + return false +} |