mirror of https://github.com/docker/cli.git
context list: temporarily add ContextType to JSON output
Docker Desktop currently ships with the "cloud integration" wrapper, which outputs an additional ContextType field in the JSON output. While this field is non-standard, it made its way into Visual Studio's Docker integration, which uses this to exclude "aci" and "eci" context types that are not supported by Visual Studio. This patch; - conditionally adds a ContextType field to the JSON output - but ONLY when using the default "{{json .}}" or "json" formats (which are the formats used by Visual Studio) - if the context is a "aci" or "eci" context, that type is preserved, otherwise the default "moby" type is used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
3da25f6c6d
commit
fed9fa0f72
|
@ -66,6 +66,8 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
|
||||||
Name: rawMeta.Name,
|
Name: rawMeta.Name,
|
||||||
Current: isCurrent,
|
Current: isCurrent,
|
||||||
Error: err.Error(),
|
Error: err.Error(),
|
||||||
|
|
||||||
|
ContextType: getContextType(nil, opts.format),
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -80,6 +82,8 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
|
||||||
Description: meta.Description,
|
Description: meta.Description,
|
||||||
DockerEndpoint: dockerEndpoint.Host,
|
DockerEndpoint: dockerEndpoint.Host,
|
||||||
Error: errMsg,
|
Error: errMsg,
|
||||||
|
|
||||||
|
ContextType: getContextType(meta.AdditionalFields, opts.format),
|
||||||
}
|
}
|
||||||
contexts = append(contexts, &desc)
|
contexts = append(contexts, &desc)
|
||||||
}
|
}
|
||||||
|
@ -96,6 +100,8 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
|
||||||
Name: curContext,
|
Name: curContext,
|
||||||
Current: true,
|
Current: true,
|
||||||
Error: errMsg,
|
Error: errMsg,
|
||||||
|
|
||||||
|
ContextType: getContextType(nil, opts.format),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sort.Slice(contexts, func(i, j int) bool {
|
sort.Slice(contexts, func(i, j int) bool {
|
||||||
|
@ -111,6 +117,30 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getContextType sets the LegacyContextType field for compatibility with
|
||||||
|
// Visual Studio, which depends on this field from the "cloud integration"
|
||||||
|
// wrapper.
|
||||||
|
//
|
||||||
|
// https://github.com/docker/compose-cli/blob/c156ce6da4c2b317174d42daf1b019efa87e9f92/api/context/store/contextmetadata.go#L28-L34
|
||||||
|
// https://github.com/docker/compose-cli/blob/c156ce6da4c2b317174d42daf1b019efa87e9f92/api/context/store/store.go#L34-L51
|
||||||
|
//
|
||||||
|
// TODO(thaJeztah): remove this and [ClientContext.ContextType] once Visual Studio is updated to no longer depend on this.
|
||||||
|
func getContextType(meta map[string]any, format string) string {
|
||||||
|
if format != formatter.JSONFormat && format != formatter.JSONFormatKey {
|
||||||
|
// We only need the ContextType field when formatting as JSON,
|
||||||
|
// which is the format-string used by Visual Studio to detect the
|
||||||
|
// context-type.
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if ct, ok := meta["Type"]; ok {
|
||||||
|
// If the context on-disk has a context-type (ecs, aci), return it.
|
||||||
|
return ct.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the default context-type.
|
||||||
|
return "moby"
|
||||||
|
}
|
||||||
|
|
||||||
func format(dockerCli command.Cli, opts *listOptions, contexts []*formatter.ClientContext) error {
|
func format(dockerCli command.Cli, opts *listOptions, contexts []*formatter.ClientContext) error {
|
||||||
contextCtx := formatter.Context{
|
contextCtx := formatter.Context{
|
||||||
Output: dockerCli.Out(),
|
Output: dockerCli.Out(),
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{"Current":false,"Description":"description of context1","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"context1"}
|
{"Name":"context1","Description":"description of context1","DockerEndpoint":"https://someswarmserver.example.com","Current":false,"Error":"","ContextType":"aci"}
|
||||||
{"Current":false,"Description":"description of context2","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"context2"}
|
{"Name":"context2","Description":"description of context2","DockerEndpoint":"https://someswarmserver.example.com","Current":false,"Error":"","ContextType":"ecs"}
|
||||||
{"Current":false,"Description":"description of context3","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"context3"}
|
{"Name":"context3","Description":"description of context3","DockerEndpoint":"https://someswarmserver.example.com","Current":false,"Error":"","ContextType":"moby"}
|
||||||
{"Current":true,"Description":"description of current","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"current"}
|
{"Name":"current","Description":"description of current","DockerEndpoint":"https://someswarmserver.example.com","Current":true,"Error":"","ContextType":"moby"}
|
||||||
{"Current":false,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","Error":"","Name":"default"}
|
{"Name":"default","Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","Current":false,"Error":"","ContextType":"moby"}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ClientContextTableFormat is the default client context format.
|
// ClientContextTableFormat is the default client context format.
|
||||||
ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.Error}}"
|
ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.Error}}"
|
||||||
|
@ -28,6 +30,13 @@ type ClientContext struct {
|
||||||
DockerEndpoint string
|
DockerEndpoint string
|
||||||
Current bool
|
Current bool
|
||||||
Error string
|
Error string
|
||||||
|
|
||||||
|
// ContextType is a temporary field for compatibility with
|
||||||
|
// Visual Studio, which depends on this from the "cloud integration"
|
||||||
|
// wrapper.
|
||||||
|
//
|
||||||
|
// Deprecated: this type is only for backward-compatibility. Do not use.
|
||||||
|
ContextType string `json:"ContextType,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientContextWrite writes formatted contexts using the Context
|
// ClientContextWrite writes formatted contexts using the Context
|
||||||
|
@ -60,6 +69,13 @@ func newClientContextContext() *clientContextContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *clientContextContext) MarshalJSON() ([]byte, error) {
|
func (c *clientContextContext) MarshalJSON() ([]byte, error) {
|
||||||
|
if c.c.ContextType != "" {
|
||||||
|
// We only have ContextType set for plain "json" or "{{json .}}" formatting,
|
||||||
|
// so we should be able to just use the default json.Marshal with no
|
||||||
|
// special handling.
|
||||||
|
return json.Marshal(c.c)
|
||||||
|
}
|
||||||
|
// FIXME(thaJeztah): why do we need a special marshal function here?
|
||||||
return MarshalJSON(c)
|
return MarshalJSON(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue