mirror of
https://github.com/kopia/kopia.git
synced 2026-01-25 23:08:01 -05:00
* cli: added standard --json flags to several commands Fixes #272 * Update flag description Co-authored-by: Julio López <julio+gh@kasten.io>
46 lines
925 B
Go
46 lines
925 B
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/snapshot/policy"
|
|
)
|
|
|
|
var policyListCommand = policyCommands.Command("list", "List policies.").Alias("ls")
|
|
|
|
func init() {
|
|
registerJSONOutputFlags(policyListCommand)
|
|
policyListCommand.Action(repositoryReaderAction(listPolicies))
|
|
}
|
|
|
|
func listPolicies(ctx context.Context, rep repo.Repository) error {
|
|
var jl jsonList
|
|
|
|
jl.begin()
|
|
defer jl.end()
|
|
|
|
policies, err := policy.ListPolicies(ctx, rep)
|
|
if err != nil {
|
|
return errors.Wrap(err, "error listing policies")
|
|
}
|
|
|
|
sort.Slice(policies, func(i, j int) bool {
|
|
return policies[i].Target().String() < policies[j].Target().String()
|
|
})
|
|
|
|
for _, pol := range policies {
|
|
if jsonOutput {
|
|
jl.emit(policy.TargetWithPolicy{ID: pol.ID(), Target: pol.Target(), Policy: pol})
|
|
} else {
|
|
fmt.Println(pol.ID(), pol.Target())
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|