|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + cmderrors "github.com/snyk/driftctl/pkg/cmd/errors" |
| 9 | + "github.com/snyk/driftctl/pkg/cmd/scan/output" |
| 10 | + "github.com/snyk/driftctl/pkg/iac/config" |
| 11 | + "github.com/snyk/driftctl/pkg/iac/supplier" |
| 12 | + "github.com/snyk/driftctl/pkg/iac/terraform/state/backend" |
| 13 | +) |
| 14 | + |
| 15 | +func parseFromFlag(from []string) ([]config.SupplierConfig, error) { |
| 16 | + |
| 17 | + configs := make([]config.SupplierConfig, 0, len(from)) |
| 18 | + |
| 19 | + for _, flag := range from { |
| 20 | + schemePath := strings.Split(flag, "://") |
| 21 | + if len(schemePath) != 2 || schemePath[1] == "" || schemePath[0] == "" { |
| 22 | + return nil, errors.Wrapf( |
| 23 | + cmderrors.NewUsageError( |
| 24 | + fmt.Sprintf( |
| 25 | + "\nAccepted schemes are: %s", |
| 26 | + strings.Join(supplier.GetSupportedSchemes(), ","), |
| 27 | + ), |
| 28 | + ), |
| 29 | + "Unable to parse from flag '%s'", |
| 30 | + flag, |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + scheme := schemePath[0] |
| 35 | + path := schemePath[1] |
| 36 | + supplierBackend := strings.Split(scheme, "+") |
| 37 | + if len(supplierBackend) > 2 { |
| 38 | + return nil, errors.Wrapf( |
| 39 | + cmderrors.NewUsageError(fmt.Sprintf( |
| 40 | + "\nAccepted schemes are: %s", |
| 41 | + strings.Join(supplier.GetSupportedSchemes(), ","), |
| 42 | + ), |
| 43 | + ), |
| 44 | + "Unable to parse from scheme '%s'", |
| 45 | + scheme, |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + supplierKey := supplierBackend[0] |
| 50 | + if !supplier.IsSupplierSupported(supplierKey) { |
| 51 | + return nil, errors.Wrapf( |
| 52 | + cmderrors.NewUsageError( |
| 53 | + fmt.Sprintf( |
| 54 | + "\nAccepted values are: %s", |
| 55 | + strings.Join(supplier.GetSupportedSuppliers(), ","), |
| 56 | + ), |
| 57 | + ), |
| 58 | + "Unsupported IaC source '%s'", |
| 59 | + supplierKey, |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + backendString := "" |
| 64 | + if len(supplierBackend) == 2 { |
| 65 | + backendString = supplierBackend[1] |
| 66 | + if !backend.IsSupported(backendString) { |
| 67 | + return nil, errors.Wrapf( |
| 68 | + cmderrors.NewUsageError( |
| 69 | + fmt.Sprintf( |
| 70 | + "\nAccepted values are: %s", |
| 71 | + strings.Join(backend.GetSupportedBackends(), ","), |
| 72 | + ), |
| 73 | + ), |
| 74 | + "Unsupported IaC backend '%s'", |
| 75 | + backendString, |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + configs = append(configs, config.SupplierConfig{ |
| 81 | + Key: supplierKey, |
| 82 | + Backend: backendString, |
| 83 | + Path: path, |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + return configs, nil |
| 88 | +} |
| 89 | + |
| 90 | +func parseOutputFlags(out []string) ([]output.OutputConfig, error) { |
| 91 | + result := make([]output.OutputConfig, 0, len(out)) |
| 92 | + for _, v := range out { |
| 93 | + o, err := parseOutputFlag(v) |
| 94 | + if err != nil { |
| 95 | + return result, err |
| 96 | + } |
| 97 | + result = append(result, *o) |
| 98 | + } |
| 99 | + return result, nil |
| 100 | +} |
| 101 | + |
| 102 | +func parseOutputFlag(out string) (*output.OutputConfig, error) { |
| 103 | + schemeOpts := strings.Split(out, "://") |
| 104 | + if len(schemeOpts) < 2 || schemeOpts[0] == "" { |
| 105 | + return nil, errors.Wrapf( |
| 106 | + cmderrors.NewUsageError( |
| 107 | + fmt.Sprintf( |
| 108 | + "\nAccepted formats are: %s", |
| 109 | + strings.Join(output.SupportedOutputsExample(), ","), |
| 110 | + ), |
| 111 | + ), |
| 112 | + "Unable to parse output flag '%s'", |
| 113 | + out, |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + o := &output.OutputConfig{ |
| 118 | + Key: schemeOpts[0], |
| 119 | + } |
| 120 | + if !output.IsSupported(o.Key) { |
| 121 | + return nil, errors.Wrapf( |
| 122 | + cmderrors.NewUsageError( |
| 123 | + fmt.Sprintf( |
| 124 | + "\nValid formats are: %s", |
| 125 | + strings.Join(output.SupportedOutputsExample(), ","), |
| 126 | + ), |
| 127 | + ), |
| 128 | + "Unsupported output '%s'", |
| 129 | + o.Key, |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + opts := schemeOpts[1:] |
| 134 | + |
| 135 | + switch o.Key { |
| 136 | + case output.JSONOutputType: |
| 137 | + if len(opts) != 1 || opts[0] == "" { |
| 138 | + return nil, errors.Wrapf( |
| 139 | + cmderrors.NewUsageError( |
| 140 | + fmt.Sprintf( |
| 141 | + "\nMust be of kind: %s", |
| 142 | + output.Example(output.JSONOutputType), |
| 143 | + ), |
| 144 | + ), |
| 145 | + "Invalid json output '%s'", |
| 146 | + out, |
| 147 | + ) |
| 148 | + } |
| 149 | + o.Path = opts[0] |
| 150 | + case output.HTMLOutputType: |
| 151 | + if len(opts) != 1 || opts[0] == "" { |
| 152 | + return nil, errors.Wrapf( |
| 153 | + cmderrors.NewUsageError( |
| 154 | + fmt.Sprintf( |
| 155 | + "\nMust be of kind: %s", |
| 156 | + output.Example(output.HTMLOutputType), |
| 157 | + ), |
| 158 | + ), |
| 159 | + "Invalid html output '%s'", |
| 160 | + out, |
| 161 | + ) |
| 162 | + } |
| 163 | + o.Path = opts[0] |
| 164 | + case output.PlanOutputType: |
| 165 | + if len(opts) != 1 || opts[0] == "" { |
| 166 | + return nil, errors.Wrapf( |
| 167 | + cmderrors.NewUsageError( |
| 168 | + fmt.Sprintf( |
| 169 | + "\nMust be of kind: %s", |
| 170 | + output.Example(output.PlanOutputType), |
| 171 | + ), |
| 172 | + ), |
| 173 | + "Invalid plan output '%s'", |
| 174 | + out, |
| 175 | + ) |
| 176 | + } |
| 177 | + o.Path = opts[0] |
| 178 | + } |
| 179 | + |
| 180 | + return o, nil |
| 181 | +} |
0 commit comments