Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init command JSON output #3132

Merged
merged 4 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions 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
metalsp0rk marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/restic/restic/pull/3132
27 changes: 22 additions & 5 deletions cmd/restic/cmd_init.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"strconv"

"github.com/restic/chunker"
Expand Down Expand Up @@ -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
}
Expand All @@ -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"`
}