This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
/
env.go
56 lines (49 loc) · 1.48 KB
/
env.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/docker/app/internal"
"github.com/docker/cli/cli/command"
cliconfig "github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/context/docker"
kubcontext "github.com/docker/cli/cli/context/kubernetes"
contextstore "github.com/docker/cli/cli/context/store"
cliflags "github.com/docker/cli/cli/flags"
)
var storeConfig = contextstore.NewConfig(
func() interface{} { return &command.DockerContext{} },
contextstore.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }),
contextstore.EndpointTypeGetter(kubcontext.KubernetesEndpoint, func() interface{} { return &kubcontext.EndpointMeta{} }),
)
func setupDockerContext() (command.Cli, error) {
s := contextstore.New(cliconfig.ContextStoreDir(), storeConfig)
f, err := os.Open(internal.CredentialDockerContextPath)
if err != nil {
return nil, err
}
defer f.Close()
if err := contextstore.Import("cnab", s, f); err != nil {
return nil, err
}
cli, err := command.NewDockerCli()
if err != nil {
return nil, err
}
if err := cli.Initialize(&cliflags.ClientOptions{
Common: &cliflags.CommonOptions{
Context: "cnab",
},
}); err != nil {
return nil, err
}
authConfigsJSON, err := ioutil.ReadFile(internal.CredentialRegistryPath)
if err != nil {
return nil, err
}
configFile := cli.ConfigFile()
if err := json.Unmarshal(authConfigsJSON, &configFile.AuthConfigs); err != nil {
return nil, err
}
return cli, nil
}