diff --git a/internal/cli/app.go b/internal/cli/app.go index 7518321..c0ca452 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -938,6 +938,7 @@ func (a *App) CLI() *CLI { Short: "", Long: "update", Args: "", + IsHidden: core.NoSelfUpdate, Description: "Update the fetch binary in place", Default: "", IsSet: func() bool { diff --git a/internal/core/vars.go b/internal/core/vars.go index 8946a9d..6e08153 100644 --- a/internal/core/vars.go +++ b/internal/core/vars.go @@ -14,9 +14,13 @@ type TerminalSize struct { HeightPx int // Height in pixels (0 if unavailable) } +var packageManager string // set via ldflags to disable self-update (e.g. "Homebrew") + var ( - IsStderrTerm bool - IsStdoutTerm bool + IsStderrTerm bool + IsStdoutTerm bool + NoSelfUpdate bool + PackageManager string UserAgent string Version string @@ -29,6 +33,10 @@ func init() { IsStderrTerm = isTerminal(int(os.Stderr.Fd())) IsStdoutTerm = isTerminal(int(os.Stdout.Fd())) + // Set whether self-update is disabled. + PackageManager = packageManager + NoSelfUpdate = PackageManager != "" + // Set executable version and user-agent. Version = getVersion() UserAgent = "fetch/" + Version diff --git a/main.go b/main.go index d681efc..98362ed 100644 --- a/main.go +++ b/main.go @@ -60,7 +60,7 @@ func main() { } // Start async update, if necessary. - if !app.Update && app.Cfg.AutoUpdate != nil && *app.Cfg.AutoUpdate >= 0 { + if !app.Update && !core.NoSelfUpdate && app.Cfg.AutoUpdate != nil && *app.Cfg.AutoUpdate >= 0 { checkForUpdate(ctx, handle.Stderr(), *app.Cfg.AutoUpdate) } @@ -94,6 +94,11 @@ func main() { // Attempt to update the current executable. verbosity := getVerbosity(app) if app.Update { + if core.NoSelfUpdate { + p := handle.Stderr() + core.WriteErrorMsg(p, errSelfUpdateDisabled(core.PackageManager)) + os.Exit(1) + } p := handle.Stderr() timeout := getValue(app.Cfg.Timeout) status := update.Update(ctx, p, timeout, verbosity == core.VSilent, app.DryRun) @@ -289,6 +294,19 @@ func writeCLIErr(p *core.Printer, err error) { p.Flush() } +type errSelfUpdateDisabled string + +func (err errSelfUpdateDisabled) Error() string { + return "self-update is disabled for this installation; please update using " + string(err) +} + +func (err errSelfUpdateDisabled) PrintTo(p *core.Printer) { + p.WriteString("self-update is disabled for this installation; please update using ") + p.Set(core.Bold) + p.WriteString(string(err)) + p.Reset() +} + type errShellNotSupported string func (err errShellNotSupported) Error() string {