2
0

dockerhub.go 1.8 KB

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