Commit 24658d3e authored by ale's avatar ale

Add ability to restore legacy (v2) backups

parent 157dad0a
Pipeline #5948 passed with stages
in 5 minutes and 45 seconds
......@@ -585,8 +585,54 @@ func (c *backupCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...inter
return subcommands.ExitSuccess
}
// Legacy EncodingParams type from autoradio v2.
type legacyEncodingParams struct {
SourceName string
Format string
BitRate int32
SampleRate int32
Channels int32
StereoMode string
Quality float32
}
// Legacy Mount type from autoradio v2.
type legacyMount struct {
Name string
Username string
Password string
RelayUrl string
Fallback string
Transcoding *legacyEncodingParams
}
func (m *legacyMount) toProto() *pb.Mount {
out := &pb.Mount{
Path: m.Name,
SourceUsername: m.Username,
SourcePassword: m.Password,
RelayUrl: m.RelayUrl,
FallbackPath: m.Fallback,
}
if m.Transcoding != nil {
out.Transcode = true
out.TranscodeParams = &pb.EncodingParams{
SourcePath: m.Transcoding.SourceName,
Format: m.Transcoding.Format,
BitRate: m.Transcoding.BitRate,
SampleRate: m.Transcoding.SampleRate,
Channels: m.Transcoding.Channels,
StereoMode: m.Transcoding.StereoMode,
Quality: m.Transcoding.Quality,
}
}
return out
}
// Restore mount configuration.
type restoreCommand struct{}
type restoreCommand struct {
v2compat bool
}
func (c *restoreCommand) Name() string { return "restore" }
func (c *restoreCommand) Synopsis() string { return "Restore mount configuration" }
......@@ -597,7 +643,9 @@ Read a configuration dump from standard input and restore it.
`
}
func (c *restoreCommand) SetFlags(_ *flag.FlagSet) {}
func (c *restoreCommand) SetFlags(f *flag.FlagSet) {
f.BoolVar(&c.v2compat, "v2-compat", false, "restore an autoradio v2 backup")
}
func (c *restoreCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if f.NArg() > 0 {
......@@ -605,10 +653,22 @@ func (c *restoreCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...inte
return subcommands.ExitUsageError
}
decoder := json.NewDecoder(os.Stdin)
var mounts []*pb.Mount
if err := json.NewDecoder(os.Stdin).Decode(&mounts); err != nil {
log.Printf("ERROR: %v", err)
return subcommands.ExitFailure
if c.v2compat {
var legacyMounts []legacyMount
if err := decoder.Decode(&legacyMounts); err != nil {
log.Printf("ERROR: %v", err)
return subcommands.ExitFailure
}
for _, m := range legacyMounts {
mounts = append(mounts, m.toProto())
}
} else {
if err := decoder.Decode(&mounts); err != nil {
log.Printf("ERROR: %v", err)
return subcommands.ExitFailure
}
}
client := getClient()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment