Skip to content

Commit

Permalink
Multiple report outputs (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
aegershman authored Dec 9, 2019
1 parent d8d87e2 commit fa94263
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ cf report-usage
cf report-usage -o voyager
cf report-usage -o voyager -o tenzing [-o orgName...]

# report using different formats
# report using specific formats
cf report-usage -o voyager -o tenzing --format table
cf report-usage -o voyager -o tenzing --format string
cf report-usage -o voyager -o tenzing --format json

# or output multiple report formats in the same run by specifying --format multiple times
cf report-usage -o voyager -o tenzing --format table --format json
cf report-usage -o voyager -o tenzing [--format formatName...]
```

`--format table`:
Expand Down Expand Up @@ -225,8 +229,7 @@ This example is going to be a bit cluttered, so it's recommended using `jq` to p
"spring_cloud_services_count": 8,
"stopped_app_instances_count": 7,
"stopped_apps_count": 28
},
"format": "json"
}
}
```

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

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

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

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

## backwards compatibility

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`.

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.
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.

## use in pivotal licensing

Expand Down
10 changes: 2 additions & 8 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ tasks:
test:
cmds:
- task: install
# TODO wasteful; maybe ability to specify multiple formats on a single run...?
- cf report-usage -o voyager -o tenzing --format string
- cf report-usage -o voyager -o tenzing --format table
- cf report-usage -o voyager -o tenzing --format json
# TODO will remove this last line, shouldn't be displayed in README
- cf report-usage -o voyager -o tenzing --format table-org-quota
# TODO long running / test failures
- cf report-usage --format table-org-quota
- cf report-usage -o not-real-org
- cf report-usage -o voyager --format table-org-quota # TODO on this
- cf report-usage -o voyager -o tenzing --format string --format table --format json
- task: uninstall
29 changes: 21 additions & 8 deletions cmd/reportusage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,31 @@ func (o *orgNamesFlag) Set(value string) error {
return nil
}

type formatFlag struct {
formats []string
}

func (f *formatFlag) String() string {
return fmt.Sprint(f.formats)
}

func (f *formatFlag) Set(value string) error {
f.formats = append(f.formats, value)
return nil
}

// reportUsageCommand is the "real" main entrypoint into program execution
func (cmd *reportUsageCmd) reportUsageCommand(cli plugin.CliConnection, args []string) {

var (
orgNamesFlag orgNamesFlag
formatFlag string
formatFlag formatFlag
logLevelFlag string
)

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

presenter := presentation.NewPresenter(*summaryReport, formatFlag) // todo hacky pointer
presenter := presentation.NewPresenter(*summaryReport, formatFlag.formats...) // todo hacky pointer
presenter.Render()
}

Expand All @@ -64,7 +77,7 @@ func (cmd *reportUsageCmd) Run(cli plugin.CliConnection, args []string) {
case "report-usage":
cmd.reportUsageCommand(cli, args)
default:
log.Infoln("did you know plugin commands can still get ran when uninstalling a plugin? interesting, right?")
log.Debugln("did you know plugin commands can still get ran when uninstalling a plugin? interesting, right?")
return
}
}
Expand All @@ -75,18 +88,18 @@ func (cmd *reportUsageCmd) GetMetadata() plugin.PluginMetadata {
Name: "cf-report-usage-plugin",
Version: plugin.VersionType{
Major: 3,
Minor: 1,
Build: 1,
Minor: 2,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "report-usage",
HelpText: "View AIs, SIs and memory usage for orgs and spaces",
UsageDetails: plugin.Usage{
Usage: "cf report-usage [-o orgName...] --format formatChoice",
Usage: "cf report-usage [-o orgName...] [--format formatChoice...]",
Options: map[string]string{
"o": "organization(s) included in report. Flag can be provided multiple times.",
"format": "format to print as (options: string,table,json) (default: table)",
"format": "format to print as (options: string,table,json). Flag can be provided multiple times.",
"log-level": "(options: info,debug,trace) (default: info)",
},
},
Expand Down
38 changes: 21 additions & 17 deletions internal/presentation/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@ package presentation

import (
"github.com/aegershman/cf-report-usage-plugin/internal/report"
log "github.com/sirupsen/logrus"
)

// Presenter -
type Presenter struct {
SummaryReport report.SummaryReport `json:"summary_report"`
Format string `json:"format"`
formats []string
}

// NewPresenter -
func NewPresenter(r report.SummaryReport, format string) Presenter {
func NewPresenter(r report.SummaryReport, formats ...string) Presenter {
return Presenter{
SummaryReport: r,
Format: format,
formats: formats,
}
}

// Render -
func (p *Presenter) Render() {
switch p.Format {
case "json":
p.asJSON()
case "string":
p.asString()
case "table-org-quota": // again, TODO, bleh
p.asTableOrgQuota()
case "table":
// TODO better handling of defaults
if len(p.formats) == 0 {
p.asTable()
default:
// TODO
// yeah this is kind of awful I know, I'm sorry, I'm still learning,
// I'll fix this along with much better and earlier error handling on this
// I'll fix this, I promise
p.asString()
}

for _, format := range p.formats {
switch format {
case "json":
p.asJSON()
case "string":
p.asString()
case "table-org-quota": // again, TODO, get rid of this, bleh
p.asTableOrgQuota()
case "table":
p.asTable()
default:
log.Debugf("could not identify presentation format %s\n", format)
}
}
}

0 comments on commit fa94263

Please sign in to comment.