From e888fe0ec1f2bccb2ea058c45a31e29bc138e592 Mon Sep 17 00:00:00 2001 From: wispem-wantex Date: Sun, 24 Aug 2025 11:37:23 -0700 Subject: [PATCH] Add main program with 'sqlite_lint' subcommand --- cmd/main.go | 30 +++++++++++++++ cmd/subcmd_sqlite_lint.go | 79 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 cmd/main.go create mode 100644 cmd/subcmd_sqlite_lint.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..9119edf --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +const ( + GREEN = "\033[0;32m" + RED = "\033[0;31m" + RESET = "\033[0m" +) + +func main() { + rootCmd := &cobra.Command{ + Use: "gas", + SilenceErrors: true, + SilenceUsage: true, + } + rootCmd.AddCommand(sqlite_lint) + if err := rootCmd.Execute(); err != nil { + fmt.Println(RED + err.Error() + RESET) + os.Exit(1) + } +} + +// Subcommand "init" +// Subcommand "generate_models" diff --git a/cmd/subcmd_sqlite_lint.go b/cmd/subcmd_sqlite_lint.go new file mode 100644 index 0000000..6d48c05 --- /dev/null +++ b/cmd/subcmd_sqlite_lint.go @@ -0,0 +1,79 @@ +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 ", + 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" +}