mirror of
https://github.com/octoleo/restic.git
synced 2024-12-22 19:08:55 +00:00
init: cleanup json print code
This commit is contained in:
parent
933c9af328
commit
9a9f559806
@ -1,7 +1,7 @@
|
|||||||
Enhancement: Enable support for json output on init command
|
Enhancement: Support json output for the `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
|
|
||||||
|
|
||||||
|
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/issues/3124
|
||||||
https://github.com/restic/restic/pull/3132
|
https://github.com/restic/restic/pull/3132
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -51,12 +50,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []string) error {
|
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
|
var version uint
|
||||||
if opts.RepositoryVersion == "latest" || opts.RepositoryVersion == "" {
|
if opts.RepositoryVersion == "latest" || opts.RepositoryVersion == "" {
|
||||||
version = restic.MaxRepoVersion
|
version = restic.MaxRepoVersion
|
||||||
@ -116,26 +109,17 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []
|
|||||||
Verbosef("irrecoverably lost.\n")
|
Verbosef("irrecoverably lost.\n")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
status := JSONSuccess{
|
status := initSuccess{
|
||||||
Status: "success",
|
Status: "success",
|
||||||
ID: s.Config().ID,
|
ID: s.Config().ID,
|
||||||
Repository: location.StripPassword(gopts.Repo),
|
Repository: location.StripPassword(gopts.Repo),
|
||||||
}
|
}
|
||||||
Verbosef(toJSONString(status))
|
return json.NewEncoder(gopts.stdout).Encode(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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) {
|
func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts GlobalOptions) (*chunker.Pol, error) {
|
||||||
if opts.CopyChunkerParameters {
|
if opts.CopyChunkerParameters {
|
||||||
otherGopts, _, err := fillSecondaryGlobalOpts(opts.secondaryRepoOptions, gopts, "secondary")
|
otherGopts, _, err := fillSecondaryGlobalOpts(opts.secondaryRepoOptions, gopts, "secondary")
|
||||||
@ -157,3 +141,9 @@ func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts Glo
|
|||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type initSuccess struct {
|
||||||
|
Status string `json:"status"` // "success"
|
||||||
|
ID string `json:"id"`
|
||||||
|
Repository string `json:"repository"`
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user