run_job.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package v2
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/fatih/color"
  6. api "github.com/porter-dev/porter/api/client"
  7. "github.com/porter-dev/porter/cli/cmd/config"
  8. )
  9. // RunAppJobInput is the input for the RunAppJob function
  10. type RunAppJobInput struct {
  11. // CLIConfig is the CLI configuration
  12. CLIConfig config.CLIConfig
  13. // Client is the Porter API client
  14. Client api.Client
  15. AppName string
  16. JobName string
  17. }
  18. // RunAppJob triggers a job run for an app and returns without waiting for the job to complete
  19. func RunAppJob(ctx context.Context, inp RunAppJobInput) error {
  20. targetResp, err := inp.Client.DefaultDeploymentTarget(ctx, inp.CLIConfig.Project, inp.CLIConfig.Cluster)
  21. if err != nil {
  22. return fmt.Errorf("error calling default deployment target endpoint: %w", err)
  23. }
  24. resp, err := inp.Client.RunAppJob(ctx, inp.CLIConfig.Project, inp.CLIConfig.Cluster, inp.AppName, inp.JobName, targetResp.DeploymentTargetID)
  25. if err != nil {
  26. return fmt.Errorf("unable to run job: %w", err)
  27. }
  28. color.New(color.FgGreen).Println("Triggered job with id:", resp.JobRunID) // nolint:errcheck,gosec
  29. return nil
  30. }