2018-12-17 05:27:07 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2020-06-10 09:07:23 -04:00
|
|
|
"encoding/json"
|
2018-12-17 05:27:07 -05:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DockerContext is a typed representation of what we put in Context metadata
|
|
|
|
type DockerContext struct {
|
2022-02-22 07:46:35 -05:00
|
|
|
Description string
|
|
|
|
AdditionalFields map[string]interface{}
|
2020-06-10 09:07:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements custom JSON marshalling
|
|
|
|
func (dc DockerContext) MarshalJSON() ([]byte, error) {
|
|
|
|
s := map[string]interface{}{}
|
|
|
|
if dc.Description != "" {
|
|
|
|
s["Description"] = dc.Description
|
|
|
|
}
|
|
|
|
if dc.AdditionalFields != nil {
|
|
|
|
for k, v := range dc.AdditionalFields {
|
|
|
|
s[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements custom JSON marshalling
|
|
|
|
func (dc *DockerContext) UnmarshalJSON(payload []byte) error {
|
|
|
|
var data map[string]interface{}
|
|
|
|
if err := json.Unmarshal(payload, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range data {
|
|
|
|
switch k {
|
|
|
|
case "Description":
|
|
|
|
dc.Description = v.(string)
|
|
|
|
default:
|
|
|
|
if dc.AdditionalFields == nil {
|
|
|
|
dc.AdditionalFields = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
dc.AdditionalFields[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDockerContext extracts metadata from stored context metadata
|
2019-04-18 09:12:30 -04:00
|
|
|
func GetDockerContext(storeMetadata store.Metadata) (DockerContext, error) {
|
2018-12-17 05:27:07 -05:00
|
|
|
if storeMetadata.Metadata == nil {
|
|
|
|
// can happen if we save endpoints before assigning a context metadata
|
|
|
|
// it is totally valid, and we should return a default initialized value
|
|
|
|
return DockerContext{}, nil
|
|
|
|
}
|
|
|
|
res, ok := storeMetadata.Metadata.(DockerContext)
|
|
|
|
if !ok {
|
|
|
|
return DockerContext{}, errors.New("context metadata is not a valid DockerContext")
|
|
|
|
}
|
2022-11-04 07:34:14 -04:00
|
|
|
if storeMetadata.Name == DefaultContextName {
|
|
|
|
res.Description = "Current DOCKER_HOST based configuration"
|
|
|
|
}
|
2018-12-17 05:27:07 -05:00
|
|
|
return res, nil
|
|
|
|
}
|