2
0

rollback.go 1.3 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. // RollbackInput is the input for the Rollback function
  10. type RollbackInput struct {
  11. // CLIConfig is the CLI configuration
  12. CLIConfig config.CLIConfig
  13. // Client is the Porter API client
  14. Client api.Client
  15. // AppName is the name of the app to rollback
  16. AppName string
  17. }
  18. // Rollback deploys the previous successful revision of an app
  19. func Rollback(ctx context.Context, inp RollbackInput) 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. deploymentTargetID := targetResp.DeploymentTargetID
  25. color.New(color.FgGreen).Printf("Rolling back to last deployed revision ...\n") // nolint:errcheck,gosec
  26. rollbackResp, err := inp.Client.RollbackRevision(ctx, inp.CLIConfig.Project, inp.CLIConfig.Cluster, inp.AppName, deploymentTargetID)
  27. if err != nil {
  28. return fmt.Errorf("error calling rollback revision endpoint: %w", err)
  29. }
  30. color.New(color.FgGreen).Printf("Successfully rolled back to revision %d\n", rollbackResp.TargetRevisionNumber) // nolint:errcheck,gosec
  31. return nil
  32. }