Merge pull request #3132 from metalsp0rk/init-json

Init command JSON output
This commit is contained in:
Michael Eischer 2022-12-02 21:49:22 +01:00 committed by GitHub
commit 0af89a5738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -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

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"strconv" "strconv"
"github.com/restic/chunker" "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) 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)) if !gopts.JSON {
Verbosef("\n") Verbosef("created restic repository %v at %s\n", s.Config().ID[:10], location.StripPassword(gopts.Repo))
Verbosef("Please note that knowledge of your password is required to access\n") Verbosef("\n")
Verbosef("the repository. Losing your password means that your data is\n") Verbosef("Please note that knowledge of your password is required to access\n")
Verbosef("irrecoverably lost.\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 return nil
} }
@ -130,3 +141,9 @@ func maybeReadChunkerPolynomial(ctx context.Context, opts InitOptions, gopts Glo
} }
return nil, nil return nil, nil
} }
type initSuccess struct {
MessageType string `json:"message_type"` // "initialized"
ID string `json:"id"`
Repository string `json:"repository"`
}