Skip to content

Commit

Permalink
Changed to InitFromName and InitFromRepo
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenari committed Aug 5, 2023
1 parent 641d308 commit 652434e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 76 deletions.
10 changes: 9 additions & 1 deletion internal/boxcli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/spf13/cobra"

"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/templates"
"go.jetpack.io/devbox/internal/ux"
)
Expand Down Expand Up @@ -76,7 +77,14 @@ func runCreateCmd(cmd *cobra.Command, args []string, flags *createCmdFlags) erro
path = filepath.Join(wd, flags.template)
}

err := templates.Init(cmd.ErrOrStderr(), flags.template, flags.repo, flags.subdir, path)
var err error
if flags.template != "" {
err = templates.InitFromName(cmd.ErrOrStderr(), flags.template, path)
} else if flags.repo != "" {
err = templates.InitFromRepo(cmd.ErrOrStderr(), flags.repo, flags.subdir, path)
} else {
err = usererr.New("either --template or --repo need to be specified")
}
if err != nil {
return err
}
Expand Down
46 changes: 20 additions & 26 deletions internal/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ import (
"go.jetpack.io/devbox/internal/boxcli/usererr"
)

func Init(w io.Writer, template, repo, subdir, dir string) error {
if err := createDirAndEnsureEmpty(dir); err != nil {
func InitFromName(w io.Writer, template string, target string) error {
templatePath, ok := templates[template]
if !ok {
return usererr.New("unknown template name or format %q", template)
}
return InitFromRepo(w, "https://github.com/jetpack-io/devbox", templatePath, target)
}

func InitFromRepo(w io.Writer, repo string, subdir string, target string) error {
if err := createDirAndEnsureEmpty(target); err != nil {
return err
}
repoURL, subdirPath, err := GetTemplateRepoAndSubdir(template, repo, subdir)
parsedRepoURL, err := ParseRepoURL(repo)
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -32,7 +40,7 @@ func Init(w io.Writer, template, repo, subdir, dir string) error {
if err != nil {
return errors.WithStack(err)
}
cmd := exec.Command("git", "clone", repoURL, tmp)
cmd := exec.Command("git", "clone", parsedRepoURL, tmp)
fmt.Fprintf(w, "%s\n", cmd)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand All @@ -42,7 +50,7 @@ func Init(w io.Writer, template, repo, subdir, dir string) error {

cmd = exec.Command(
"sh", "-c",
fmt.Sprintf("cp -r %s %s", filepath.Join(tmp, subdirPath, "*"), dir),
fmt.Sprintf("cp -r %s %s", filepath.Join(tmp, subdir, "*"), target),
)
fmt.Fprintf(w, "%s\n", cmd)
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -80,26 +88,12 @@ func createDirAndEnsureEmpty(dir string) error {
return nil
}

func GetTemplateRepoAndSubdir(template string, repo string, subdir string) (string, string, error) {
repoURL := "https://github.com/jetpack-io/devbox"
subdirPath := ""
if template != "" {
tPath, ok := templates[template]
if !ok {
return "", "", usererr.New("unknown template name or format %q", template)
}
subdirPath = tPath
} else if repo != "" {
_, err := url.Parse(template)
if err != nil {
return "", "", usererr.New("Invalid URL format for --repo %s", repo)
}
subdirPath = subdir
// this is to handle cases where user puts repo url with .git at the end
// like: https://github.com/jetpack-io/devbox.git
repoURL, _ = strings.CutSuffix(repo, ".git")
} else {
return "", "", usererr.New("either --template or --repo need to be specified %q", template)
func ParseRepoURL(repo string) (string, error) {
u, err := url.Parse(repo)
if err != nil || u.Scheme == "" || u.Host == "" {
return "", usererr.New("Invalid URL format for --repo %s", repo)
}
return repoURL, subdirPath, nil
// this is to handle cases where user puts repo url with .git at the end
// like: https://github.com/jetpack-io/devbox.git
return strings.TrimSuffix(repo, ".git"), nil
}
64 changes: 15 additions & 49 deletions internal/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,21 @@ func TestTemplatesExist(t *testing.T) {
}
}

func TestGetTemplateRepoAndSubdir(t *testing.T) {
// devbox create --template=nonexistenttemplate
_, _, err := GetTemplateRepoAndSubdir(
"nonexistenttemplate",
"",
"",
)
func TestParseRepoURL(t *testing.T) {
// devbox create --repo="http:::/not.valid/a//a??a?b=&&c#hi"
_, err := ParseRepoURL("http:::/not.valid/a//a??a?b=&&c#hi")
assert.Error(t, err)

// devbox create --template=apache --repo="https://example.com/org/repo.git" \
// --subdir="examples/test/should/ignore/repo/and/subdir"
repoURL, subdirPath, err := GetTemplateRepoAndSubdir(
"apache",
"https://example.com/org/repo.git",
"examples/test/should/ignore/repo/and/subdir",
)
assert.NoError(t, err)
assert.Equal(t, "https://github.com/jetpack-io/devbox", repoURL)
assert.Equal(t, "examples/servers/apache/", subdirPath)

// devbox create --repo="https://github.com/jetpack-io/typeid.git"
repoURL, subdirPath, err = GetTemplateRepoAndSubdir(
"",
"https://github.com/jetpack-io/typeid.git",
"",
)
assert.NoError(t, err)
assert.Equal(t, "https://github.com/jetpack-io/typeid", repoURL)
assert.Equal(t, "", subdirPath)

// devbox create --repo="https://github.com/jetpack-io/devbox" \
// --subdir="examples/servers/apache"
repoURL, subdirPath, err = GetTemplateRepoAndSubdir(
"",
"https://github.com/jetpack-io/devbox",
"examples/servers/apache",
)
assert.NoError(t, err)
assert.Equal(t, "https://github.com/jetpack-io/devbox", repoURL)
assert.Equal(t, "examples/servers/apache", subdirPath)

// devbox create --repo="[email protected]:jetpack-io/devbox.git" \
// --subdir="examples/servers/apache"
repoURL, subdirPath, err = GetTemplateRepoAndSubdir(
"",
"[email protected]:jetpack-io/devbox.git",
"examples/servers/apache",
)
_, err = ParseRepoURL("http//github.com")
assert.Error(t, err)
_, err = ParseRepoURL("github.com")
assert.Error(t, err)
_, err = ParseRepoURL("/foo/bar")
assert.Error(t, err)
_, err = ParseRepoURL("http://")
assert.Error(t, err)
_, err = ParseRepoURL("[email protected]:jetpack-io/devbox.git")
assert.Error(t, err)
u, err := ParseRepoURL("http://github.com")
assert.NoError(t, err)
assert.Equal(t, "[email protected]:jetpack-io/devbox", repoURL)
assert.Equal(t, "examples/servers/apache", subdirPath)
assert.Equal(t, "http://github.com", u)
}

0 comments on commit 652434e

Please sign in to comment.