Repository: cli/cli
File: pkg/cmd/extension/manager.go (Go)

Code before:
		return m.upgradeExtensions(exts, force)
	}
	for _, f := range exts {
		if f.Name() != name {
			continue
		}
		if f.IsLocal() {
			return localExtensionUpgradeError
		}
		// For single extensions manually retrieve latest version since we forgo doing it during list.
		if latestVersion := f.LatestVersion(); latestVersion == "" {
			return fmt.Errorf("unable to retrieve latest version for extension %q", name)
		}
		return m.upgradeExtensions([]*Extension{f}, force)
	}
	return fmt.Errorf("no extension matched %q", name)
}

func (m *Manager) upgradeExtensions(exts []*Extension, force bool) error {
	var failed bool
	for _, f := range exts {
		fmt.Fprintf(m.io.Out, "[%s]: ", f.Name())
		currentVersion := displayExtensionVersion(f, f.CurrentVersion())
		err := m.upgradeExtension(f, force)
		if err != nil {
			if !errors.Is(err, localExtensionUpgradeError) &&
				!errors.Is(err, upToDateError) &&
				!errors.Is(err, pinnedExtensionUpgradeError) {
				failed = true
			}
			fmt.Fprintf(m.io.Out, "%s\n", err)
			continue
		}
		latestVersion := displayExtensionVersion(f, f.LatestVersion())
		if m.dryRunMode {
			fmt.Fprintf(m.io.Out, "would have upgraded from %s to %s\n", currentVersion, latestVersion)
		} else {
			fmt.Fprintf(m.io.Out, "upgraded from %s to %s\n", currentVersion, latestVersion)
		}
	}
	if failed {
		return errors.New("some extensions failed to upgrade")
	}
	return nil
}

func (m *Manager) upgradeExtension(ext *Extension, force bool) error {
	if ext.IsLocal() {
		return localExtensionUpgradeError
	}
	if !force && ext.IsPinned() {

Review comment (suggestion):
Could also seeing this use [`slices.Max`](https://pkg.go.dev/slices#Max) or [`slices.MaxFunc`](https://pkg.go.dev/slices#MaxFunc) 🤔 

Code after:
		return m.upgradeExtensions(exts, force)
	}
	for _, f := range exts {
		if f.Name() != name {
			continue
		}
		if f.IsLocal() {
			return localExtensionUpgradeError
		}
		// For single extensions manually retrieve latest version since we forgo doing it during list.
		if latestVersion := f.LatestVersion(); latestVersion == "" {
			return fmt.Errorf("unable to retrieve latest version for extension %q", name)
		}
		return m.upgradeExtensions([]*Extension{f}, force)
	}
	return fmt.Errorf("no extension matched %q", name)
}

func (m *Manager) upgradeExtensions(exts []*Extension, force bool) error {
	var longestExtName = 0
	for _, ext := range exts {
		l := len(ext.Name())
		if len(ext.Name()) > longestExtName {
			longestExtName = l
		}
	}
	format := fmt.Sprintf("[%%%ds]: ", longestExtName)

	var failed bool
	for _, f := range exts {
		fmt.Fprintf(m.io.Out, format, f.Name())
		currentVersion := displayExtensionVersion(f, f.CurrentVersion())
		err := m.upgradeExtension(f, force)
		if err != nil {
			if !errors.Is(err, localExtensionUpgradeError) &&
				!errors.Is(err, upToDateError) &&
				!errors.Is(err, pinnedExtensionUpgradeError) {
				failed = true
			}
			fmt.Fprintf(m.io.Out, "%s\n", err)
			continue
		}
		latestVersion := displayExtensionVersion(f, f.LatestVersion())
		if m.dryRunMode {
			fmt.Fprintf(m.io.Out, "would have upgraded from %s to %s\n", currentVersion, latestVersion)
		} else {
			fmt.Fprintf(m.io.Out, "upgraded from %s to %s\n", currentVersion, latestVersion)
		}
	}
	if failed {
		return errors.New("some extensions failed to upgrade")