helm.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/fatih/color"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. "github.com/porter-dev/porter/cli/cmd/utils"
  9. )
  10. // Helm connects a Helm repository using HTTP basic authentication
  11. func Helm(
  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. // query for helm repo name
  20. helmName, err := utils.PromptPlaintext(fmt.Sprintf(`Give this Helm repository a name: `))
  21. if err != nil {
  22. return 0, err
  23. }
  24. repoURL, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the Helm repository URL: `))
  25. if err != nil {
  26. return 0, err
  27. }
  28. userResp, err := utils.PromptPlaintext(
  29. fmt.Sprintf(`Does this endpoint require a username/password to authenticate? %s `,
  30. color.New(color.FgCyan).Sprintf("[y/n]"),
  31. ),
  32. )
  33. if err != nil {
  34. return 0, err
  35. }
  36. username := ""
  37. password := ""
  38. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  39. username, err = utils.PromptPlaintext(fmt.Sprintf(`Username: `))
  40. if err != nil {
  41. return 0, err
  42. }
  43. password, err = utils.PromptPasswordWithConfirmation()
  44. if err != nil {
  45. return 0, err
  46. }
  47. }
  48. // create the basic auth integration
  49. integration, err := client.CreateBasicAuthIntegration(
  50. context.Background(),
  51. projectID,
  52. &api.CreateBasicAuthIntegrationRequest{
  53. Username: username,
  54. Password: password,
  55. },
  56. )
  57. if err != nil {
  58. return 0, err
  59. }
  60. color.New(color.FgGreen).Printf("created basic auth integration with id %d\n", integration.ID)
  61. // create the helm repo
  62. hr, err := client.CreateHelmRepo(
  63. context.Background(),
  64. projectID,
  65. &api.CreateHelmRepoRequest{
  66. Name: helmName,
  67. RepoURL: repoURL,
  68. BasicIntegrationID: integration.ID,
  69. },
  70. )
  71. if err != nil {
  72. return 0, err
  73. }
  74. color.New(color.FgGreen).Printf("created helm repo with id %d and name %s\n", hr.ID, hr.Name)
  75. return hr.ID, nil
  76. }