2018-11-09 09:10:41 -05:00
|
|
|
package formatter
|
|
|
|
|
2024-05-30 08:33:15 -04:00
|
|
|
import "encoding/json"
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
const (
|
2022-11-04 09:39:07 -04:00
|
|
|
// ClientContextTableFormat is the default client context format.
|
|
|
|
ClientContextTableFormat = "table {{.Name}}{{if .Current}} *{{end}}\t{{.Description}}\t{{.DockerEndpoint}}\t{{.Error}}"
|
2018-11-09 09:10:41 -05:00
|
|
|
|
2022-02-22 07:46:35 -05:00
|
|
|
dockerEndpointHeader = "DOCKER ENDPOINT"
|
|
|
|
quietContextFormat = "{{.Name}}"
|
2022-11-04 10:48:30 -04:00
|
|
|
|
|
|
|
maxErrLength = 45
|
2018-11-09 09:10:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClientContextFormat returns a Format for rendering using a Context
|
|
|
|
func NewClientContextFormat(source string, quiet bool) Format {
|
|
|
|
if quiet {
|
2022-11-28 10:28:47 -05:00
|
|
|
return quietContextFormat
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
if source == TableFormatKey {
|
2022-11-28 10:28:47 -05:00
|
|
|
return ClientContextTableFormat
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
return Format(source)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientContext is a context for display
|
|
|
|
type ClientContext struct {
|
2022-02-22 07:46:35 -05:00
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
DockerEndpoint string
|
|
|
|
Current bool
|
2022-11-04 09:39:07 -04:00
|
|
|
Error string
|
2024-05-30 08:33:15 -04:00
|
|
|
|
|
|
|
// 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"`
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientContextWrite writes formatted contexts using the Context
|
|
|
|
func ClientContextWrite(ctx Context, contexts []*ClientContext) error {
|
|
|
|
render := func(format func(subContext SubContext) error) error {
|
|
|
|
for _, context := range contexts {
|
|
|
|
if err := format(&clientContextContext{c: context}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(newClientContextContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientContextContext struct {
|
|
|
|
HeaderContext
|
|
|
|
c *ClientContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientContextContext() *clientContextContext {
|
|
|
|
ctx := clientContextContext{}
|
|
|
|
ctx.Header = SubHeaderContext{
|
2022-02-22 07:46:35 -05:00
|
|
|
"Name": NameHeader,
|
|
|
|
"Description": DescriptionHeader,
|
|
|
|
"DockerEndpoint": dockerEndpointHeader,
|
2022-11-04 09:39:07 -04:00
|
|
|
"Error": ErrorHeader,
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
return &ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientContextContext) MarshalJSON() ([]byte, error) {
|
2024-05-30 08:33:15 -04:00
|
|
|
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?
|
2018-11-09 09:10:41 -05:00
|
|
|
return MarshalJSON(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientContextContext) Current() bool {
|
|
|
|
return c.c.Current
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientContextContext) Name() string {
|
|
|
|
return c.c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientContextContext) Description() string {
|
|
|
|
return c.c.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientContextContext) DockerEndpoint() string {
|
|
|
|
return c.c.DockerEndpoint
|
|
|
|
}
|
|
|
|
|
2022-11-04 09:39:07 -04:00
|
|
|
// Error returns the truncated error (if any) that occurred when loading the context.
|
|
|
|
func (c *clientContextContext) Error() string {
|
|
|
|
// TODO(thaJeztah) add "--no-trunc" option to context ls and set default to 30 cols to match "docker service ps"
|
2022-11-04 10:48:30 -04:00
|
|
|
return Ellipsis(c.c.Error, maxErrLength)
|
2022-11-04 09:39:07 -04:00
|
|
|
}
|