apply.go 1.2 KB

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