2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-01 08:30:49 +00:00

add JSON output support for restic key list cmd

Signed-off-by: Steve Kriss <steve@heptio.com>
This commit is contained in:
Steve Kriss 2018-06-18 15:20:37 -07:00
parent ae72b438b0
commit 5088905502
2 changed files with 65 additions and 16 deletions

View File

@ -0,0 +1,6 @@
Enhancement: Add JSON output support to `restic key list`
This PR enables users to get the output of `restic key list` in JSON in addition
to the existing table format.
https://github.com/restic/restic/pull/1853

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -35,33 +36,75 @@ func init() {
flags.StringVarP(&newPasswordFile, "new-password-file", "", "", "the file from which to load a new password") flags.StringVarP(&newPasswordFile, "new-password-file", "", "", "the file from which to load a new password")
} }
func listKeys(ctx context.Context, s *repository.Repository) error { type keyInfo struct {
tab := NewTable() Current bool `json:"current"`
tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created") ID string `json:"id"`
tab.RowFormat = "%s%-10s %-10s %-10s %s" UserName string `json:"userName"`
HostName string `json:"hostName"`
Created string `json:"created"`
}
err := s.List(ctx, restic.KeyFile, func(id restic.ID, size int64) error { func (ki keyInfo) CurrentStr() string {
if ki.Current {
return "*"
}
return " "
}
func listKeys(ctx context.Context, s *repository.Repository, gopts GlobalOptions) error {
var (
appendKey func(keyInfo)
printKeys func() error
)
switch gopts.JSON {
case true:
var keys []keyInfo
appendKey = func(key keyInfo) {
keys = append(keys, key)
}
printKeys = func() error {
return json.NewEncoder(gopts.stdout).Encode(keys)
}
default:
tab := NewTable()
tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created")
tab.RowFormat = "%s%-10s %-10s %-10s %s"
appendKey = func(key keyInfo) {
tab.Rows = append(tab.Rows, []interface{}{key.CurrentStr(), key.ID, key.UserName, key.HostName, key.Created})
}
printKeys = func() error {
return tab.Write(globalOptions.stdout)
}
}
if err := s.List(ctx, restic.KeyFile, func(id restic.ID, size int64) error {
k, err := repository.LoadKey(ctx, s, id.String()) k, err := repository.LoadKey(ctx, s, id.String())
if err != nil { if err != nil {
Warnf("LoadKey() failed: %v\n", err) Warnf("LoadKey() failed: %v\n", err)
return nil return nil
} }
var current string key := keyInfo{
if id.String() == s.KeyName() { Current: id.String() == s.KeyName(),
current = "*" ID: id.Str(),
} else { UserName: k.Username,
current = " " HostName: k.Hostname,
Created: k.Created.Format(TimeFormat),
} }
tab.Rows = append(tab.Rows, []interface{}{current, id.Str(),
k.Username, k.Hostname, k.Created.Format(TimeFormat)}) appendKey(key)
return nil return nil
}) }); err != nil {
if err != nil {
return err return err
} }
return tab.Write(globalOptions.stdout) return printKeys()
} }
// testKeyNewPassword is used to set a new password during integration testing. // testKeyNewPassword is used to set a new password during integration testing.
@ -160,7 +203,7 @@ func runKey(gopts GlobalOptions, args []string) error {
return err return err
} }
return listKeys(ctx, repo) return listKeys(ctx, repo, gopts)
case "add": case "add":
lock, err := lockRepo(repo) lock, err := lockRepo(repo)
defer unlockRepo(lock) defer unlockRepo(lock)