showing deployments

pull/29/head
Karolis Rusenas 2017-06-27 23:40:47 +01:00
parent 28bc7c7751
commit e670db5013
2 changed files with 124 additions and 0 deletions

51
bot/formatter/custom.go Normal file
View File

@ -0,0 +1,51 @@
package formatter
import (
"strings"
)
const (
imageHeader = "IMAGE"
createdSinceHeader = "CREATED"
createdAtHeader = "CREATED AT"
sizeHeader = "SIZE"
labelsHeader = "LABELS"
nameHeader = "NAME"
driverHeader = "DRIVER"
scopeHeader = "SCOPE"
)
type subContext interface {
FullHeader() string
AddHeader(header string)
}
// HeaderContext provides the subContext interface for managing headers
type HeaderContext struct {
header []string
}
// FullHeader returns the header as a string
func (c *HeaderContext) FullHeader() string {
if c.header == nil {
return ""
}
return strings.Join(c.header, "\t")
}
// AddHeader adds another column to the header
func (c *HeaderContext) AddHeader(header string) {
if c.header == nil {
c.header = []string{}
}
c.header = append(c.header, strings.ToUpper(header))
}
func stripNamePrefix(ss []string) []string {
sss := make([]string, len(ss))
for i, s := range ss {
sss[i] = s[1:]
}
return sss
}

View File

@ -0,0 +1,73 @@
package formatter
import (
// "fmt"
// "strings"
log "github.com/Sirupsen/logrus"
)
// Deployment - internal deployment, used to better represent keel related info
type Deployment struct {
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
}
const (
defaultDeploymentQuietFormat = "{{.Name}}"
defaultDeploymentTableFormat = "table {{.Namespace}}\t{{.Name}}"
DeploymentNamespaceHeader = "NAMESPACE"
DeploymentNameHeader = "NAME"
)
// 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 {
log.WithFields(log.Fields{
"name": deployment.Name,
"namespace": deployment.Namespace,
}).Info("formatting deployment")
if err := format(&DeploymentContext{v: deployment}); err != nil {
return err
}
}
return nil
}
return ctx.Write(&DeploymentContext{}, render)
}
type DeploymentContext struct {
HeaderContext
v Deployment
}
func (c *DeploymentContext) MarshalJSON() ([]byte, error) {
return marshalJSON(c)
}
func (c *DeploymentContext) Namespace() string {
c.AddHeader(DeploymentNamespaceHeader)
return c.v.Namespace
}
func (c *DeploymentContext) Name() string {
c.AddHeader(DeploymentNameHeader)
return c.v.Name
}