Skip to content

Commit

Permalink
Merge pull request #20 from anchore/remove-unused-code-presenters
Browse files Browse the repository at this point in the history
feat: remove presenters, only support json output
  • Loading branch information
bradleyjones committed Jan 5, 2023
2 parents 156cbd1 + 232d903 commit 0f07eb7
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 202 deletions.
14 changes: 1 addition & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/anchore/elastic-container-gatherer/ecg"
"github.com/anchore/elastic-container-gatherer/ecg/presenter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -44,18 +43,7 @@ var rootCmd = &cobra.Command{
}

func init() {
// output & formatting options
opt := "output"
rootCmd.Flags().StringP(
opt, "o", presenter.JSONPresenter.String(),
fmt.Sprintf("report output formatter, options=%v", presenter.Options),
)
if err := viper.BindPFlag(opt, rootCmd.Flags().Lookup(opt)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
os.Exit(1)
}

opt = "polling-interval-seconds"
opt := "polling-interval-seconds"
rootCmd.Flags().StringP(opt, "p", "300", "This specifies the polling interval of the ECS API in seconds")
if err := viper.BindPFlag(opt, rootCmd.Flags().Lookup(opt)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", opt, err)
Expand Down
20 changes: 15 additions & 5 deletions ecg/lib.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package ecg

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/anchore/elastic-container-gatherer/ecg/inventory"
"github.com/anchore/elastic-container-gatherer/ecg/logger"
"github.com/anchore/elastic-container-gatherer/ecg/presenter"
"github.com/anchore/elastic-container-gatherer/ecg/reporter"
"github.com/anchore/elastic-container-gatherer/internal/config"
"github.com/aws/aws-sdk-go/aws"
Expand All @@ -17,6 +17,18 @@ import (

var log logger.Logger

// Output the JSON formatted report to stdout
func reportToStdout(report inventory.Report) error {
enc := json.NewEncoder(os.Stdout)
// prevent > and < from being escaped in the payload
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(report); err != nil {
return fmt.Errorf("unable to show inventory: %w", err)
}
return nil
}

func HandleReport(report inventory.Report, cfg *config.Application) error {
if cfg.AnchoreDetails.IsValid() {
if err := reporter.Post(report, cfg.AnchoreDetails, cfg); err != nil {
Expand All @@ -26,10 +38,8 @@ func HandleReport(report inventory.Report, cfg *config.Application) error {
log.Debug("Anchore details not specified, not reporting inventory")
}

if err := presenter.GetPresenter(cfg.PresenterOpt, report).Present(os.Stdout); err != nil {
return fmt.Errorf("unable to show inventory: %w", err)
}
return nil
// Encode the report to JSON and output to stdout (maintains same behaviour as when multiple presenters were supported)
return reportToStdout(report)
}

// PeriodicallyGetInventoryReport periodically retrieve image results and report/output them according to the configuration.
Expand Down
30 changes: 0 additions & 30 deletions ecg/presenter/json/presenter.go

This file was deleted.

44 changes: 0 additions & 44 deletions ecg/presenter/option.go

This file was deleted.

27 changes: 0 additions & 27 deletions ecg/presenter/presenter.go

This file was deleted.

73 changes: 0 additions & 73 deletions ecg/presenter/table/presenter.go

This file was deleted.

10 changes: 0 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"gopkg.in/yaml.v2"

"github.com/adrg/xdg"
"github.com/anchore/elastic-container-gatherer/ecg/presenter"
"github.com/anchore/elastic-container-gatherer/internal"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
Expand All @@ -36,8 +35,6 @@ type CliOnlyOptions struct {
// All Application configurations
type Application struct {
ConfigPath string
PresenterOpt presenter.Option
Output string `mapstructure:"output"`
Log Logging `mapstructure:"log"`
CliOptions CliOnlyOptions
MissingTagPolicy MissingTagConf `mapstructure:"missing-tag-policy"`
Expand Down Expand Up @@ -121,13 +118,6 @@ func LoadConfigFromFile(v *viper.Viper, cliOpts *CliOnlyOptions) (*Application,

// Build the configuration object (to be used as a singleton)
func (cfg *Application) Build() error {
// set the presenter
presenterOption := presenter.ParseOption(cfg.Output)
if presenterOption == presenter.UnknownPresenter {
return fmt.Errorf("bad --output value '%s'", cfg.Output)
}
cfg.PresenterOpt = presenterOption

if cfg.Log.Level != "" {
if cfg.CliOptions.Verbosity > 0 {
return fmt.Errorf("cannot explicitly set log level (cfg file or env var) and use -v flag together")
Expand Down

0 comments on commit 0f07eb7

Please sign in to comment.