diff --git a/changelog/unreleased/issue-3124 b/changelog/unreleased/issue-3124 new file mode 100644 index 000000000..25d754eed --- /dev/null +++ b/changelog/unreleased/issue-3124 @@ -0,0 +1,7 @@ +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 f833369ef..2932870e8 100644 --- a/cmd/restic/cmd_init.go +++ b/cmd/restic/cmd_init.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "strconv" "github.com/restic/chunker" @@ -100,11 +101,21 @@ 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 := initSuccess{ + MessageType: "initialized", + ID: s.Config().ID, + Repository: location.StripPassword(gopts.Repo), + } + return json.NewEncoder(gopts.stdout).Encode(status) + } return nil } @@ -130,3 +141,9 @@ func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts Glo } return nil, nil } + +type initSuccess struct { + MessageType string `json:"message_type"` // "initialized" + ID string `json:"id"` + Repository string `json:"repository"` +}