/
docker_cmd.go
68 lines (58 loc) · 1.44 KB
/
docker_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package auth
import (
"encoding/json"
"fmt"
"os"
"github.com/docker/cli/cli/config/configfile"
"github.com/spf13/cobra"
)
var configureDockerCmd = &cobra.Command{
Use: "configure-docker",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
configPath := os.ExpandEnv("${HOME}/.docker/config.json")
config := configfile.New(configPath)
configFile, err := os.Open(configPath)
if err == nil {
err = config.LoadFromReader(configFile)
configFile.Close()
if err != nil {
return fmt.Errorf("unable to load docker config file: %w", err)
}
}
if config.CredentialHelpers == nil {
config.CredentialHelpers = make(map[string]string)
}
for _, hostname := range args {
config.CredentialHelpers[hostname] = "gcloud"
}
err = config.Save()
if err != nil {
return fmt.Errorf("unable to save docker config: %w", err)
}
return nil
},
}
type DockerHelperRes struct {
Secret string
Username string
}
var dockerHelperCmd = &cobra.Command{
Use: "docker-helper",
RunE: func(cmd *cobra.Command, args []string) error {
// ignore all operations except for get
if len(args) > 0 && args[0] != "get" {
return nil
}
tok, err := Token()
if err != nil {
return fmt.Errorf("unable to get token: %w", err)
}
res := &DockerHelperRes{
Secret: tok.AccessToken,
Username: "_dcgcloud_token",
}
encoder := json.NewEncoder(cmd.OutOrStdout())
return encoder.Encode(res)
},
}