feat(pkg): accept install repo shorthand
All checks were successful
Security Scan / security (push) Successful in 20s
All checks were successful
Security Scan / security (push) Successful in 20s
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
d84d8cc838
commit
32342dfd31
3 changed files with 86 additions and 8 deletions
|
|
@ -25,7 +25,7 @@ var (
|
||||||
// addPkgInstallCommand adds the 'pkg install' command.
|
// addPkgInstallCommand adds the 'pkg install' command.
|
||||||
func addPkgInstallCommand(parent *cobra.Command) {
|
func addPkgInstallCommand(parent *cobra.Command) {
|
||||||
installCmd := &cobra.Command{
|
installCmd := &cobra.Command{
|
||||||
Use: "install <org/repo>",
|
Use: "install [org/]repo",
|
||||||
Short: i18n.T("cmd.pkg.install.short"),
|
Short: i18n.T("cmd.pkg.install.short"),
|
||||||
Long: i18n.T("cmd.pkg.install.long"),
|
Long: i18n.T("cmd.pkg.install.long"),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
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 {
|
func runPkgInstall(repoArg, targetDir string, addToRegistry bool) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Parse org/repo
|
// 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, "/")
|
parts := strings.Split(repoArg, "/")
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||||
return errors.New(i18n.T("cmd.pkg.error.invalid_repo_format"))
|
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
|
// Determine target directory
|
||||||
if targetDir == "" {
|
if targetDir == "" {
|
||||||
|
|
|
||||||
69
cmd/core/pkgcmd/cmd_install_test.go
Normal file
69
cmd/core/pkgcmd/cmd_install_test.go
Normal file
|
|
@ -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")
|
||||||
|
}
|
||||||
|
|
@ -60,10 +60,10 @@ core pkg search --refresh
|
||||||
|
|
||||||
## pkg install
|
## 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
|
```bash
|
||||||
core pkg install <org/repo> [flags]
|
core pkg install [org/]repo [flags]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Flags
|
### Flags
|
||||||
|
|
@ -76,6 +76,9 @@ core pkg install <org/repo> [flags]
|
||||||
### Examples
|
### Examples
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
# Clone from the default host-uk org
|
||||||
|
core pkg install core-api
|
||||||
|
|
||||||
# Clone to packages/
|
# Clone to packages/
|
||||||
core pkg install host-uk/core-php
|
core pkg install host-uk/core-php
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue