package binarymgr import ( "context" "errors" "fmt" "os" "time" "github.com/hashicorp/go-version" gitlab "gitlab.com/gitlab-org/api/client-go/v3" "gitlab.com/gitlab-org/cli/internal/config" "gitlab.com/gitlab-org/cli/internal/iostreams" ) // Executor runs the resolved binary with the given args. The binarymgr // package leaves this to consumers because the right strategy is // platform-specific (syscall.Exec on Unix, subprocess on Windows) and may // also need to inject distribution-specific environment variables. type Executor func(ctx context.Context, binaryPath string, args []string) error // Runner orchestrates the per-invocation lifecycle: ensure the binary is // installed, prompt for run consent, save metadata, schedule a background // update check, then hand off to Executor. type Runner struct { IO *iostreams.IOStreams Cfg config.Config Spec Spec Manager *Manager Executor Executor // Update / Install / Yes mirror the glab-owned flags after the // caller has parsed them out of the cobra args. Args holds the // remaining pass-through args destined for the managed binary. Update bool Install bool Yes bool Args []string } // updateCheckResult is the outcome of an update check. type updateCheckResult struct { hasUpdate bool currentVersion string latestVersion string newMajorVersion string } // Run executes the default flow: ensure installed, prompt for consent (when // needed), and exec the binary with Args. func (r *Runner) Run(ctx context.Context) error { managedPath, err := ManagedBinaryPath(r.Spec) if err != nil { return err } installedPath, _ := r.Cfg.Get("", r.Spec.configKey("binary_path")) if installedPath != "" && installedPath != managedPath && r.Update { color := r.IO.Color() r.IO.LogInfof("%s Updates are not applicable when using a custom binary path (%s).\n", color.DotWarnIcon(), installedPath) return nil } if r.Update { return r.HandleUpdate(ctx) } installedVersion, _ := r.Cfg.Get("", r.Spec.configKey("binary_version")) installedVersion = r.versionAfterFloor(installedVersion, installedPath, managedPath) autoDownload, _ := r.Cfg.Get("", r.Spec.configKey("auto_download")) if r.Yes { autoDownload = "true" } result, err := r.Manager.EnsureInstalled(ctx, installedVersion, installedPath, autoDownload) if err != nil { return err } info := result.Info if info.Path == managedPath { r.saveAutoDownloadPreference(result.AutoDownloadPreference) if result.Downloaded { r.saveLastUpdateCheck(time.Now()) } if err := r.SaveBinaryInfo(info); err != nil { r.warnf("Failed to save binary metadata: %v", err) } r.checkForUpdates(ctx) } else { color := r.IO.Color() r.IO.LogErrorf("%s Using custom %s binary: %s\n", color.DotWarnIcon(), r.Spec.DisplayName, info.Path) } if !r.Yes { if err := r.checkAutoRun(ctx); err != nil { return err } } return r.Executor(ctx, info.Path, r.Args) } // HandleInstall installs (or refreshes) the managed binary without running // it. If the user has configured a custom path, the path is reported and // nothing is downloaded. func (r *Runner) HandleInstall(ctx context.Context) error { managedPath, err := ManagedBinaryPath(r.Spec) if err != nil { return err } installedVersion, _ := r.Cfg.Get("", r.Spec.configKey("binary_version")) installedPath, _ := r.Cfg.Get("", r.Spec.configKey("binary_path")) installedVersion = r.versionAfterFloor(installedVersion, installedPath, managedPath) autoDownload, _ := r.Cfg.Get("", r.Spec.configKey("auto_download")) if r.Yes { autoDownload = "true" } result, err := r.Manager.EnsureInstalled(ctx, installedVersion, installedPath, autoDownload) if err != nil { return err } info := result.Info if info.Path != managedPath { color := r.IO.Color() r.IO.LogInfof("%s Using custom %s binary: %s\n", color.DotWarnIcon(), r.Spec.DisplayName, info.Path) return nil } if installedVersion != "" && info.Version == installedVersion { color := r.IO.Color() r.IO.LogInfof("%s %s version %s is already installed.\n", color.GreenCheck(), r.Spec.DisplayName, installedVersion) return nil } r.saveAutoDownloadPreference(result.AutoDownloadPreference) r.saveLastUpdateCheck(time.Now()) return r.SaveBinaryInfo(info) } // HandleUpdate forces an update check and applies any newer compatible // version. If nothing is installed yet, it falls back to a fresh install. func (r *Runner) HandleUpdate(ctx context.Context) error { result, err := r.performUpdateCheck(ctx, true) if err != nil { return fmt.Errorf("failed to check for updates: %w", err) } if result == nil { r.IO.LogError("No version installed, downloading latest...") installedVersion, _ := r.Cfg.Get("", r.Spec.configKey("binary_version")) installedPath, _ := r.Cfg.Get("", r.Spec.configKey("binary_path")) autoDownload, _ := r.Cfg.Get("", r.Spec.configKey("auto_download")) if r.Yes { autoDownload = "true" } result, err := r.Manager.EnsureInstalled(ctx, installedVersion, installedPath, autoDownload) if err != nil { return err } r.saveAutoDownloadPreference(result.AutoDownloadPreference) r.saveLastUpdateCheck(time.Now()) return r.SaveBinaryInfo(result.Info) } if !result.hasUpdate { color := r.IO.Color() r.IO.LogInfof("%s You are already using the latest compatible version (%s)\n", color.GreenCheck(), result.currentVersion) if result.newMajorVersion != "" { r.IO.LogInfof("%s %s %s is available but requires a newer version of glab.\n", color.DotWarnIcon(), r.Spec.DisplayName, result.newMajorVersion) r.IO.LogInfof("Run 'glab check-update' to check for the latest glab version.\n") } return nil } r.IO.LogInfof("Update available: %s → %s\n", result.currentVersion, result.latestVersion) info, err := r.Manager.Update(ctx) if err != nil { return fmt.Errorf("failed to update: %w", err) } if err := r.SaveBinaryInfo(info); err != nil { r.warnf("Failed to save binary metadata: %v", err) } color := r.IO.Color() r.IO.LogInfof("%s Successfully updated to version %s\n", color.GreenCheck(), result.latestVersion) return nil } // SaveBinaryInfo writes path/version/checksum to config and persists. func (r *Runner) SaveBinaryInfo(info *BinaryInfo) error { if err := r.Cfg.Set("", r.Spec.configKey("binary_path"), info.Path); err != nil { return err } if err := r.Cfg.Set("", r.Spec.configKey("binary_version"), info.Version); err != nil { return err } if err := r.Cfg.Set("", r.Spec.configKey("binary_checksum"), info.Checksum); err != nil { return err } return r.Cfg.Write() } // ShouldForceUpdateCheck returns true when the spec's force-update env var // is "true". Useful for tests and CI that want to bypass the 24h cache. func (r *Runner) ShouldForceUpdateCheck() bool { return os.Getenv(r.Spec.envVar("CHECK_UPDATE")) == "true" } // InstallStatus describes the binary Run would exec without a download. type InstallStatus struct { Path string Version string Installed bool } // InstalledBinary resolves the binary the way Run does, but never prompts, downloads, or logs. // Config read errors are returned together with a best-effort status so the caller picks the fallback. func InstalledBinary(cfg config.Config, spec Spec) (InstallStatus, error) { var errs []error read := func(suffix string) string { value, err := cfg.Get("", spec.configKey(suffix)) if err != nil { errs = append(errs, fmt.Errorf("reading %s: %w", spec.configKey(suffix), err)) } return value } path := read("binary_path") version := read("binary_version") managedPath, managedErr := ManagedBinaryPath(spec) status := InstallStatus{Path: path, Version: version} if path != "" && path != managedPath { status.Installed = validateBinaryPath(path, spec) == nil return status, errors.Join(errs...) } status.Path = managedPath status.Installed = version != "" && !spec.belowFloor(version) && isBinaryValid(managedPath) return status, errors.Join(append(errs, managedErr)...) } func (s Spec) belowFloor(installedVersion string) bool { if s.MinVersion == "" || installedVersion == "" { return false } floor, err := version.NewVersion(s.MinVersion) if err != nil { return false } installed, err := version.NewVersion(installedVersion) return err == nil && installed.LessThan(floor) } func (r *Runner) versionAfterFloor(installedVersion, installedPath, managedPath string) string { if installedPath != "" && installedPath != managedPath { return installedVersion } if !r.Spec.belowFloor(installedVersion) { return installedVersion } color := r.IO.Color() r.IO.LogErrorf("%s Installed %s version %s is older than the required %s\n", color.DotWarnIcon(), r.Spec.DisplayName, installedVersion, r.Spec.MinVersion) return "" } func (r *Runner) performUpdateCheck(ctx context.Context, forceCheck bool) (*updateCheckResult, error) { currentVersion, _ := r.Cfg.Get("", r.Spec.configKey("binary_version")) if currentVersion == "" { return nil, nil } lastCheckStr, _ := r.Cfg.Get("", r.Spec.configKey("last_update_check")) var lastCheckTime time.Time if lastCheckStr != "" { lastCheckTime, _ = time.Parse(time.RFC3339, lastCheckStr) } check, err := r.Manager.CheckForUpdate(ctx, currentVersion, lastCheckTime, forceCheck) // A failed check is stamped too, so an unreachable registry costs one timeout per day, not one per run. if !check.NewCheckTime.IsZero() { if err := r.Cfg.Set("", r.Spec.configKey("last_update_check"), check.NewCheckTime.Format(time.RFC3339)); err != nil { r.warnf("Failed to save update check time: %v", err) } if err := r.Cfg.Write(); err != nil { r.warnf("Failed to write config: %v", err) } } if err != nil { return nil, err } return &updateCheckResult{ hasUpdate: check.HasUpdate, currentVersion: currentVersion, latestVersion: check.LatestVersion, newMajorVersion: check.NewMajorVersion, }, nil } func (r *Runner) checkAutoRun(ctx context.Context) error { autoRun, _ := r.Cfg.Get("", r.Spec.configKey("auto_run")) if autoRun == "true" { return nil } if !r.IO.CanPrompt() { return nil } confirm := true if err := r.IO.Confirm(ctx, &confirm, fmt.Sprintf("Run the %s?", r.Spec.DisplayName)); err != nil { return err } if !confirm { return errors.New("execution cancelled") } var always bool if err := r.IO.Confirm(ctx, &always, "Always run without prompting?"); err != nil { r.warnf("Failed to get auto-run preference: %v", err) return nil } if always { if err := r.Cfg.Set("", r.Spec.configKey("auto_run"), "true"); err != nil { r.warnf("Failed to save preference: %v", err) } if err := r.Cfg.Write(); err != nil { r.warnf("Failed to write config: %v", err) } } return nil } // ReportUpdate prints the update notice for the managed binary without the // 24-hour throttle. It stays silent on an unsupported platform and for a // binary that is not installed or is custom, since glab updates none of them. // Unlike InstalledBinary, it reports a binary below Spec.MinVersion, since // that user needs the update most. func (r *Runner) ReportUpdate(ctx context.Context) error { managedPath, err := ManagedBinaryPath(r.Spec) if errors.Is(err, ErrUnsupportedPlatform) { return nil } if err != nil { return fmt.Errorf("failed checking for %s updates: %w", r.Spec.DisplayName, err) } installedPath, _ := r.Cfg.Get("", r.Spec.configKey("binary_path")) installedVersion, _ := r.Cfg.Get("", r.Spec.configKey("binary_version")) if installedPath != "" && installedPath != managedPath { return nil } if installedVersion == "" || !isBinaryValid(managedPath) { return nil } result, err := r.performUpdateCheck(ctx, true) if err != nil { // An outage answers with an HTML page that the error carries in full. if errResp, ok := errors.AsType[*gitlab.ErrorResponse](err); ok { return fmt.Errorf("failed checking for %s updates: the package registry responded with HTTP %d", r.Spec.DisplayName, errResp.StatusCode) } return fmt.Errorf("failed checking for %s updates: %w", r.Spec.DisplayName, err) } r.printUpdateNotice(result) return nil } func (r *Runner) checkForUpdates(ctx context.Context) { result, err := r.performUpdateCheck(ctx, r.ShouldForceUpdateCheck()) if err != nil || result == nil { return } r.printUpdateNotice(result) } func (r *Runner) printUpdateNotice(result *updateCheckResult) { color := r.IO.Color() if result.hasUpdate { r.IO.LogErrorf("\n%s New %s version available: %s → %s\n", color.DotWarnIcon(), r.Spec.DisplayName, result.currentVersion, result.latestVersion) r.IO.LogErrorf("Run 'glab %s update' to update to the latest version\n", r.Spec.Command) } if result.newMajorVersion != "" { r.IO.LogErrorf("\n%s %s %s is available but requires a newer version of glab.\n", color.DotWarnIcon(), r.Spec.DisplayName, result.newMajorVersion) r.IO.LogErrorf("Run 'glab check-update' to check for the latest glab version.\n") } } // saveAutoDownloadPreference persists the user's "always download updates" // choice. EnsureInstalled returns "true" when the user opted in during the // follow-up prompt and "" otherwise; we only write on opt-in so this is a // no-op for the common case. func (r *Runner) saveAutoDownloadPreference(pref string) { if pref == "" { return } if err := r.Cfg.Set("", r.Spec.configKey("auto_download"), pref); err != nil { r.warnf("Failed to save preference: %v", err) return } if err := r.Cfg.Write(); err != nil { r.warnf("Failed to write config: %v", err) } } // saveLastUpdateCheck stamps the moment a known-current install was placed // on disk so the next interactive run skips the redundant registry hit. func (r *Runner) saveLastUpdateCheck(t time.Time) { if err := r.Cfg.Set("", r.Spec.configKey("last_update_check"), t.Format(time.RFC3339)); err != nil { r.warnf("Failed to save update check time: %v", err) return } if err := r.Cfg.Write(); err != nil { r.warnf("Failed to write config: %v", err) } } func (r *Runner) warnf(format string, args ...any) { color := r.IO.Color() r.IO.LogErrorf("%s "+format+"\n", append([]any{color.DotWarnIcon()}, args...)...) }