2022-12-25 22:01:48 +00:00
|
|
|
//go:build selfupdate
|
2018-08-12 15:10:04 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-31 22:08:13 +00:00
|
|
|
"context"
|
2018-08-12 15:10:04 +00:00
|
|
|
"os"
|
2020-09-20 10:04:08 +00:00
|
|
|
"path/filepath"
|
2018-08-12 15:10:04 +00:00
|
|
|
|
|
|
|
"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: `
|
2018-10-18 22:45:31 +00:00
|
|
|
The command "self-update" downloads the latest stable release of restic from
|
2018-08-12 15:10:04 +00:00
|
|
|
GitHub and replaces the currently running binary. After download, the
|
|
|
|
authenticity of the binary is verified using the GPG signature on the release
|
|
|
|
files.
|
2019-11-05 06:03:38 +00:00
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
2018-08-12 15:10:04 +00:00
|
|
|
`,
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-10-02 21:24:37 +00:00
|
|
|
return runSelfUpdate(cmd.Context(), selfUpdateOptions, globalOptions, args)
|
2018-08-12 15:10:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2018-10-14 15:29:19 +00:00
|
|
|
flags.StringVar(&selfUpdateOptions.Output, "output", "", "Save the downloaded file as `filename` (default: running binary itself)")
|
2018-08-12 15:10:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
func runSelfUpdate(ctx context.Context, opts SelfUpdateOptions, gopts GlobalOptions, args []string) error {
|
2018-10-14 15:29:19 +00:00
|
|
|
if opts.Output == "" {
|
|
|
|
file, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "unable to find executable")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Output = file
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Lstat(opts.Output)
|
|
|
|
if err != nil {
|
2020-09-20 10:04:08 +00:00
|
|
|
dirname := filepath.Dir(opts.Output)
|
|
|
|
di, err := os.Lstat(dirname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !di.Mode().IsDir() {
|
|
|
|
return errors.Fatalf("output parent path %v is not a directory, use --output to specify a different file path", dirname)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !fi.Mode().IsRegular() {
|
|
|
|
return errors.Fatalf("output path %v is not a normal file, use --output to specify a different file path", opts.Output)
|
|
|
|
}
|
2018-10-14 15:29:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 20:24:14 +00:00
|
|
|
Verbosef("writing restic to %v\n", opts.Output)
|
2018-10-14 15:29:19 +00:00
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
v, err := selfupdate.DownloadLatestStableRelease(ctx, opts.Output, version, Verbosef)
|
2018-08-12 15:10:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Fatalf("unable to update restic: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-14 15:29:46 +00:00
|
|
|
if v != version {
|
|
|
|
Printf("successfully updated restic to version %v\n", v)
|
|
|
|
}
|
2018-08-12 15:10:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|