2020-05-14 21:13:07 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2020 Docker Inc.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
obtaining a copy of this software and associated documentation
|
|
|
|
files (the "Software"), to deal in the Software without
|
|
|
|
restriction, including without limitation the rights to use, copy,
|
|
|
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED,
|
|
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY,
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
TORT OR OTHERWISE,
|
|
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
|
|
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-05-14 20:55:40 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
cliconfig "github.com/docker/api/cli/config"
|
|
|
|
"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-05-20 15:55:05 +02:00
|
|
|
if err := cliconfig.WriteCurrentContext(cliconfig.Dir(ctx), name); err != nil {
|
2020-05-14 20:55:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(name)
|
|
|
|
return nil
|
|
|
|
}
|