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

Code before:
type extensionReleaseInfo struct {
	CurrentVersion string
	LatestVersion  string
	Pinned         bool
	URL            string
}

func NewCmdExtension(io *iostreams.IOStreams, em extensions.ExtensionManager, ext extensions.Extension) *cobra.Command {
	// Setup channel containing information about potential latest release info
	updateMessageChan := make(chan *extensionReleaseInfo)

	return &cobra.Command{
		Use:   ext.Name(),
		Short: fmt.Sprintf("Extension %s", ext.Name()),
		// PreRun handles looking up whether extension has a latest version only when the command is ran.
		PreRun: func(c *cobra.Command, args []string) {
			go func() {
				if ext.UpdateAvailable() {
					updateMessageChan <- &extensionReleaseInfo{
						CurrentVersion: ext.CurrentVersion(),
						LatestVersion:  ext.LatestVersion(),
						Pinned:         ext.IsPinned(),
						URL:            ext.URL(),
					}
				}
			}()
		},
		RunE: func(c *cobra.Command, args []string) error {
			args = append([]string{ext.Name()}, args...)
			if _, err := em.Dispatch(args, io.In, io.Out, io.ErrOut); err != nil {
				var execError *exec.ExitError
				if errors.As(err, &execError) {
					return &ExternalCommandExitError{execError}
				}
				return fmt.Errorf("failed to run extension: %w\n", err)
			}
			return nil
		},
		// PostRun handles communicating extension release information if found
		PostRun: func(c *cobra.Command, args []string) {
			releaseInfo := <-updateMessageChan
			if releaseInfo != nil {
				stderr := io.ErrOut
				fmt.Fprintf(stderr, "\n\n%s %s → %s\n",
					ansi.Color(fmt.Sprintf("A new release of %s is available:", ext.Name()), "yellow"),
					ansi.Color(strings.TrimPrefix(releaseInfo.CurrentVersion, "v"), "cyan"),
					ansi.Color(strings.TrimPrefix(releaseInfo.LatestVersion, "v"), "cyan"))
				if releaseInfo.Pinned {
					fmt.Fprintf(stderr, "To upgrade, run: gh extension upgrade %s --force\n", ext.Name())
				} else {
					fmt.Fprintf(stderr, "To upgrade, run: gh extension upgrade %s\n", ext.Name())

Review comment (refactor):
🤔 Something about this being an IIFE (instantly invoked functional expression) is giving me pause... That said, I'm not sure I've ever seen a goroutine created with a named function. Is that the convention?

Either way, I know you're interested in testing, and if this IIFE was extracted to a named function with its dependencies injected (`updateMessageChan`, `extensionReleaseInfo`, and `ext`) testing it would be pretty simple. It's a lot harder to test it here, as written. 

Code after:
type extensionReleaseInfo struct {
	CurrentVersion string
	LatestVersion  string
	Pinned         bool
	URL            string
}

func NewCmdExtension(io *iostreams.IOStreams, em extensions.ExtensionManager, ext extensions.Extension) *cobra.Command {
	updateMessageChan := make(chan *extensionReleaseInfo)
	cs := io.ColorScheme()

	return &cobra.Command{
		Use:   ext.Name(),
		Short: fmt.Sprintf("Extension %s", ext.Name()),
		// PreRun handles looking up whether extension has a latest version only when the command is ran.
		PreRun: func(c *cobra.Command, args []string) {
			go func() {
				if ext.UpdateAvailable() {
					updateMessageChan <- &extensionReleaseInfo{
						CurrentVersion: ext.CurrentVersion(),
						LatestVersion:  ext.LatestVersion(),
						Pinned:         ext.IsPinned(),
						URL:            ext.URL(),
					}
				}
			}()
		},
		RunE: func(c *cobra.Command, args []string) error {
			args = append([]string{ext.Name()}, args...)
			if _, err := em.Dispatch(args, io.In, io.Out, io.ErrOut); err != nil {
				var execError *exec.ExitError
				if errors.As(err, &execError) {
					return &ExternalCommandExitError{execError}
				}
				return fmt.Errorf("failed to run extension: %w\n", err)
			}
			return nil
		},
		// PostRun handles communicating extension release information if found
		PostRun: func(c *cobra.Command, args []string) {
			select {
			case releaseInfo := <-updateMessageChan:
				if releaseInfo != nil {
					stderr := io.ErrOut
					fmt.Fprintf(stderr, "\n\n%s %s → %s\n",
						cs.Yellowf("A new release of %s is available:", ext.Name()),
						cs.Cyan(strings.TrimPrefix(releaseInfo.CurrentVersion, "v")),
						cs.Cyan(strings.TrimPrefix(releaseInfo.LatestVersion, "v")))
					if releaseInfo.Pinned {
						fmt.Fprintf(stderr, "To upgrade, run: gh extension upgrade %s --force\n", ext.Name())
					} else {