dockerhub.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  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 Dockerhub(
  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 dockerhub name
  20. repoName, err := utils.PromptPlaintext(`Provide the Docker Hub repository, in the form of ${org_name}/${repo_name}. For example, porter1/porter.
  21. Repository: `)
  22. if err != nil {
  23. return 0, err
  24. }
  25. orgRepo := strings.Split(repoName, "/")
  26. if len(orgRepo) != 2 || orgRepo[0] == "" || orgRepo[1] == "" {
  27. return 0, fmt.Errorf("invalid Docker Hub image path: %s", repoName)
  28. }
  29. username, err := utils.PromptPlaintext("Docker Hub username: ")
  30. if err != nil {
  31. return 0, err
  32. }
  33. password, err := utils.PromptPassword("Provide the Docker Hub personal access token.\nToken: ")
  34. if err != nil {
  35. return 0, err
  36. }
  37. // create the basic auth integration
  38. integration, err := client.CreateBasicAuthIntegration(
  39. context.Background(),
  40. projectID,
  41. &types.CreateBasicRequest{
  42. Username: username,
  43. Password: password,
  44. },
  45. )
  46. if err != nil {
  47. return 0, err
  48. }
  49. color.New(color.FgGreen).Printf("created basic auth integration with id %d\n", integration.ID)
  50. reg, err := client.CreateRegistry(
  51. context.Background(),
  52. projectID,
  53. &types.CreateRegistryRequest{
  54. URL: fmt.Sprintf("index.docker.io/%s", repoName),
  55. Name: repoName,
  56. BasicIntegrationID: integration.ID,
  57. },
  58. )
  59. if err != nil {
  60. return 0, err
  61. }
  62. color.New(color.FgGreen).Printf("created private registry with id %d and name %s\n", reg.ID, reg.Name)
  63. return reg.ID, nil
  64. }