Add main program with 'sqlite_lint' subcommand
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				CI / release-test (push) Failing after 16s
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	CI / release-test (push) Failing after 16s
				
			This commit is contained in:
		
							parent
							
								
									c402c775a3
								
							
						
					
					
						commit
						e888fe0ec1
					
				
							
								
								
									
										30
									
								
								cmd/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								cmd/main.go
									
									
									
									
									
										Normal file
									
								
							| @ -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" | ||||
							
								
								
									
										79
									
								
								cmd/subcmd_sqlite_lint.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								cmd/subcmd_sqlite_lint.go
									
									
									
									
									
										Normal file
									
								
							| @ -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 <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" | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user