Skip to content

Commit 67f15ee

Browse files
committed
Added missing exported symbols + ContextStoreConfig comment
Signed-off-by: Simon Ferquel <[email protected]>
1 parent d7cb9a4 commit 67f15ee

11 files changed

Lines changed: 172 additions & 167 deletions

File tree

cli/command/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ type DockerCli struct {
8080
}
8181

8282
// ContextStoreConfig contains the config used by the context store
83-
// Plugins init functions could modify it to define other typed endpoint metadata
83+
// A plugin's init function may modify it to define other typed endpoint metadata by calling
84+
// ContextStoreConfig.SetEndpoint()
8485
var ContextStoreConfig = store.NewConfig(
8586
func() interface{} { return &DockerContext{} },
8687
store.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }),

cli/command/context/create.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import (
1414
"github.com/spf13/cobra"
1515
)
1616

17-
type createOptions struct {
18-
name string
19-
description string
20-
defaultStackOrchestrator string
21-
docker map[string]string
22-
kubernetes map[string]string
17+
// CreateOptions are the options used for creating a context
18+
type CreateOptions struct {
19+
Name string
20+
Description string
21+
DefaultStackOrchestrator string
22+
Docker map[string]string
23+
Kubernetes map[string]string
2324
}
2425

2526
func longCreateDescription() string {
@@ -43,62 +44,62 @@ func longCreateDescription() string {
4344
}
4445

4546
func newCreateCommand(dockerCli command.Cli) *cobra.Command {
46-
opts := &createOptions{}
47+
opts := &CreateOptions{}
4748
cmd := &cobra.Command{
4849
Use: "create [OPTIONS] CONTEXT",
4950
Short: "Create a context",
5051
Args: cli.ExactArgs(1),
5152
RunE: func(cmd *cobra.Command, args []string) error {
52-
opts.name = args[0]
53+
opts.Name = args[0]
5354
return RunCreate(dockerCli, opts)
5455
},
5556
Long: longCreateDescription(),
5657
}
5758
flags := cmd.Flags()
58-
flags.StringVar(&opts.description, "description", "", "Description of the context")
59+
flags.StringVar(&opts.Description, "description", "", "Description of the context")
5960
flags.StringVar(
60-
&opts.defaultStackOrchestrator,
61+
&opts.DefaultStackOrchestrator,
6162
"default-stack-orchestrator", "",
6263
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
63-
flags.StringToStringVar(&opts.docker, "docker", nil, "set the docker endpoint")
64-
flags.StringToStringVar(&opts.kubernetes, "kubernetes", nil, "set the kubernetes endpoint")
64+
flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint")
65+
flags.StringToStringVar(&opts.Kubernetes, "kubernetes", nil, "set the kubernetes endpoint")
6566
return cmd
6667
}
6768

6869
// RunCreate creates a Docker context
69-
func RunCreate(cli command.Cli, o *createOptions) error {
70+
func RunCreate(cli command.Cli, o *CreateOptions) error {
7071
s := cli.ContextStore()
71-
if err := checkContextNameForCreation(s, o.name); err != nil {
72+
if err := checkContextNameForCreation(s, o.Name); err != nil {
7273
return err
7374
}
74-
stackOrchestrator, err := command.NormalizeOrchestrator(o.defaultStackOrchestrator)
75+
stackOrchestrator, err := command.NormalizeOrchestrator(o.DefaultStackOrchestrator)
7576
if err != nil {
7677
return errors.Wrap(err, "unable to parse default-stack-orchestrator")
7778
}
7879
contextMetadata := store.ContextMetadata{
7980
Endpoints: make(map[string]interface{}),
8081
Metadata: command.DockerContext{
81-
Description: o.description,
82+
Description: o.Description,
8283
StackOrchestrator: stackOrchestrator,
8384
},
84-
Name: o.name,
85+
Name: o.Name,
8586
}
86-
if o.docker == nil {
87+
if o.Docker == nil {
8788
return errors.New("docker endpoint configuration is required")
8889
}
8990
contextTLSData := store.ContextTLSData{
9091
Endpoints: make(map[string]store.EndpointTLSData),
9192
}
92-
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(cli, o.docker)
93+
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(cli, o.Docker)
9394
if err != nil {
9495
return errors.Wrap(err, "unable to create docker endpoint config")
9596
}
9697
contextMetadata.Endpoints[docker.DockerEndpoint] = dockerEP
9798
if dockerTLS != nil {
9899
contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerTLS
99100
}
100-
if o.kubernetes != nil {
101-
kubernetesEP, kubernetesTLS, err := getKubernetesEndpointMetadataAndTLS(cli, o.kubernetes)
101+
if o.Kubernetes != nil {
102+
kubernetesEP, kubernetesTLS, err := getKubernetesEndpointMetadataAndTLS(cli, o.Kubernetes)
102103
if err != nil {
103104
return errors.Wrap(err, "unable to create kubernetes endpoint config")
104105
}
@@ -118,11 +119,11 @@ func RunCreate(cli command.Cli, o *createOptions) error {
118119
if err := s.CreateOrUpdateContext(contextMetadata); err != nil {
119120
return err
120121
}
121-
if err := s.ResetContextTLSMaterial(o.name, &contextTLSData); err != nil {
122+
if err := s.ResetContextTLSMaterial(o.Name, &contextTLSData); err != nil {
122123
return err
123124
}
124-
fmt.Fprintln(cli.Out(), o.name)
125-
fmt.Fprintf(cli.Err(), "Successfully created context %q\n", o.name)
125+
fmt.Fprintln(cli.Out(), o.Name)
126+
fmt.Fprintf(cli.Err(), "Successfully created context %q\n", o.Name)
126127
return nil
127128
}
128129

cli/command/context/create_test.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,67 +46,67 @@ func TestCreateInvalids(t *testing.T) {
4646
defer cleanup()
4747
assert.NilError(t, cli.ContextStore().CreateOrUpdateContext(store.ContextMetadata{Name: "existing-context"}))
4848
tests := []struct {
49-
options createOptions
49+
options CreateOptions
5050
expecterErr string
5151
}{
5252
{
5353
expecterErr: `context name cannot be empty`,
5454
},
5555
{
56-
options: createOptions{
57-
name: " ",
56+
options: CreateOptions{
57+
Name: " ",
5858
},
5959
expecterErr: `context name " " is invalid`,
6060
},
6161
{
62-
options: createOptions{
63-
name: "existing-context",
62+
options: CreateOptions{
63+
Name: "existing-context",
6464
},
6565
expecterErr: `context "existing-context" already exists`,
6666
},
6767
{
68-
options: createOptions{
69-
name: "invalid-docker-host",
70-
docker: map[string]string{
68+
options: CreateOptions{
69+
Name: "invalid-docker-host",
70+
Docker: map[string]string{
7171
keyHost: "some///invalid/host",
7272
},
7373
},
7474
expecterErr: `unable to parse docker host`,
7575
},
7676
{
77-
options: createOptions{
78-
name: "invalid-orchestrator",
79-
defaultStackOrchestrator: "invalid",
77+
options: CreateOptions{
78+
Name: "invalid-orchestrator",
79+
DefaultStackOrchestrator: "invalid",
8080
},
8181
expecterErr: `specified orchestrator "invalid" is invalid, please use either kubernetes, swarm or all`,
8282
},
8383
{
84-
options: createOptions{
85-
name: "orchestrator-swarm-no-endpoint",
86-
defaultStackOrchestrator: "swarm",
84+
options: CreateOptions{
85+
Name: "orchestrator-swarm-no-endpoint",
86+
DefaultStackOrchestrator: "swarm",
8787
},
8888
expecterErr: `docker endpoint configuration is required`,
8989
},
9090
{
91-
options: createOptions{
92-
name: "orchestrator-kubernetes-no-endpoint",
93-
defaultStackOrchestrator: "kubernetes",
94-
docker: map[string]string{},
91+
options: CreateOptions{
92+
Name: "orchestrator-kubernetes-no-endpoint",
93+
DefaultStackOrchestrator: "kubernetes",
94+
Docker: map[string]string{},
9595
},
9696
expecterErr: `cannot specify orchestrator "kubernetes" without configuring a Kubernetes endpoint`,
9797
},
9898
{
99-
options: createOptions{
100-
name: "orchestrator-all-no-endpoint",
101-
defaultStackOrchestrator: "all",
102-
docker: map[string]string{},
99+
options: CreateOptions{
100+
Name: "orchestrator-all-no-endpoint",
101+
DefaultStackOrchestrator: "all",
102+
Docker: map[string]string{},
103103
},
104104
expecterErr: `cannot specify orchestrator "all" without configuring a Kubernetes endpoint`,
105105
},
106106
}
107107
for _, tc := range tests {
108108
tc := tc
109-
t.Run(tc.options.name, func(t *testing.T) {
109+
t.Run(tc.options.Name, func(t *testing.T) {
110110
err := RunCreate(cli, &tc.options)
111111
assert.ErrorContains(t, err, tc.expecterErr)
112112
})
@@ -117,10 +117,10 @@ func TestCreateOrchestratorSwarm(t *testing.T) {
117117
cli, cleanup := makeFakeCli(t)
118118
defer cleanup()
119119

120-
err := RunCreate(cli, &createOptions{
121-
name: "test",
122-
defaultStackOrchestrator: "swarm",
123-
docker: map[string]string{},
120+
err := RunCreate(cli, &CreateOptions{
121+
Name: "test",
122+
DefaultStackOrchestrator: "swarm",
123+
Docker: map[string]string{},
124124
})
125125
assert.NilError(t, err)
126126
assert.Equal(t, "test\n", cli.OutBuffer().String())
@@ -131,9 +131,9 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
131131
cli, cleanup := makeFakeCli(t)
132132
defer cleanup()
133133

134-
err := RunCreate(cli, &createOptions{
135-
name: "test",
136-
docker: map[string]string{},
134+
err := RunCreate(cli, &CreateOptions{
135+
Name: "test",
136+
Docker: map[string]string{},
137137
})
138138
assert.NilError(t, err)
139139
}
@@ -156,13 +156,13 @@ func createTestContextWithKube(t *testing.T, cli command.Cli) {
156156
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
157157
defer revert()
158158

159-
err := RunCreate(cli, &createOptions{
160-
name: "test",
161-
defaultStackOrchestrator: "all",
162-
kubernetes: map[string]string{
159+
err := RunCreate(cli, &CreateOptions{
160+
Name: "test",
161+
DefaultStackOrchestrator: "all",
162+
Kubernetes: map[string]string{
163163
keyFromCurrent: "true",
164164
},
165-
docker: map[string]string{},
165+
Docker: map[string]string{},
166166
})
167167
assert.NilError(t, err)
168168
}

cli/command/context/export-import_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ func TestExportImportWithFile(t *testing.T) {
2121
defer cleanup()
2222
createTestContextWithKube(t, cli)
2323
cli.ErrBuffer().Reset()
24-
assert.NilError(t, RunExport(cli, &exportOptions{
25-
contextName: "test",
26-
dest: contextFile,
24+
assert.NilError(t, RunExport(cli, &ExportOptions{
25+
ContextName: "test",
26+
Dest: contextFile,
2727
}))
2828
assert.Equal(t, cli.ErrBuffer().String(), fmt.Sprintf("Written file %q\n", contextFile))
2929
cli.OutBuffer().Reset()
@@ -48,9 +48,9 @@ func TestExportImportPipe(t *testing.T) {
4848
createTestContextWithKube(t, cli)
4949
cli.ErrBuffer().Reset()
5050
cli.OutBuffer().Reset()
51-
assert.NilError(t, RunExport(cli, &exportOptions{
52-
contextName: "test",
53-
dest: "-",
51+
assert.NilError(t, RunExport(cli, &ExportOptions{
52+
ContextName: "test",
53+
Dest: "-",
5454
}))
5555
assert.Equal(t, cli.ErrBuffer().String(), "")
5656
cli.SetIn(command.NewInStream(ioutil.NopCloser(bytes.NewBuffer(cli.OutBuffer().Bytes()))))
@@ -79,18 +79,18 @@ func TestExportKubeconfig(t *testing.T) {
7979
defer cleanup()
8080
createTestContextWithKube(t, cli)
8181
cli.ErrBuffer().Reset()
82-
assert.NilError(t, RunExport(cli, &exportOptions{
83-
contextName: "test",
84-
dest: contextFile,
85-
kubeconfig: true,
82+
assert.NilError(t, RunExport(cli, &ExportOptions{
83+
ContextName: "test",
84+
Dest: contextFile,
85+
Kubeconfig: true,
8686
}))
8787
assert.Equal(t, cli.ErrBuffer().String(), fmt.Sprintf("Written file %q\n", contextFile))
88-
assert.NilError(t, RunCreate(cli, &createOptions{
89-
name: "test2",
90-
kubernetes: map[string]string{
88+
assert.NilError(t, RunCreate(cli, &CreateOptions{
89+
Name: "test2",
90+
Kubernetes: map[string]string{
9191
keyKubeconfig: contextFile,
9292
},
93-
docker: map[string]string{},
93+
Docker: map[string]string{},
9494
}))
9595
validateTestKubeEndpoint(t, cli.ContextStore(), "test2")
9696
}
@@ -105,6 +105,6 @@ func TestExportExistingFile(t *testing.T) {
105105
createTestContextWithKube(t, cli)
106106
cli.ErrBuffer().Reset()
107107
assert.NilError(t, ioutil.WriteFile(contextFile, []byte{}, 0644))
108-
err = RunExport(cli, &exportOptions{contextName: "test", dest: contextFile})
108+
err = RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
109109
assert.Assert(t, os.IsExist(err))
110110
}

0 commit comments

Comments
 (0)