2
0

dockerhub.go 1.7 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. func Dockerhub(
  11. client *api.Client,
  12. projectID uint,
  13. ) (uint, error) {
  14. // if project ID is 0, ask the user to set the project ID or create a project
  15. if projectID == 0 {
  16. return 0, fmt.Errorf("no project set, please run porter project set [id]")
  17. }
  18. // query for dockerhub name
  19. repoName, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the Docker Hub image path, in the form of ${org_name}/${repo_name}. For example, porter1/porter.
  20. Image path: `))
  21. if err != nil {
  22. return 0, err
  23. }
  24. username, err := utils.PromptPlaintext(fmt.Sprintf(`Docker Hub username: `))
  25. if err != nil {
  26. return 0, err
  27. }
  28. password, err := utils.PromptPassword(`Provide the Docker Hub personal access token.
  29. Token:`)
  30. if err != nil {
  31. return 0, err
  32. }
  33. // create the basic auth integration
  34. integration, err := client.CreateBasicAuthIntegration(
  35. context.Background(),
  36. projectID,
  37. &types.CreateBasicRequest{
  38. Username: username,
  39. Password: password,
  40. },
  41. )
  42. if err != nil {
  43. return 0, err
  44. }
  45. color.New(color.FgGreen).Printf("created basic auth integration with id %d\n", integration.ID)
  46. reg, err := client.CreateRegistry(
  47. context.Background(),
  48. projectID,
  49. &types.CreateRegistryRequest{
  50. URL: fmt.Sprintf("index.docker.io/%s", repoName),
  51. Name: repoName,
  52. BasicIntegrationID: integration.ID,
  53. },
  54. )
  55. if err != nil {
  56. return 0, err
  57. }
  58. color.New(color.FgGreen).Printf("created private registry with id %d and name %s\n", reg.ID, reg.Name)
  59. return reg.ID, nil
  60. }