Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Group top level commands by registry #140

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gh-pages/content/en/docs/overview/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ toc: true
| verify_package_signature | bool | whether to verify the package signature during package installation (will be available in 1.8) |
| extra_remotes | map | extra remote registry configurations, see extra remote configuration (available 1.8+) |
| enable_package_setup_hook | bool | call setup hook after a new version of package is installed (available 1.9+) |
| group_help_by_registry | bool | group help by registry, default true (available 1.13+) |

### extra remote configuration

Expand Down
3 changes: 3 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func setDefaultConfig() {

viper.SetDefault(EXTRA_REMOTES_KEY, []map[string]string{})
viper.SetDefault(ENABLE_PACKAGE_SETUP_HOOK_KEY, false)

// by default, group the top level command by registry in the help message
viper.SetDefault(GROUP_HELP_BY_REGISTRY_KEY, true)
}

func initDefaultConfigFile() {
Expand Down
4 changes: 4 additions & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
EXTRA_REMOTE_REPOSITORY_DIR_KEY = "REPOSITORY_DIR"
EXTRA_REMOTE_SYNC_POLICY_KEY = "SYNC_POLICY"
ENABLE_PACKAGE_SETUP_HOOK_KEY = "ENABLE_PACKAGE_SETUP_HOOK"
GROUP_HELP_BY_REGISTRY_KEY = "GROUP_HELP_BY_REGISTRY"

// internal commands are the commands with start partition number > INTERNAL_START_PARTITION
INTERNAL_COMMAND_ENABLED_KEY = "INTERNAL_COMMAND_ENABLED"
Expand Down Expand Up @@ -77,6 +78,7 @@ func init() {
SYSTEM_PACKAGE_KEY,
SYSTEM_PACKAGE_PUBLIC_KEY_FILE_KEY,
ENABLE_PACKAGE_SETUP_HOOK_KEY,
GROUP_HELP_BY_REGISTRY_KEY,
)
}

Expand Down Expand Up @@ -133,6 +135,8 @@ func SetSettingValue(key string, value string) error {
return setBooleanConfig(upperKey, value)
case ENABLE_PACKAGE_SETUP_HOOK_KEY:
return setBooleanConfig(upperKey, value)
case GROUP_HELP_BY_REGISTRY_KEY:
return setBooleanConfig(upperKey, value)
}

return fmt.Errorf("unsupported config %s", key)
Expand Down
26 changes: 26 additions & 0 deletions internal/frontend/default-frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type defaultFrontend struct {

groupCmds map[string]*cobra.Command
executableCmds map[string]*cobra.Command

cmdCategories []*cobra.Group
}

func NewDefaultFrontend(appCtx context.LauncherContext, rootCmd *cobra.Command, backend backend.Backend) Frontend {
Expand All @@ -50,9 +52,24 @@ func NewDefaultFrontend(appCtx context.LauncherContext, rootCmd *cobra.Command,
groupCmds: make(map[string]*cobra.Command),
executableCmds: make(map[string]*cobra.Command),
}
// add all command categories
if viper.GetBool(config.GROUP_HELP_BY_REGISTRY_KEY) {
frontend.RegisterCommandCategory()
}
return frontend
}

func (self *defaultFrontend) RegisterCommandCategory() {
self.cmdCategories = []*cobra.Group{}
for _, source := range self.backend.AllPackageSources() {
self.cmdCategories = append(self.cmdCategories, &cobra.Group{
ID: source.Repo.Name(),
Title: fmt.Sprintf("Commands from '%s' registry", source.Repo.Name()),
})
}
self.rootCmd.AddGroup(self.cmdCategories...)
}

func (self *defaultFrontend) AddUserCommands() {
self.addGroupCommands()
self.addExecutableCommands()
Expand All @@ -61,6 +78,7 @@ func (self *defaultFrontend) AddUserCommands() {
func (self *defaultFrontend) addGroupCommands() {
groups := self.backend.GroupCommands()
for _, v := range groups {
registryName := v.RepositoryID()
group := v.RuntimeGroup()
name := v.RuntimeName()
usage := strings.TrimSpace(fmt.Sprintf("%s %s",
Expand Down Expand Up @@ -99,13 +117,18 @@ func (self *defaultFrontend) addGroupCommands() {
self.processFlags(false, group, name, cmd, flags, exclusiveFlags, groupFlags)

self.groupCmds[v.RuntimeName()] = cmd

if viper.GetBool(config.GROUP_HELP_BY_REGISTRY_KEY) {
cmd.GroupID = registryName
}
self.rootCmd.AddCommand(cmd)
}
}

func (self *defaultFrontend) addExecutableCommands() {
executables := self.backend.ExecutableCommands()
for _, v := range executables {
registryName := v.RepositoryID()
group := v.RuntimeGroup()
name := v.RuntimeName()
usage := strings.TrimSpace(fmt.Sprintf("%s %s",
Expand Down Expand Up @@ -205,6 +228,9 @@ func (self *defaultFrontend) addExecutableCommands() {
}

if v.RuntimeGroup() == "" {
if viper.GetBool(config.GROUP_HELP_BY_REGISTRY_KEY) {
cmd.GroupID = registryName
}
self.rootCmd.AddCommand(cmd)
} else {
if group, exists := self.groupCmds[v.RuntimeGroup()]; exists {
Expand Down
35 changes: 35 additions & 0 deletions test/integration/test-basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,38 @@ else
echo "KO - hello command shouldn't exist"
exit 1
fi

##
# test help message
##

# clean up the dropin folder
rm -rf $CL_HOME/dropins
mkdir -p $CL_HOME/dropins

# copy the example to the dropin folder for the test
cp -R $SCRIPT_DIR/../packages-src/bonjour $CL_HOME/dropins

echo "> test group help message"
RESULT=$($CL_PATH)
echo "$RESULT" | grep -q "Commands from 'dropin' registry"
if [ $? -eq 0 ]; then
# ok
echo "OK"
else
echo "KO - should group help message by registry"
exit 1
fi

echo "> test help message without group"
# set group_help_by_registry to false
$CL_PATH config group_help_by_registry false
# run command launcher to show the help message
RESULT=$($CL_PATH)
echo "$RESULT" | grep -q "Commands from 'dropin' registry"
if [ $? -eq 0 ]; then
echo "KO - should group help message by registry"
exit 1
else
echo "OK"
fi
9 changes: 9 additions & 0 deletions test/integration/test-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ else
exit 1
fi

echo "> test group_help_by_registry config exist, and default true"
RESULT=$($OUTPUT_DIR/cl config --json)
VALUE=$(echo "$RESULT" | jq -r '.group_help_by_registry')
if [ $VALUE == "true" ]; then
echo "OK"
else
echo "KO - incorrect config value"
exit 1
fi
Loading