helmrepo.go 2.1 KB

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