apply.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package cmd
  2. import (
  3. "io/ioutil"
  4. "os"
  5. api "github.com/porter-dev/porter/api/client"
  6. "github.com/porter-dev/porter/api/types"
  7. "github.com/porter-dev/switchboard/internal/exec"
  8. "github.com/porter-dev/switchboard/pkg/parser"
  9. "github.com/porter-dev/switchboard/pkg/worker"
  10. "github.com/spf13/cobra"
  11. )
  12. // applyCmd represents the "porter apply" base command when called
  13. // with a porter.yaml file as an argument
  14. var applyCmd = &cobra.Command{
  15. Use: "apply",
  16. Short: "Applies the provided porter.yaml to a project",
  17. Run: func(cmd *cobra.Command, args []string) {
  18. err := checkLoginAndRun(args, apply)
  19. if err != nil {
  20. os.Exit(1)
  21. }
  22. },
  23. }
  24. var porterYAML string
  25. func init() {
  26. rootCmd.AddCommand(applyCmd)
  27. applyCmd.Flags().StringVarP(&porterYAML, "file", "f", "", "path to porter.yaml")
  28. applyCmd.MarkFlagRequired("file")
  29. }
  30. func apply(user *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  31. fileBytes, err := ioutil.ReadFile(porterYAML)
  32. if err != nil {
  33. return err
  34. }
  35. resGroup, err := parser.ParseRawBytes(fileBytes)
  36. if err != nil {
  37. return err
  38. }
  39. basePath, err := os.Getwd()
  40. if err != nil {
  41. return err
  42. }
  43. worker := worker.NewWorker()
  44. return worker.Apply(resGroup, &exec.ApplyOpts{
  45. BasePath: basePath,
  46. })
  47. }