dockerhub.go 1.7 KB

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