From a6ae79b39e9bbf4090747c247f2a01197c47f7b7 Mon Sep 17 00:00:00 2001 From: Kyle Brennan Date: Sat, 28 Nov 2020 01:54:21 -0800 Subject: [PATCH 1/4] support json output for init command --- cmd/restic/cmd_init.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/cmd/restic/cmd_init.go b/cmd/restic/cmd_init.go index f833369ef..2e293e040 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -1,7 +1,9 @@ package main import ( + "bytes" "context" + "encoding/json" "strconv" "github.com/restic/chunker" @@ -49,6 +51,12 @@ func init() { } func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []string) error { + type JSONSuccess struct { + Status string `json:"status"` + ID string `json:"id"` + Repository string `json:"repository"` + } + var version uint if opts.RepositoryVersion == "latest" || opts.RepositoryVersion == "" { version = restic.MaxRepoVersion @@ -100,15 +108,34 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args [] return errors.Fatalf("create key in repository at %s failed: %v\n", location.StripPassword(gopts.Repo), err) } - Verbosef("created restic repository %v at %s\n", s.Config().ID[:10], location.StripPassword(gopts.Repo)) - Verbosef("\n") - Verbosef("Please note that knowledge of your password is required to access\n") - Verbosef("the repository. Losing your password means that your data is\n") - Verbosef("irrecoverably lost.\n") + if !gopts.JSON { + Verbosef("created restic repository %v at %s\n", s.Config().ID[:10], location.StripPassword(gopts.Repo)) + Verbosef("\n") + Verbosef("Please note that knowledge of your password is required to access\n") + Verbosef("the repository. Losing your password means that your data is\n") + Verbosef("irrecoverably lost.\n") + + } else { + status := JSONSuccess{ + Status: "success", + ID: s.Config().ID, + Repository: location.StripPassword(gopts.Repo), + } + Verbosef(toJSONString(status)) + } return nil } +func toJSONString(status interface{}) string { + buf := new(bytes.Buffer) + err := json.NewEncoder(buf).Encode(status) + if err != nil { + panic("ERROR: Could not encode JSON string") + } + return buf.String() +} + func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts GlobalOptions) (*chunker.Pol, error) { if opts.CopyChunkerParameters { otherGopts, _, err := fillSecondaryGlobalOpts(opts.secondaryRepoOptions, gopts, "secondary") From 933c9af328cc7f0f9b70f1d93d2f23310fd667bc Mon Sep 17 00:00:00 2001 From: Kyle Brennan Date: Sat, 28 Nov 2020 01:56:45 -0800 Subject: [PATCH 2/4] create changelog entry for issue-3124 and pull-3132 --- changelog/unreleased/issue-3124 | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 changelog/unreleased/issue-3124 diff --git a/changelog/unreleased/issue-3124 b/changelog/unreleased/issue-3124 new file mode 100644 index 000000000..bfd75b93d --- /dev/null +++ b/changelog/unreleased/issue-3124 @@ -0,0 +1,7 @@ +Enhancement: Enable support for json output on init command + +Init command did not listen to the --json flag, it now outputs a struct depending on a successful or failed creation of a repository, with all relevant information + + +https://github.com/restic/restic/issues/3124 +https://github.com/restic/restic/pull/3132 From 9a9f559806a8c62ee6200792b95d0489a9d9ebbc Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 21 Oct 2022 22:32:11 +0200 Subject: [PATCH 3/4] init: cleanup json print code --- changelog/unreleased/issue-3124 | 6 +++--- cmd/restic/cmd_init.go | 26 ++++++++------------------ 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/changelog/unreleased/issue-3124 b/changelog/unreleased/issue-3124 index bfd75b93d..25d754eed 100644 --- a/changelog/unreleased/issue-3124 +++ b/changelog/unreleased/issue-3124 @@ -1,7 +1,7 @@ -Enhancement: Enable support for json output on init command - -Init command did not listen to the --json flag, it now outputs a struct depending on a successful or failed creation of a repository, with all relevant information +Enhancement: Support json output for the `init` command +The `init` command ignored the `--json` flag. It now outputs a JSON message if +the repository was created successfully. https://github.com/restic/restic/issues/3124 https://github.com/restic/restic/pull/3132 diff --git a/cmd/restic/cmd_init.go b/cmd/restic/cmd_init.go index 2e293e040..7df4a810c 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "context" "encoding/json" "strconv" @@ -51,12 +50,6 @@ func init() { } func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []string) error { - type JSONSuccess struct { - Status string `json:"status"` - ID string `json:"id"` - Repository string `json:"repository"` - } - var version uint if opts.RepositoryVersion == "latest" || opts.RepositoryVersion == "" { version = restic.MaxRepoVersion @@ -116,26 +109,17 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args [] Verbosef("irrecoverably lost.\n") } else { - status := JSONSuccess{ + status := initSuccess{ Status: "success", ID: s.Config().ID, Repository: location.StripPassword(gopts.Repo), } - Verbosef(toJSONString(status)) + return json.NewEncoder(gopts.stdout).Encode(status) } return nil } -func toJSONString(status interface{}) string { - buf := new(bytes.Buffer) - err := json.NewEncoder(buf).Encode(status) - if err != nil { - panic("ERROR: Could not encode JSON string") - } - return buf.String() -} - func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts GlobalOptions) (*chunker.Pol, error) { if opts.CopyChunkerParameters { otherGopts, _, err := fillSecondaryGlobalOpts(opts.secondaryRepoOptions, gopts, "secondary") @@ -157,3 +141,9 @@ func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts Glo } return nil, nil } + +type initSuccess struct { + Status string `json:"status"` // "success" + ID string `json:"id"` + Repository string `json:"repository"` +} From 364a396fd66da701e2f7a7433659c14cd136f155 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 21 Oct 2022 22:33:39 +0200 Subject: [PATCH 4/4] init: use standard name `message_type` to distinguish JSON messages --- cmd/restic/cmd_init.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/restic/cmd_init.go b/cmd/restic/cmd_init.go index 7df4a810c..2932870e8 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -110,9 +110,9 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args [] } else { status := initSuccess{ - Status: "success", - ID: s.Config().ID, - Repository: location.StripPassword(gopts.Repo), + MessageType: "initialized", + ID: s.Config().ID, + Repository: location.StripPassword(gopts.Repo), } return json.NewEncoder(gopts.stdout).Encode(status) } @@ -143,7 +143,7 @@ func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts Glo } type initSuccess struct { - Status string `json:"status"` // "success" - ID string `json:"id"` - Repository string `json:"repository"` + MessageType string `json:"message_type"` // "initialized" + ID string `json:"id"` + Repository string `json:"repository"` }