registry.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. 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. // query for helm repo name
  21. repoURL, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the image registry URL (include the protocol). For example, https://my-custom-registry.getporter.dev.
  22. Image registry URL: `))
  23. if err != nil {
  24. return 0, err
  25. }
  26. username, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the username/password for authentication (press enter if no authenicaiton is required).
  27. Username: `))
  28. if err != nil {
  29. return 0, err
  30. }
  31. password, err := utils.PromptPasswordWithConfirmation()
  32. if err != nil {
  33. return 0, err
  34. }
  35. // create the basic auth integration
  36. integration, err := client.CreateBasicAuthIntegration(
  37. ctx,
  38. projectID,
  39. &types.CreateBasicRequest{
  40. Username: username,
  41. Password: password,
  42. },
  43. )
  44. if err != nil {
  45. return 0, err
  46. }
  47. color.New(color.FgGreen).Printf("created basic auth integration with id %d\n", integration.ID)
  48. reg, err := client.CreateRegistry(
  49. ctx,
  50. projectID,
  51. &types.CreateRegistryRequest{
  52. URL: repoURL,
  53. Name: repoURL,
  54. BasicIntegrationID: integration.ID,
  55. },
  56. )
  57. if err != nil {
  58. return 0, err
  59. }
  60. color.New(color.FgGreen).Printf("created private registry with id %d and name %s\n", reg.ID, reg.Name)
  61. return reg.ID, nil
  62. }