diff --git a/cmd/restic/cmd_self_update.go b/cmd/restic/cmd_self_update.go index 00d91b294..c7a29ac86 100644 --- a/cmd/restic/cmd_self_update.go +++ b/cmd/restic/cmd_self_update.go @@ -1,4 +1,4 @@ -// +build selfupdate +// xbuild selfupdate package main @@ -36,10 +36,30 @@ func init() { cmdRoot.AddCommand(cmdSelfUpdate) flags := cmdSelfUpdate.Flags() - flags.StringVar(&selfUpdateOptions.Output, "output", os.Args[0], "Save the downloaded file as `filename`") + flags.StringVar(&selfUpdateOptions.Output, "output", "", "Save the downloaded file as `filename` (default: running binary itself)") } func runSelfUpdate(opts SelfUpdateOptions, gopts GlobalOptions, args []string) error { + 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 { + return err + } + + if !fi.Mode().IsRegular() { + return errors.Errorf("output file %v is not a normal file, use --output to specify a different file", opts.Output) + } + + Printf("writing restic to %v\n", opts.Output) + v, err := selfupdate.DownloadLatestStableRelease(gopts.ctx, opts.Output, Verbosef) if err != nil { return errors.Fatalf("unable to update restic: %v", err)