docker.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package cmd
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "github.com/fatih/color"
  8. "github.com/spf13/cobra"
  9. "github.com/docker/cli/cli/config/configfile"
  10. )
  11. var dockerCmd = &cobra.Command{
  12. Use: "docker",
  13. Short: "Commands to configure Docker for a project",
  14. }
  15. var configureCmd = &cobra.Command{
  16. Use: "configure",
  17. Short: "Configures the host's Docker instance",
  18. Run: func(cmd *cobra.Command, args []string) {
  19. if err := dockerConfig(); err != nil {
  20. color.New(color.FgRed).Println("Configuring Docker unsuccessful:", err.Error())
  21. os.Exit(1)
  22. }
  23. },
  24. }
  25. func init() {
  26. rootCmd.AddCommand(dockerCmd)
  27. dockerCmd.AddCommand(configureCmd)
  28. }
  29. func dockerConfig() error {
  30. dockerConfigFile := filepath.Join(home, ".docker", "config.json")
  31. // check that a compatible version of docker is installed
  32. // determine if configfile exists
  33. // if it does not exist, create it
  34. // if it does exist, read it
  35. configBytes, err := ioutil.ReadFile(dockerConfigFile)
  36. if err != nil {
  37. return err
  38. }
  39. config := &configfile.ConfigFile{
  40. Filename: dockerConfigFile,
  41. }
  42. err = json.Unmarshal(configBytes, config)
  43. if err != nil {
  44. return err
  45. }
  46. config.CredentialHelpers["393629051022.dkr.ecr.us-east-2.amazonaws.com"] = "porter"
  47. err = config.Save()
  48. if err != nil {
  49. return err
  50. }
  51. return nil
  52. // z := &github.ZIPReleaseGetter{
  53. // AssetName: "docker-credential-porter",
  54. // AssetFolderDest: "/usr/local/bin",
  55. // ZipFolderDest: filepath.Join(home, ".porter"),
  56. // ZipName: "docker-credential-porter_latest.zip",
  57. // EntityID: "porter-dev",
  58. // RepoName: "porter",
  59. // IsPlatformDependent: true,
  60. // }
  61. // err = z.GetLatestRelease()
  62. // if err != nil {
  63. // return err
  64. // }
  65. // return nil
  66. }