(ctx context.Context, rep repo.RepositoryWriter)
| 36 | } |
| 37 | |
| 38 | func (c *commandPolicyImport) run(ctx context.Context, rep repo.RepositoryWriter) error { |
| 39 | var input io.Reader |
| 40 | |
| 41 | var err error |
| 42 | |
| 43 | if c.filePath != "" { |
| 44 | file, err := os.Open(c.filePath) |
| 45 | if err != nil { |
| 46 | return errors.Wrap(err, "unable to read policy file") |
| 47 | } |
| 48 | |
| 49 | defer file.Close() //nolint:errcheck |
| 50 | |
| 51 | input = file |
| 52 | } else { |
| 53 | input = c.svc.stdin() |
| 54 | } |
| 55 | |
| 56 | policies := make(map[string]*policy.Policy) |
| 57 | d := json.NewDecoder(input) |
| 58 | |
| 59 | if !c.allowUnknownFields { |
| 60 | d.DisallowUnknownFields() |
| 61 | } |
| 62 | |
| 63 | err = d.Decode(&policies) |
| 64 | if err != nil { |
| 65 | return errors.Wrap(err, "unable to decode policy file as valid json") |
| 66 | } |
| 67 | |
| 68 | var targetLimit []snapshot.SourceInfo |
| 69 | |
| 70 | if c.global || len(c.targets) > 0 { |
| 71 | targetLimit, err = c.policyTargets(ctx, rep) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | shouldImportSource := func(target snapshot.SourceInfo) bool { |
| 78 | if targetLimit == nil { |
| 79 | return true |
| 80 | } |
| 81 | |
| 82 | return slices.Contains(targetLimit, target) |
| 83 | } |
| 84 | |
| 85 | importedSources := make([]string, 0, len(policies)) |
| 86 | |
| 87 | for ts, newPolicy := range policies { |
| 88 | target, err := snapshot.ParseSourceInfo(ts, rep.ClientOptions().Hostname, rep.ClientOptions().Username) |
| 89 | if err != nil { |
| 90 | return errors.Wrapf(err, "unable to parse source info: %q", ts) |
| 91 | } |
| 92 | |
| 93 | if !shouldImportSource(target) { |
| 94 | continue |
| 95 | } |
nothing calls this directly
no test coverage detected