2
0

dockerhub.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. 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 dockerhub name
  21. repoName, err := utils.PromptPlaintext("Provide the Docker Hub repository, in the form of ${org_name}/${repo_name}. For example, porter1/porter.\nRepository: ")
  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 repository: %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. ctx,
  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. ctx,
  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. }