mirror of
https://github.com/octoleo/restic.git
synced 2024-12-23 19:38:57 +00:00
ls: Stream output when using --json option
This commit is contained in:
parent
d708d607fa
commit
04c67d700d
@ -3,9 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@ -64,25 +62,14 @@ func init() {
|
|||||||
|
|
||||||
type lsSnapshot struct {
|
type lsSnapshot struct {
|
||||||
*restic.Snapshot
|
*restic.Snapshot
|
||||||
|
|
||||||
ID *restic.ID `json:"id"`
|
ID *restic.ID `json:"id"`
|
||||||
ShortID string `json:"short_id"`
|
ShortID string `json:"short_id"`
|
||||||
Nodes []lsNode `json:"nodes"`
|
|
||||||
StructType string `json:"struct_type"` // "snapshot"
|
StructType string `json:"struct_type"` // "snapshot"
|
||||||
}
|
}
|
||||||
|
|
||||||
type lsNode struct {
|
type lsNode struct {
|
||||||
Name string `json:"name"`
|
*restic.Node
|
||||||
Type string `json:"type"`
|
StructType string `json:"struct_type"` // "node"
|
||||||
Path string `json:"path"`
|
|
||||||
UID uint32 `json:"uid"`
|
|
||||||
GID uint32 `json:"gid"`
|
|
||||||
Size uint64 `json:"size,omitempty"`
|
|
||||||
Mode os.FileMode `json:"mode,omitempty"`
|
|
||||||
ModTime time.Time `json:"mtime,omitempty"`
|
|
||||||
AccessTime time.Time `json:"atime,omitempty"`
|
|
||||||
ChangeTime time.Time `json:"ctime,omitempty"`
|
|
||||||
StructType string `json:"struct_type"` // "node"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runLs(opts LsOptions, gopts GlobalOptions, args []string) error {
|
func runLs(opts LsOptions, gopts GlobalOptions, args []string) error {
|
||||||
@ -150,54 +137,33 @@ func runLs(opts LsOptions, gopts GlobalOptions, args []string) error {
|
|||||||
var (
|
var (
|
||||||
printSnapshot func(sn *restic.Snapshot)
|
printSnapshot func(sn *restic.Snapshot)
|
||||||
printNode func(path string, node *restic.Node)
|
printNode func(path string, node *restic.Node)
|
||||||
printFinish func() error
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if gopts.JSON {
|
if gopts.JSON {
|
||||||
var lssnapshots []lsSnapshot
|
enc := json.NewEncoder(gopts.stdout)
|
||||||
|
|
||||||
printSnapshot = func(sn *restic.Snapshot) {
|
printSnapshot = func(sn *restic.Snapshot) {
|
||||||
lss := lsSnapshot{
|
enc.Encode(lsSnapshot{
|
||||||
Snapshot: sn,
|
Snapshot: sn,
|
||||||
ID: sn.ID(),
|
ID: sn.ID(),
|
||||||
ShortID: sn.ID().Str(),
|
ShortID: sn.ID().Str(),
|
||||||
StructType: "snapshot",
|
StructType: "snapshot",
|
||||||
}
|
})
|
||||||
lssnapshots = append(lssnapshots, lss)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printNode = func(path string, node *restic.Node) {
|
printNode = func(path string, node *restic.Node) {
|
||||||
lsn := lsNode{
|
enc.Encode(lsNode{
|
||||||
Name: node.Name,
|
Node: node,
|
||||||
Type: node.Type,
|
|
||||||
Path: path,
|
|
||||||
UID: node.UID,
|
|
||||||
GID: node.GID,
|
|
||||||
Size: node.Size,
|
|
||||||
Mode: node.Mode,
|
|
||||||
ModTime: node.ModTime,
|
|
||||||
AccessTime: node.AccessTime,
|
|
||||||
ChangeTime: node.ChangeTime,
|
|
||||||
StructType: "node",
|
StructType: "node",
|
||||||
}
|
})
|
||||||
s := &lssnapshots[len(lssnapshots)-1]
|
|
||||||
s.Nodes = append(s.Nodes, lsn)
|
|
||||||
}
|
|
||||||
|
|
||||||
printFinish = func() error {
|
|
||||||
return json.NewEncoder(gopts.stdout).Encode(lssnapshots)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// default output methods
|
|
||||||
printSnapshot = func(sn *restic.Snapshot) {
|
printSnapshot = func(sn *restic.Snapshot) {
|
||||||
Verbosef("snapshot %s of %v filtered by %v at %s):\n", sn.ID().Str(), sn.Paths, dirs, sn.Time)
|
Verbosef("snapshot %s of %v filtered by %v at %s):\n", sn.ID().Str(), sn.Paths, dirs, sn.Time)
|
||||||
}
|
}
|
||||||
printNode = func(path string, node *restic.Node) {
|
printNode = func(path string, node *restic.Node) {
|
||||||
Printf("%s\n", formatNode(path, node, lsOptions.ListLong))
|
Printf("%s\n", formatNode(path, node, lsOptions.ListLong))
|
||||||
}
|
}
|
||||||
printFinish = func() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args[:1]) {
|
for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args[:1]) {
|
||||||
@ -240,5 +206,6 @@ func runLs(opts LsOptions, gopts GlobalOptions, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return printFinish()
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -360,7 +360,9 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) {
|
|||||||
if len(id) > 8 {
|
if len(id) > 8 {
|
||||||
id = id[:8]
|
id = id[:8]
|
||||||
}
|
}
|
||||||
Verbosef("repository %v opened successfully, password is correct\n", id)
|
if !opts.JSON {
|
||||||
|
Verbosef("repository %v opened successfully, password is correct\n", id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.NoCache {
|
if opts.NoCache {
|
||||||
|
@ -50,7 +50,7 @@ type Node struct {
|
|||||||
|
|
||||||
Error string `json:"error,omitempty"`
|
Error string `json:"error,omitempty"`
|
||||||
|
|
||||||
Path string `json:"-"`
|
Path string `json:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nodes is a slice of nodes that can be sorted.
|
// Nodes is a slice of nodes that can be sorted.
|
||||||
|
Loading…
Reference in New Issue
Block a user