stack.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package formatter
  2. import (
  3. "strconv"
  4. )
  5. const (
  6. defaultStackTableFormat = "table {{.Name}}\t{{.Services}}"
  7. stackServicesHeader = "SERVICES"
  8. )
  9. // Stack contains deployed stack information.
  10. type Stack struct {
  11. // Name is the name of the stack
  12. Name string
  13. // Services is the number of the services
  14. Services int
  15. }
  16. // NewStackFormat returns a format for use with a stack Context
  17. func NewStackFormat(source string) Format {
  18. switch source {
  19. case TableFormatKey:
  20. return defaultStackTableFormat
  21. }
  22. return Format(source)
  23. }
  24. // StackWrite writes formatted stacks using the Context
  25. func StackWrite(ctx Context, stacks []*Stack) error {
  26. render := func(format func(subContext subContext) error) error {
  27. for _, stack := range stacks {
  28. if err := format(&stackContext{s: stack}); err != nil {
  29. return err
  30. }
  31. }
  32. return nil
  33. }
  34. return ctx.Write(newStackContext(), render)
  35. }
  36. type stackContext struct {
  37. HeaderContext
  38. s *Stack
  39. }
  40. func newStackContext() *stackContext {
  41. stackCtx := stackContext{}
  42. stackCtx.header = map[string]string{
  43. "Name": nameHeader,
  44. "Services": stackServicesHeader,
  45. }
  46. return &stackCtx
  47. }
  48. func (s *stackContext) MarshalJSON() ([]byte, error) {
  49. return marshalJSON(s)
  50. }
  51. func (s *stackContext) Name() string {
  52. return s.s.Name
  53. }
  54. func (s *stackContext) Services() string {
  55. return strconv.Itoa(s.s.Services)
  56. }