feat(cli): add --json output to 'repo status' command (#1834)

* cleanup(repository) add kopia:sensitive tag to content.FormattingOptions field

* feat(cli): add --json output to  command
This commit is contained in:
Julio Lopez
2022-03-17 22:22:24 -07:00
committed by GitHub
parent c7ce87f95b
commit 3f9e27c97b
3 changed files with 70 additions and 4 deletions

View File

@@ -0,0 +1,23 @@
package cli_test
import (
"testing"
"github.com/kopia/kopia/cli"
"github.com/kopia/kopia/internal/testutil"
"github.com/kopia/kopia/tests/testenv"
)
func TestRepoStatusJSON(t *testing.T) {
t.Parallel()
e := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
var rs cli.RepositoryStatus
e.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", e.RepoDir)
defer e.RunAndExpectSuccess(t, "repo", "disconnect")
e.RunAndExpectSuccess(t, "repo", "status")
testutil.MustParseJSONLines(t, e.RunAndExpectSuccess(t, "repo", "status", "--json"), &rs)
}

View File

@@ -2,6 +2,7 @@
import (
"context"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
@@ -12,6 +13,9 @@
"github.com/kopia/kopia/internal/scrubber"
"github.com/kopia/kopia/internal/units"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/object"
)
type commandRepositoryStatus struct {
@@ -19,9 +23,22 @@ type commandRepositoryStatus struct {
statusReconnectTokenIncludePassword bool
svc advancedAppServices
jo jsonOutput
out textOutput
}
// RepositoryStatus is used to display the repository info in JSON format.
type RepositoryStatus struct {
ConfigFile string `json:"configFile"`
UniqueIDHex string `json:"uniqueIDHex"`
ClientOptions repo.ClientOptions `json:"clientOptions"`
Storage blob.ConnectionInfo `json:"storage"`
ContentFormat content.FormattingOptions `json:"contentFormat"`
ObjectFormat object.Format `json:"objectFormat"`
BlobRetention content.BlobCfgBlob `json:"blobRetention"`
}
func (c *commandRepositoryStatus) setup(svc advancedAppServices, parent commandParent) {
cmd := parent.Command("status", "Display the status of connected repository.")
cmd.Flag("reconnect-token", "Display reconnect command").Short('t').BoolVar(&c.statusReconnectToken)
@@ -30,9 +47,35 @@ func (c *commandRepositoryStatus) setup(svc advancedAppServices, parent commandP
c.svc = svc
c.out.setup(svc)
c.jo.setup(svc, cmd)
}
func (c *commandRepositoryStatus) outputJSON(r repo.Repository) error {
s := RepositoryStatus{
ConfigFile: c.svc.repositoryConfigFileName(),
ClientOptions: r.ClientOptions(),
}
dr, ok := r.(repo.DirectRepository)
if ok {
ci := dr.BlobReader().ConnectionInfo()
s.UniqueIDHex = hex.EncodeToString(dr.UniqueID())
s.ObjectFormat = dr.ObjectFormat()
s.BlobRetention = dr.BlobCfg()
s.Storage = scrubber.ScrubSensitiveData(reflect.ValueOf(ci)).Interface().(blob.ConnectionInfo) // nolint:forcetypeassert
s.ContentFormat = scrubber.ScrubSensitiveData(reflect.ValueOf(dr.ContentReader().ContentFormat())).Interface().(content.FormattingOptions) // nolint:forcetypeassert
}
c.out.printStdout("%s\n", c.jo.jsonBytes(s))
return nil
}
func (c *commandRepositoryStatus) run(ctx context.Context, rep repo.Repository) error {
if c.jo.jsonOutput {
return c.outputJSON(rep)
}
c.out.printStdout("Config file: %v\n", c.svc.repositoryConfigFileName())
c.out.printStdout("\n")
c.out.printStdout("Description: %v\n", rep.ClientOptions().Description)

View File

@@ -28,10 +28,10 @@
// FormattingOptions describes the rules for formatting contents in repository.
type FormattingOptions struct {
Hash string `json:"hash,omitempty"` // identifier of the hash algorithm used
Encryption string `json:"encryption,omitempty"` // identifier of the encryption algorithm used
HMACSecret []byte `json:"secret,omitempty"` // HMAC secret used to generate encryption keys
MasterKey []byte `json:"masterKey,omitempty"` // master encryption key (SIV-mode encryption only)
Hash string `json:"hash,omitempty"` // identifier of the hash algorithm used
Encryption string `json:"encryption,omitempty"` // identifier of the encryption algorithm used
HMACSecret []byte `json:"secret,omitempty" kopia:"sensitive"` // HMAC secret used to generate encryption keys
MasterKey []byte `json:"masterKey,omitempty" kopia:"sensitive"` // master encryption key (SIV-mode encryption only)
MutableParameters
UpgradeLock *UpgradeLock `json:"upgradeLock,omitempty"` // declares the intent to lock the repository for exclusive access during upgrade