(ctx context.Context, rep repo.RepositoryWriter)
| 64 | } |
| 65 | |
| 66 | func (c *commandPolicyEdit) run(ctx context.Context, rep repo.RepositoryWriter) error { |
| 67 | targets, err := c.policyTargets(ctx, rep) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | for _, target := range targets { |
| 73 | original, err := policy.GetDefinedPolicy(ctx, rep, target) |
| 74 | if errors.Is(err, policy.ErrPolicyNotFound) { |
| 75 | original = &policy.Policy{} |
| 76 | } |
| 77 | |
| 78 | log(ctx).Infof("Editing policy for %v using external editor...", target) |
| 79 | |
| 80 | s := fmt.Sprintf(policyEditHelpText, target) + prettyJSON(original) |
| 81 | s = insertHelpText(s, ` "retention": {`, policyEditRetentionHelpText) |
| 82 | s = insertHelpText(s, ` "files": {`, policyEditFilesHelpText) |
| 83 | s = insertHelpText(s, ` "scheduling": {`, policyEditSchedulingHelpText) |
| 84 | |
| 85 | var updated *policy.Policy |
| 86 | |
| 87 | if err := editor.EditLoop(ctx, "policy.conf", s, true, func(edited string) error { |
| 88 | updated = &policy.Policy{} |
| 89 | d := json.NewDecoder(bytes.NewBufferString(edited)) |
| 90 | d.DisallowUnknownFields() |
| 91 | |
| 92 | return d.Decode(updated) |
| 93 | }); err != nil { |
| 94 | return errors.Wrap(err, "unable to launch editor") |
| 95 | } |
| 96 | |
| 97 | if jsonEqual(updated, original) { |
| 98 | log(ctx).Infof("Policy for %v unchanged", target) |
| 99 | continue |
| 100 | } |
| 101 | |
| 102 | log(ctx).Infof("Updated policy for %v\n%v", target, prettyJSON(updated)) |
| 103 | |
| 104 | c.out.printStdout("Save updated policy? (y/N) ") |
| 105 | |
| 106 | var shouldSave string |
| 107 | |
| 108 | fmt.Scanf("%v", &shouldSave) //nolint:errcheck |
| 109 | |
| 110 | if strings.HasPrefix(strings.ToLower(shouldSave), "y") { |
| 111 | if err := policy.SetPolicy(ctx, rep, target, updated); err != nil { |
| 112 | return errors.Wrapf(err, "can't save policy for %v", target) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func prettyJSON(v *policy.Policy) string { |
| 121 | var b bytes.Buffer |
nothing calls this directly
no test coverage detected