From c6ea94f6ce6b58089484440f99e953c267c92104 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Fri, 9 Apr 2021 13:15:16 +0200 Subject: [PATCH] refactor: NewDriftCTL interface --- pkg/cmd/scan.go | 21 ++++----------------- pkg/driftctl.go | 27 +++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/pkg/cmd/scan.go b/pkg/cmd/scan.go index 9d9bed32..a764c183 100644 --- a/pkg/cmd/scan.go +++ b/pkg/cmd/scan.go @@ -7,7 +7,6 @@ import ( "strings" "syscall" - "github.com/jmespath/go-jmespath" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -26,20 +25,8 @@ import ( "github.com/cloudskiff/driftctl/pkg/terraform" ) -type ScanOptions struct { - Coverage bool - Detect bool - From []config.SupplierConfig - To string - Output output.OutputConfig - Filter *jmespath.JMESPath - Quiet bool - BackendOptions *backend.Options - Strict bool -} - func NewScanCmd() *cobra.Command { - opts := &ScanOptions{} + opts := &pkg.ScanOptions{} opts.BackendOptions = &backend.Options{} cmd := &cobra.Command{ @@ -138,7 +125,7 @@ func NewScanCmd() *cobra.Command { "Use those HTTP headers to query the provided URL.\n"+ "Only used with tfstate+http(s) backend for now.\n", ) - fl.BoolVar(&opts.Strict, + fl.BoolVar(&opts.StrictMode, "strict", false, "Includes cloud provider service-linked roles (disabled by default)", @@ -147,7 +134,7 @@ func NewScanCmd() *cobra.Command { return cmd } -func scanRun(opts *ScanOptions) error { +func scanRun(opts *pkg.ScanOptions) error { selectedOutput := output.GetOutput(opts.Output, opts.Quiet) c := make(chan os.Signal) @@ -180,7 +167,7 @@ func scanRun(opts *ScanOptions) error { resFactory := terraform.NewTerraformResourceFactory(providerLibrary) - ctl := pkg.NewDriftCTL(scanner, iacSupplier, opts.Filter, alerter, resFactory, opts.Strict) + ctl := pkg.NewDriftCTL(scanner, iacSupplier, alerter, resFactory, opts) go func() { <-c diff --git a/pkg/driftctl.go b/pkg/driftctl.go index e45b9353..0ef82f31 100644 --- a/pkg/driftctl.go +++ b/pkg/driftctl.go @@ -9,11 +9,26 @@ import ( "github.com/cloudskiff/driftctl/pkg/alerter" "github.com/cloudskiff/driftctl/pkg/analyser" + "github.com/cloudskiff/driftctl/pkg/cmd/scan/output" "github.com/cloudskiff/driftctl/pkg/filter" + "github.com/cloudskiff/driftctl/pkg/iac/config" + "github.com/cloudskiff/driftctl/pkg/iac/terraform/state/backend" "github.com/cloudskiff/driftctl/pkg/middlewares" "github.com/cloudskiff/driftctl/pkg/resource" ) +type ScanOptions struct { + Coverage bool + Detect bool + From []config.SupplierConfig + To string + Output output.OutputConfig + Filter *jmespath.JMESPath + Quiet bool + BackendOptions *backend.Options + StrictMode bool +} + type DriftCTL struct { remoteSupplier resource.Supplier iacSupplier resource.Supplier @@ -24,8 +39,16 @@ type DriftCTL struct { strictMode bool } -func NewDriftCTL(remoteSupplier resource.Supplier, iacSupplier resource.Supplier, filter *jmespath.JMESPath, alerter *alerter.Alerter, resFactory resource.ResourceFactory, strictMode bool) *DriftCTL { - return &DriftCTL{remoteSupplier, iacSupplier, alerter, analyser.NewAnalyzer(alerter), filter, resFactory, strictMode} +func NewDriftCTL(remoteSupplier resource.Supplier, iacSupplier resource.Supplier, alerter *alerter.Alerter, resFactory resource.ResourceFactory, opts *ScanOptions) *DriftCTL { + return &DriftCTL{ + remoteSupplier, + iacSupplier, + alerter, + analyser.NewAnalyzer(alerter), + opts.Filter, + resFactory, + opts.StrictMode, + } } func (d DriftCTL) Run() (*analyser.Analysis, error) {