80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema/lint"
|
|
)
|
|
|
|
var sqlite_lint = &cobra.Command{
|
|
Use: "sqlite_lint <path/to/schema.sql>",
|
|
Short: "Lint a SQLite schema",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
if len(args) != 1 {
|
|
return errors.New("required: filepath as first argument")
|
|
}
|
|
return nil
|
|
},
|
|
|
|
Run: func(rootCmd *cobra.Command, args []string) {
|
|
filename := args[0]
|
|
fmt.Printf("-----------------\nLinting %s\n", filename)
|
|
schema, err := schema.SchemaFromSQLFile(args[0])
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
is_failure := false
|
|
// Execute each check against the database
|
|
for _, check := range lint.Checks {
|
|
// Checks can be disabled via Github config / environment variables
|
|
if !is_check_enabled(check) {
|
|
continue
|
|
}
|
|
results := check.Execute(schema)
|
|
|
|
// If there are results, print them as lint errors
|
|
if len(results) > 0 {
|
|
is_failure = true
|
|
fmt.Printf(RED+"Check '%s' failed:\n"+RESET, check.Name)
|
|
for _, result := range results {
|
|
fmt.Printf(RED+"- %s: %s.%s\n"+RESET, result.ErrorMsg, result.TableName, result.ColumnName)
|
|
}
|
|
fmt.Printf(RED+"Explanation: %s\n\n"+RESET, check.Explanation)
|
|
}
|
|
}
|
|
if is_failure {
|
|
fmt.Println(RED + "Errors found" + RESET)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println(GREEN + "Success" + RESET)
|
|
},
|
|
}
|
|
|
|
// github_actions_input_env_var converts an input name to the corresponding
|
|
// environment variable name used by GitHub Actions.
|
|
func github_actions_input_env_var(name string) string {
|
|
// GitHub normalizes both hyphens and underscores to underscores, then uppercases the name
|
|
normalized := strings.NewReplacer("-", "_", " ", "_").Replace(name)
|
|
return "INPUT_" + strings.ToUpper(normalized)
|
|
}
|
|
|
|
// Setting the environment variable INPUT_REQUIRE_NOT_NULL="false" disables the "require_not_null" check
|
|
func is_check_enabled(c lint.Check) bool {
|
|
val, is_set := os.LookupEnv(github_actions_input_env_var(c.Name))
|
|
if !is_set {
|
|
// Enable all checks by default
|
|
return true
|
|
}
|
|
// Anything except "false" is true
|
|
return val != "false"
|
|
}
|