helmrepo.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "github.com/porter-dev/porter/api/types"
  7. "github.com/fatih/color"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/cli/cmd/utils"
  10. )
  11. func HelmRepo(
  12. ctx context.Context,
  13. client api.Client,
  14. projectID uint,
  15. ) (uint, error) {
  16. // if project ID is 0, ask the user to set the project ID or create a project
  17. if projectID == 0 {
  18. return 0, fmt.Errorf("no project set, please run porter project set [id]")
  19. }
  20. repoName, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the name that you would like to give this Helm registry.
  21. Name: `))
  22. if err != nil {
  23. return 0, err
  24. }
  25. repoURL, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the Helm registry URL, make sure to include the protocol. For example, https://charts.bitnami.com/bitnami.
  26. Registry URL: `))
  27. if err != nil {
  28. return 0, err
  29. }
  30. if _, err := url.Parse(repoURL); err != nil {
  31. return 0, fmt.Errorf("not a valid url: %s", err)
  32. }
  33. username, err := utils.PromptPlaintext(fmt.Sprintf(`Helm repo username (press enter for a public registry):`))
  34. if err != nil {
  35. return 0, err
  36. }
  37. password, err := utils.PromptPassword(`Helm registry password (press enter for a public registry).
  38. Password:`)
  39. if err != nil {
  40. return 0, err
  41. }
  42. var basicIntegrationID uint = 0
  43. if username != "" && password != "" {
  44. // create the basic auth integration
  45. integration, err := client.CreateBasicAuthIntegration(
  46. ctx,
  47. projectID,
  48. &types.CreateBasicRequest{
  49. Username: username,
  50. Password: password,
  51. },
  52. )
  53. if err != nil {
  54. return 0, err
  55. }
  56. color.New(color.FgGreen).Printf("created basic auth integration with id %d\n", integration.ID)
  57. basicIntegrationID = integration.ID
  58. }
  59. reg, err := client.CreateHelmRepo(
  60. ctx,
  61. projectID,
  62. &types.CreateUpdateHelmRepoRequest{
  63. URL: repoURL,
  64. Name: repoName,
  65. BasicIntegrationID: basicIntegrationID,
  66. },
  67. )
  68. if err != nil {
  69. return 0, err
  70. }
  71. color.New(color.FgGreen).Printf("created helm registry integration with id %d and name %s\n", reg.ID, reg.Name)
  72. return reg.ID, nil
  73. }