dockerhub.go 1.7 KB

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