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

Code before:
		}(),
	)

	return &extCmd
}

func checkValidLocalExtension(rootCmd *cobra.Command, m extensions.ExtensionManager, extDir string) error {
	extName := filepath.Base(extDir)
	if !strings.HasPrefix(extName, "gh-") {
		return errors.New("extension directory name must start with `gh-`")
	}

	commandName := strings.TrimPrefix(extName, "gh-")
	if c, _, _ := rootCmd.Find([]string{commandName}); c != rootCmd && c.GroupID != "extension" {
		return fmt.Errorf("%q matches the name of a built-in command or alias", commandName)
	}

	for _, ext := range m.List() {
		if ext.Name() == commandName {
			return fmt.Errorf("there is already an installed extension that provides the %q command", commandName)
		}
	}

	return nil
}

func checkValidExtension(rootCmd *cobra.Command, m extensions.ExtensionManager, extName, extOwner string) (extensions.Extension, error) {
	if !strings.HasPrefix(extName, "gh-") {
		return nil, errors.New("extension repository name must start with `gh-`")
	}

	commandName := strings.TrimPrefix(extName, "gh-")
	if c, _, _ := rootCmd.Find([]string{commandName}); c != rootCmd && c.GroupID != "extension" {
		return nil, fmt.Errorf("%q matches the name of a built-in command or alias", commandName)
	}

	for _, ext := range m.List() {
		if ext.Name() == commandName {
			if ext.Owner() == extOwner {
				return ext, alreadyInstalledError
			}
			return ext, fmt.Errorf("there is already an installed extension that provides the %q command", commandName)
		}
	}

	return nil, nil
}

func normalizeExtensionSelector(n string) string {
	if idx := strings.IndexRune(n, '/'); idx >= 0 {
		n = n[idx+1:]

Review comment (suggestion):
My primary concern is whether we can avoid duplicating `checkValidExtension` logic. 🤔
Beyond turning `extDir` into `extName`, I assume the `extOwner` comparison was problem without a clear workaround:

https://github.com/cli/cli/blob/6923fb5cc88494febf2551946810c6c03cc83f82/pkg/cmd/extension/command.go#L677-L681

Since this is potentially just a directory on the user's workstation, it is possible it isn't even a GitHub repository or a git repository.

**Could we just call `checkValidExtension` passing `extName` based on the directory like above but leave `extOwner` as an empty string?**  Having an accurate owner doesn't really matter if an extension is already installed with that name.

Code after:
		}(),
	)

	return &extCmd
}

func checkValidExtension(rootCmd *cobra.Command, m extensions.ExtensionManager, extName, extOwner string) (extensions.Extension, error) {
	if !strings.HasPrefix(extName, "gh-") {
		return nil, errors.New("extension name must start with `gh-`")
	}

	commandName := strings.TrimPrefix(extName, "gh-")
	if c, _, _ := rootCmd.Find([]string{commandName}); c != rootCmd && c.GroupID != "extension" {
		return nil, fmt.Errorf("%q matches the name of a built-in command or alias", commandName)
	}

	for _, ext := range m.List() {
		if ext.Name() == commandName {
			if extOwner != "" && ext.Owner() == extOwner {
				return ext, alreadyInstalledError
			}
			return ext, fmt.Errorf("there is already an installed extension that provides the %q command", commandName)
		}
	}

	return nil, nil
}

func normalizeExtensionSelector(n string) string {
	if idx := strings.IndexRune(n, '/'); idx >= 0 {
		n = n[idx+1:]
	}
	return strings.TrimPrefix(n, "gh-")
}

func displayExtensionVersion(ext extensions.Extension, version string) string {
	if !ext.IsBinary() && len(version) > 8 {
		return version[:8]
	}
	return version
}