Skip to content

Commit fa94263

Browse files
authored
Multiple report outputs (#104)
1 parent d8d87e2 commit fa94263

File tree

4 files changed

+54
-42
lines changed

4 files changed

+54
-42
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ cf report-usage
1212
cf report-usage -o voyager
1313
cf report-usage -o voyager -o tenzing [-o orgName...]
1414

15-
# report using different formats
15+
# report using specific formats
1616
cf report-usage -o voyager -o tenzing --format table
1717
cf report-usage -o voyager -o tenzing --format string
1818
cf report-usage -o voyager -o tenzing --format json
19+
20+
# or output multiple report formats in the same run by specifying --format multiple times
21+
cf report-usage -o voyager -o tenzing --format table --format json
22+
cf report-usage -o voyager -o tenzing [--format formatName...]
1923
```
2024

2125
`--format table`:
@@ -225,8 +229,7 @@ This example is going to be a bit cluttered, so it's recommended using `jq` to p
225229
"spring_cloud_services_count": 8,
226230
"stopped_app_instances_count": 7,
227231
"stopped_apps_count": 28
228-
},
229-
"format": "json"
232+
}
230233
}
231234
```
232235

@@ -236,20 +239,18 @@ If you want to try it out, install it directly from [the github releases tab as
236239

237240
```sh
238241
# osx 64bit
239-
cf install-plugin -f https://github.com/aegershman/cf-report-usage-plugin/releases/download/3.1.1/cf-report-usage-plugin-darwin
242+
cf install-plugin -f https://github.com/aegershman/cf-report-usage-plugin/releases/download/3.2.0/cf-report-usage-plugin-darwin
240243

241244
# linux 64bit (32bit and ARM6 also available)
242-
cf install-plugin -f https://github.com/aegershman/cf-report-usage-plugin/releases/download/3.1.1/cf-report-usage-plugin-linux-amd64
245+
cf install-plugin -f https://github.com/aegershman/cf-report-usage-plugin/releases/download/3.2.0/cf-report-usage-plugin-linux-amd64
243246

244247
# windows 64bit (32bit also available)
245-
cf install-plugin -f https://github.com/aegershman/cf-report-usage-plugin/releases/download/3.1.1/cf-report-usage-plugin-windows-amd64.exe
248+
cf install-plugin -f https://github.com/aegershman/cf-report-usage-plugin/releases/download/3.2.0/cf-report-usage-plugin-windows-amd64.exe
246249
```
247250

248251
## backwards compatibility
249252

250-
To be honest, I wouldn't describe this plugin as "totally ready" yet. It's not where I want it yet. I'd consider `3.x.x` to be the demarcation point, I suppose. I will do the best I can to maintain backwards compatibility with the current set of properties that can be rendered by a presenter. However, the only output format changes I'll _really_ care about is the `--format json`.
251-
252-
Changes to any `json` rendering will warrant a `minor` bump for the time being. Changes to either the `string` or `table` presenter can qualify as `patch` bumps, because they're just for humans. The `json` presenter, however, I want to be more careful with, because it's more versatile for machine usage.
253+
To be honest, I wouldn't describe this plugin as "totally ready" yet. It's not where I want it yet. I will do the best I can to maintain backwards compatibility with the current set of properties that can be rendered by a presenter.
253254

254255
## use in pivotal licensing
255256

Taskfile.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ tasks:
1919
test:
2020
cmds:
2121
- task: install
22-
# TODO wasteful; maybe ability to specify multiple formats on a single run...?
23-
- cf report-usage -o voyager -o tenzing --format string
24-
- cf report-usage -o voyager -o tenzing --format table
25-
- cf report-usage -o voyager -o tenzing --format json
26-
# TODO will remove this last line, shouldn't be displayed in README
27-
- cf report-usage -o voyager -o tenzing --format table-org-quota
28-
# TODO long running / test failures
29-
- cf report-usage --format table-org-quota
3022
- cf report-usage -o not-real-org
23+
- cf report-usage -o voyager --format table-org-quota # TODO on this
24+
- cf report-usage -o voyager -o tenzing --format string --format table --format json
3125
- task: uninstall

cmd/reportusage/main.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,31 @@ func (o *orgNamesFlag) Set(value string) error {
2525
return nil
2626
}
2727

28+
type formatFlag struct {
29+
formats []string
30+
}
31+
32+
func (f *formatFlag) String() string {
33+
return fmt.Sprint(f.formats)
34+
}
35+
36+
func (f *formatFlag) Set(value string) error {
37+
f.formats = append(f.formats, value)
38+
return nil
39+
}
40+
2841
// reportUsageCommand is the "real" main entrypoint into program execution
2942
func (cmd *reportUsageCmd) reportUsageCommand(cli plugin.CliConnection, args []string) {
3043

3144
var (
3245
orgNamesFlag orgNamesFlag
33-
formatFlag string
46+
formatFlag formatFlag
3447
logLevelFlag string
3548
)
3649

3750
flagss := flag.NewFlagSet(args[0], flag.ContinueOnError)
3851
flagss.Var(&orgNamesFlag, "o", "")
39-
flagss.StringVar(&formatFlag, "format", "table", "")
52+
flagss.Var(&formatFlag, "format", "")
4053
flagss.StringVar(&logLevelFlag, "log-level", "info", "")
4154
if err := flagss.Parse(args[1:]); err != nil {
4255
log.Fatalln(err)
@@ -54,7 +67,7 @@ func (cmd *reportUsageCmd) reportUsageCommand(cli plugin.CliConnection, args []s
5467
log.Fatalln(err)
5568
}
5669

57-
presenter := presentation.NewPresenter(*summaryReport, formatFlag) // todo hacky pointer
70+
presenter := presentation.NewPresenter(*summaryReport, formatFlag.formats...) // todo hacky pointer
5871
presenter.Render()
5972
}
6073

@@ -64,7 +77,7 @@ func (cmd *reportUsageCmd) Run(cli plugin.CliConnection, args []string) {
6477
case "report-usage":
6578
cmd.reportUsageCommand(cli, args)
6679
default:
67-
log.Infoln("did you know plugin commands can still get ran when uninstalling a plugin? interesting, right?")
80+
log.Debugln("did you know plugin commands can still get ran when uninstalling a plugin? interesting, right?")
6881
return
6982
}
7083
}
@@ -75,18 +88,18 @@ func (cmd *reportUsageCmd) GetMetadata() plugin.PluginMetadata {
7588
Name: "cf-report-usage-plugin",
7689
Version: plugin.VersionType{
7790
Major: 3,
78-
Minor: 1,
79-
Build: 1,
91+
Minor: 2,
92+
Build: 0,
8093
},
8194
Commands: []plugin.Command{
8295
{
8396
Name: "report-usage",
8497
HelpText: "View AIs, SIs and memory usage for orgs and spaces",
8598
UsageDetails: plugin.Usage{
86-
Usage: "cf report-usage [-o orgName...] --format formatChoice",
99+
Usage: "cf report-usage [-o orgName...] [--format formatChoice...]",
87100
Options: map[string]string{
88101
"o": "organization(s) included in report. Flag can be provided multiple times.",
89-
"format": "format to print as (options: string,table,json) (default: table)",
102+
"format": "format to print as (options: string,table,json). Flag can be provided multiple times.",
90103
"log-level": "(options: info,debug,trace) (default: info)",
91104
},
92105
},

internal/presentation/presenter.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,42 @@ package presentation
22

33
import (
44
"github.com/aegershman/cf-report-usage-plugin/internal/report"
5+
log "github.com/sirupsen/logrus"
56
)
67

78
// Presenter -
89
type Presenter struct {
910
SummaryReport report.SummaryReport `json:"summary_report"`
10-
Format string `json:"format"`
11+
formats []string
1112
}
1213

1314
// NewPresenter -
14-
func NewPresenter(r report.SummaryReport, format string) Presenter {
15+
func NewPresenter(r report.SummaryReport, formats ...string) Presenter {
1516
return Presenter{
1617
SummaryReport: r,
17-
Format: format,
18+
formats: formats,
1819
}
1920
}
2021

2122
// Render -
2223
func (p *Presenter) Render() {
23-
switch p.Format {
24-
case "json":
25-
p.asJSON()
26-
case "string":
27-
p.asString()
28-
case "table-org-quota": // again, TODO, bleh
29-
p.asTableOrgQuota()
30-
case "table":
24+
// TODO better handling of defaults
25+
if len(p.formats) == 0 {
3126
p.asTable()
32-
default:
33-
// TODO
34-
// yeah this is kind of awful I know, I'm sorry, I'm still learning,
35-
// I'll fix this along with much better and earlier error handling on this
36-
// I'll fix this, I promise
37-
p.asString()
27+
}
28+
29+
for _, format := range p.formats {
30+
switch format {
31+
case "json":
32+
p.asJSON()
33+
case "string":
34+
p.asString()
35+
case "table-org-quota": // again, TODO, get rid of this, bleh
36+
p.asTableOrgQuota()
37+
case "table":
38+
p.asTable()
39+
default:
40+
log.Debugf("could not identify presentation format %s\n", format)
41+
}
3842
}
3943
}

0 commit comments

Comments
 (0)