package.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package cmd
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/spf13/cobra"
  8. "github.com/kyleconroy/sqlc/internal/bundler"
  9. )
  10. var packageCmd = &cobra.Command{
  11. Use: "build",
  12. Short: "Create a tarball containing schema, queries, and configuration",
  13. RunE: func(cmd *cobra.Command, args []string) error {
  14. stderr := cmd.ErrOrStderr()
  15. dir, name := getConfigPath(stderr, cmd.Flag("file"))
  16. if err := createPkg(ParseEnv(cmd), dir, name, stderr); err != nil {
  17. os.Exit(1)
  18. }
  19. return nil
  20. },
  21. }
  22. func createPkg(e Env, dir, filename string, stderr io.Writer) error {
  23. configPath, conf, err := readConfig(stderr, dir, filename)
  24. if err != nil {
  25. return err
  26. }
  27. tarball, err := bundler.Build(configPath, conf)
  28. if err != nil {
  29. return err
  30. }
  31. // TODO: Move this to the configuration file
  32. owner := "tabbed"
  33. project := "sqlc"
  34. checksum := sha256.Sum256(tarball)
  35. sha := fmt.Sprintf("%x", checksum)
  36. output := fmt.Sprintf("%s_%s_%s.tar.gz", owner, project, sha[:10])
  37. if err := os.WriteFile(output, tarball, 0644); err != nil {
  38. return err
  39. }
  40. return nil
  41. }