cmd.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package cmd
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "github.com/spf13/cobra"
  10. yaml "gopkg.in/yaml.v3"
  11. "github.com/kyleconroy/sqlc/internal/config"
  12. )
  13. // Do runs the command logic.
  14. func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int {
  15. rootCmd := &cobra.Command{Use: "sqlc", SilenceUsage: true}
  16. rootCmd.AddCommand(checkCmd)
  17. rootCmd.AddCommand(genCmd)
  18. rootCmd.AddCommand(initCmd)
  19. rootCmd.AddCommand(versionCmd)
  20. rootCmd.SetArgs(args)
  21. rootCmd.SetIn(stdin)
  22. rootCmd.SetOut(stdout)
  23. rootCmd.SetErr(stderr)
  24. err := rootCmd.Execute()
  25. if err == nil {
  26. return 0
  27. }
  28. if exitError, ok := err.(*exec.ExitError); ok {
  29. return exitError.ExitCode()
  30. }
  31. return 1
  32. }
  33. var version string
  34. var versionCmd = &cobra.Command{
  35. Use: "version",
  36. Short: "Print the sqlc version number",
  37. Run: func(cmd *cobra.Command, args []string) {
  38. if version == "" {
  39. // When no version is set, return the next bug fix version
  40. // after the most recent tag
  41. fmt.Printf("%s\n", "v1.4.1-devel")
  42. } else {
  43. fmt.Printf("%s\n", version)
  44. }
  45. },
  46. }
  47. var initCmd = &cobra.Command{
  48. Use: "init",
  49. Short: "Create an empty sqlc.yaml settings file",
  50. RunE: func(cmd *cobra.Command, args []string) error {
  51. if _, err := os.Stat("sqlc.yaml"); !os.IsNotExist(err) {
  52. return nil
  53. }
  54. blob, err := yaml.Marshal(config.V1GenerateSettings{Version: "1"})
  55. if err != nil {
  56. return err
  57. }
  58. return ioutil.WriteFile("sqlc.yaml", blob, 0644)
  59. },
  60. }
  61. type Env struct {
  62. }
  63. func ParseEnv() Env {
  64. return Env{}
  65. }
  66. var genCmd = &cobra.Command{
  67. Use: "generate",
  68. Short: "Generate Go code from SQL",
  69. Run: func(cmd *cobra.Command, args []string) {
  70. stderr := cmd.ErrOrStderr()
  71. dir, err := os.Getwd()
  72. if err != nil {
  73. fmt.Fprintln(stderr, "error parsing sqlc.json: file does not exist")
  74. os.Exit(1)
  75. }
  76. output, err := Generate(ParseEnv(), dir, stderr)
  77. if err != nil {
  78. os.Exit(1)
  79. }
  80. for filename, source := range output {
  81. os.MkdirAll(filepath.Dir(filename), 0755)
  82. if err := ioutil.WriteFile(filename, []byte(source), 0644); err != nil {
  83. fmt.Fprintf(stderr, "%s: %s\n", filename, err)
  84. os.Exit(1)
  85. }
  86. }
  87. },
  88. }
  89. var checkCmd = &cobra.Command{
  90. Use: "compile",
  91. Short: "Statically check SQL for syntax and type errors",
  92. RunE: func(cmd *cobra.Command, args []string) error {
  93. stderr := cmd.ErrOrStderr()
  94. dir, err := os.Getwd()
  95. if err != nil {
  96. fmt.Fprintln(stderr, "error parsing sqlc.json: file does not exist")
  97. os.Exit(1)
  98. }
  99. if _, err := Generate(Env{}, dir, stderr); err != nil {
  100. os.Exit(1)
  101. }
  102. return nil
  103. },
  104. }