registry.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/porter-dev/porter/api/types"
  6. "github.com/fatih/color"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/cli/cmd/utils"
  9. )
  10. // Helm connects a Helm repository using HTTP basic authentication
  11. func Registry(
  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. repoURL, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the image registry URL (include the protocol). For example, https://my-custom-registry.getporter.dev.
  21. Image registry URL: `))
  22. if err != nil {
  23. return 0, err
  24. }
  25. username, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the username/password for authentication (press enter if no authenicaiton is required).
  26. Username: `))
  27. if err != nil {
  28. return 0, err
  29. }
  30. password, err := utils.PromptPasswordWithConfirmation()
  31. if err != nil {
  32. return 0, err
  33. }
  34. // create the basic auth integration
  35. integration, err := client.CreateBasicAuthIntegration(
  36. context.Background(),
  37. projectID,
  38. &types.CreateBasicRequest{
  39. Username: username,
  40. Password: password,
  41. },
  42. )
  43. if err != nil {
  44. return 0, err
  45. }
  46. color.New(color.FgGreen).Printf("created basic auth integration with id %d\n", integration.ID)
  47. reg, err := client.CreateRegistry(
  48. context.Background(),
  49. projectID,
  50. &types.CreateRegistryRequest{
  51. URL: repoURL,
  52. Name: repoURL,
  53. BasicIntegrationID: integration.ID,
  54. },
  55. )
  56. if err != nil {
  57. return 0, err
  58. }
  59. color.New(color.FgGreen).Printf("created private registry with id %d and name %s\n", reg.ID, reg.Name)
  60. return reg.ID, nil
  61. }