forked from TrueCloudLab/restic
This commit adds a command called `self-update` which downloads the latest released version of restic from GitHub and replacing the current binary with it. It does not rely on any external program (so it'll work everywhere), but still verifies the GPG signature using the embedded GPG public key. By default, the `self-update` command is hidden behind the `selfupdate` built tag, which is only set when restic is built using `build.go`. The reason for this is that downstream distributions will then not include the command by default, so users are encouraged to use the platform-specific distribution mechanism.
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// +build selfupdate
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
"github.com/restic/restic/internal/selfupdate"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var cmdSelfUpdate = &cobra.Command{
|
|
Use: "self-update [flags]",
|
|
Short: "Update the restic binary",
|
|
Long: `
|
|
The command "update-restic" downloads the latest stable release of restic from
|
|
GitHub and replaces the currently running binary. After download, the
|
|
authenticity of the binary is verified using the GPG signature on the release
|
|
files.
|
|
`,
|
|
DisableAutoGenTag: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return runSelfUpdate(selfUpdateOptions, globalOptions, args)
|
|
},
|
|
}
|
|
|
|
// SelfUpdateOptions collects all options for the update-restic command.
|
|
type SelfUpdateOptions struct {
|
|
Output string
|
|
}
|
|
|
|
var selfUpdateOptions SelfUpdateOptions
|
|
|
|
func init() {
|
|
cmdRoot.AddCommand(cmdSelfUpdate)
|
|
|
|
flags := cmdSelfUpdate.Flags()
|
|
flags.StringVar(&selfUpdateOptions.Output, "output", os.Args[0], "Save the downloaded file as `filename`")
|
|
}
|
|
|
|
func runSelfUpdate(opts SelfUpdateOptions, gopts GlobalOptions, args []string) error {
|
|
v, err := selfupdate.DownloadLatestStableRelease(gopts.ctx, opts.Output, Verbosef)
|
|
if err != nil {
|
|
return errors.Fatalf("unable to update restic: %v", err)
|
|
}
|
|
|
|
Printf("successfully updated restic to version %v\n", v)
|
|
|
|
return nil
|
|
}
|