Skip to content

Commit

Permalink
refactor renderPlan to be more clear for use
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Oct 17, 2023
1 parent 8fe59b6 commit c768974
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tsuru/client/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func (a *app) String(simplified bool) string {
if !simplified && (a.Plan.Memory != 0 || a.Plan.CPUMilli != 0) {
buf.WriteString("\n")
buf.WriteString("App Plan:\n")
buf.WriteString(renderPlans([]apptypes.Plan{a.Plan}, false, false, false))
buf.WriteString(renderPlans([]apptypes.Plan{a.Plan}, renderPlansOpts{}))
}
if !simplified && internalAddressesTable.Rows() > 0 {
buf.WriteString("\n")
Expand Down
18 changes: 11 additions & 7 deletions tsuru/client/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func (c *PlanList) Info() *cmd.Info {
}
}

func renderPlans(plans []apptypes.Plan, isBytes, showDefaultColumn bool, showMaxBurstAllowed bool) string {
type renderPlansOpts struct {
isBytes, showDefaultColumn, showMaxBurstAllowed bool
}

func renderPlans(plans []apptypes.Plan, opts renderPlansOpts) string {
table := tablecli.NewTable()
table.Headers = []string{"Name", "CPU", "Memory"}

Expand All @@ -64,17 +68,17 @@ func renderPlans(plans []apptypes.Plan, isBytes, showDefaultColumn bool, showMax
table.Headers = append(table.Headers, "CPU Burst (default)")
}

if showBurstColumn && showMaxBurstAllowed {
if showBurstColumn && opts.showMaxBurstAllowed {
table.Headers = append(table.Headers, "CPU Burst (max customizable)")
}

if showDefaultColumn {
if opts.showDefaultColumn {
table.Headers = append(table.Headers, "Default")
}

for _, p := range plans {
var cpu, memory string
if isBytes {
if opts.isBytes {
memory = fmt.Sprintf("%d", p.Memory)
} else {
memory = resource.NewQuantity(p.Memory, resource.BinarySI).String()
Expand Down Expand Up @@ -107,11 +111,11 @@ func renderPlans(plans []apptypes.Plan, isBytes, showDefaultColumn bool, showMax
row = append(row, displayCPUBurst(p.CPUMilli, cpuBurst)+cpuBurstObservation)
}

if showBurstColumn && showMaxBurstAllowed {
if showBurstColumn && opts.showMaxBurstAllowed {
row = append(row, displayCPUBurst(p.CPUMilli, p.CPUBurst.MaxAllowed))
}

if showDefaultColumn {
if opts.showDefaultColumn {
row = append(row, strconv.FormatBool(p.Default))
}
table.AddRow(row)
Expand Down Expand Up @@ -224,7 +228,7 @@ func (c *PlanList) Run(context *cmd.Context, client *cmd.Client) error {
if c.k8sFriendly {
fmt.Fprintf(context.Stdout, "%s", renderPlansK8SFriendly(plans, c.showMaxBurstAllowed))
} else {
fmt.Fprintf(context.Stdout, "%s", renderPlans(plans, c.bytes, true, c.showMaxBurstAllowed))
fmt.Fprintf(context.Stdout, "%s", renderPlans(plans, renderPlansOpts{isBytes: c.bytes, showDefaultColumn: true, showMaxBurstAllowed: c.showMaxBurstAllowed}))
}

return nil
Expand Down

0 comments on commit c768974

Please sign in to comment.