diff --git a/cmd/core/pkgcmd/cmd_install.go b/cmd/core/pkgcmd/cmd_install.go index a486910..4d0ab9c 100644 --- a/cmd/core/pkgcmd/cmd_install.go +++ b/cmd/core/pkgcmd/cmd_install.go @@ -25,7 +25,7 @@ var ( // addPkgInstallCommand adds the 'pkg install' command. func addPkgInstallCommand(parent *cobra.Command) { installCmd := &cobra.Command{ - Use: "install ", + Use: "install [org/]repo", Short: i18n.T("cmd.pkg.install.short"), Long: i18n.T("cmd.pkg.install.long"), RunE: func(cmd *cobra.Command, args []string) error { @@ -45,12 +45,18 @@ func addPkgInstallCommand(parent *cobra.Command) { func runPkgInstall(repoArg, targetDir string, addToRegistry bool) error { ctx := context.Background() - // Parse org/repo - parts := strings.Split(repoArg, "/") - if len(parts) != 2 { - return errors.New(i18n.T("cmd.pkg.error.invalid_repo_format")) + // Parse repo shorthand: + // - repoName -> defaults to host-uk/repoName + // - org/repo -> uses the explicit org + org := "host-uk" + repoName := repoArg + if strings.Contains(repoArg, "/") { + parts := strings.Split(repoArg, "/") + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + return errors.New(i18n.T("cmd.pkg.error.invalid_repo_format")) + } + org, repoName = parts[0], parts[1] } - org, repoName := parts[0], parts[1] // Determine target directory if targetDir == "" { diff --git a/cmd/core/pkgcmd/cmd_install_test.go b/cmd/core/pkgcmd/cmd_install_test.go new file mode 100644 index 0000000..5d0f75d --- /dev/null +++ b/cmd/core/pkgcmd/cmd_install_test.go @@ -0,0 +1,69 @@ +package pkgcmd + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRunPkgInstall_AllowsRepoShorthand_Good(t *testing.T) { + tmp := t.TempDir() + targetDir := filepath.Join(tmp, "packages") + + originalGitClone := gitClone + t.Cleanup(func() { + gitClone = originalGitClone + }) + + var gotOrg, gotRepo, gotPath string + gitClone = func(_ context.Context, org, repoName, repoPath string) error { + gotOrg = org + gotRepo = repoName + gotPath = repoPath + return nil + } + + err := runPkgInstall("core-api", targetDir, false) + require.NoError(t, err) + + assert.Equal(t, "host-uk", gotOrg) + assert.Equal(t, "core-api", gotRepo) + assert.Equal(t, filepath.Join(targetDir, "core-api"), gotPath) + _, err = os.Stat(targetDir) + require.NoError(t, err) +} + +func TestRunPkgInstall_AllowsExplicitOrgRepo_Good(t *testing.T) { + tmp := t.TempDir() + targetDir := filepath.Join(tmp, "packages") + + originalGitClone := gitClone + t.Cleanup(func() { + gitClone = originalGitClone + }) + + var gotOrg, gotRepo, gotPath string + gitClone = func(_ context.Context, org, repoName, repoPath string) error { + gotOrg = org + gotRepo = repoName + gotPath = repoPath + return nil + } + + err := runPkgInstall("myorg/core-api", targetDir, false) + require.NoError(t, err) + + assert.Equal(t, "myorg", gotOrg) + assert.Equal(t, "core-api", gotRepo) + assert.Equal(t, filepath.Join(targetDir, "core-api"), gotPath) +} + +func TestRunPkgInstall_InvalidRepoFormat_Bad(t *testing.T) { + err := runPkgInstall("a/b/c", t.TempDir(), false) + require.Error(t, err) + assert.Contains(t, err.Error(), "invalid repo format") +} diff --git a/docs/cmd/pkg/index.md b/docs/cmd/pkg/index.md index f531964..c609226 100644 --- a/docs/cmd/pkg/index.md +++ b/docs/cmd/pkg/index.md @@ -60,10 +60,10 @@ core pkg search --refresh ## pkg install -Clone a package from GitHub. +Clone a package from GitHub. If you pass only a repo name, `core` assumes the `host-uk` org. ```bash -core pkg install [flags] +core pkg install [org/]repo [flags] ``` ### Flags @@ -76,6 +76,9 @@ core pkg install [flags] ### Examples ```bash +# Clone from the default host-uk org +core pkg install core-api + # Clone to packages/ core pkg install host-uk/core-php