2020-05-14 21:13:07 +02:00
|
|
|
/*
|
2020-06-18 16:13:24 +02:00
|
|
|
Copyright 2020 Docker, Inc.
|
2020-05-14 21:13:07 +02:00
|
|
|
|
2020-06-18 16:13:24 +02:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2020-05-14 21:13:07 +02:00
|
|
|
|
2020-06-18 16:13:24 +02:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2020-05-14 21:13:07 +02:00
|
|
|
|
2020-06-18 16:13:24 +02:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2020-05-14 21:13:07 +02:00
|
|
|
*/
|
|
|
|
|
2020-05-14 20:55:40 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-06-06 22:22:30 +02:00
|
|
|
"github.com/docker/api/config"
|
2020-05-14 20:55:40 +02:00
|
|
|
"github.com/docker/api/context/store"
|
|
|
|
)
|
|
|
|
|
2020-05-20 15:55:05 +02:00
|
|
|
func useCommand() *cobra.Command {
|
2020-05-14 20:55:40 +02:00
|
|
|
return &cobra.Command{
|
|
|
|
Use: "use CONTEXT",
|
|
|
|
Short: "Set the default context",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-05-20 15:55:05 +02:00
|
|
|
return runUse(cmd.Context(), args[0])
|
2020-05-14 20:55:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:55:05 +02:00
|
|
|
func runUse(ctx context.Context, name string) error {
|
2020-05-14 20:55:40 +02:00
|
|
|
s := store.ContextStore(ctx)
|
|
|
|
// Match behavior of existing CLI
|
|
|
|
if name != store.DefaultContextName {
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
if _, err := s.Get(name); err != nil {
|
2020-05-14 20:55:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 22:22:30 +02:00
|
|
|
if err := config.WriteCurrentContext(config.Dir(ctx), name); err != nil {
|
2020-05-14 20:55:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(name)
|
|
|
|
return nil
|
|
|
|
}
|