keel/bot/formatter/deployments.go

94 lines
2.3 KiB
Go
Raw Normal View History

2017-06-27 22:40:47 +00:00
package formatter
import (
2017-06-28 10:01:11 +00:00
"fmt"
"strings"
"time"
2017-06-27 22:40:47 +00:00
)
// Deployment - internal deployment, used to better represent keel related info
type Deployment struct {
2017-06-28 10:01:11 +00:00
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt time.Time
Replicas int32
AvailableReplicas int32
Images []string `json:"images,omitempty"` // image:tag list
2017-06-27 22:40:47 +00:00
}
2017-08-08 20:47:29 +00:00
// Formatter headers
2017-06-27 22:40:47 +00:00
const (
defaultDeploymentQuietFormat = "{{.Name}}"
2017-06-28 10:01:11 +00:00
defaultDeploymentTableFormat = "table {{.Namespace}}\t{{.Name}}\t{{.Ready}}\t{{.Images}}"
2017-06-27 22:40:47 +00:00
DeploymentNamespaceHeader = "NAMESPACE"
DeploymentNameHeader = "NAME"
2017-06-28 10:01:11 +00:00
DeploymentReadyHeader = "READY"
DeploymentImagesHeader = "IMAGES"
2017-06-27 22:40:47 +00:00
)
// NewDeploymentsFormat returns a format for use with a deployment Context
func NewDeploymentsFormat(source string, quiet bool) Format {
switch source {
case TableFormatKey:
if quiet {
return defaultDeploymentQuietFormat
}
return defaultDeploymentTableFormat
case RawFormatKey:
if quiet {
return `name: {{.Name}}`
}
return `name: {{.Name}}\n`
}
return Format(source)
}
// DeploymentWrite writes formatted deployments using the Context
func DeploymentWrite(ctx Context, Deployments []Deployment) error {
render := func(format func(subContext subContext) error) error {
for _, deployment := range Deployments {
if err := format(&DeploymentContext{v: deployment}); err != nil {
return err
}
}
return nil
}
return ctx.Write(&DeploymentContext{}, render)
}
2017-08-08 20:47:29 +00:00
// DeploymentContext - deployment context is a container for each line
2017-06-27 22:40:47 +00:00
type DeploymentContext struct {
HeaderContext
v Deployment
}
2017-08-08 20:47:29 +00:00
// MarshalJSON - marshal to json (inspect)
2017-06-27 22:40:47 +00:00
func (c *DeploymentContext) MarshalJSON() ([]byte, error) {
return marshalJSON(c)
}
2017-08-08 20:47:29 +00:00
// Namespace - print namespace
2017-06-27 22:40:47 +00:00
func (c *DeploymentContext) Namespace() string {
c.AddHeader(DeploymentNamespaceHeader)
return c.v.Namespace
}
2017-08-08 20:47:29 +00:00
// Name - print name
2017-06-27 22:40:47 +00:00
func (c *DeploymentContext) Name() string {
c.AddHeader(DeploymentNameHeader)
return c.v.Name
}
2017-06-28 10:01:11 +00:00
2017-08-08 20:47:29 +00:00
// Ready - print readiness
2017-06-28 10:01:11 +00:00
func (c *DeploymentContext) Ready() string {
c.AddHeader(DeploymentReadyHeader)
return fmt.Sprintf("%d/%d", c.v.AvailableReplicas, c.v.Replicas)
}
2017-08-08 20:47:29 +00:00
// Images - print used images
2017-06-28 10:01:11 +00:00
func (c *DeploymentContext) Images() string {
c.AddHeader(DeploymentImagesHeader)
return strings.Join(c.v.Images, ", ")
}