helm.go 2.1 KB

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