Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

feat(repo): Create project from URL #709

Merged
merged 2 commits into from
May 11, 2021
Merged
Changes from 1 commit
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
46 changes: 36 additions & 10 deletions commands/project/create/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/profclems/glab/pkg/prompt"

"github.com/profclems/glab/api"
"github.com/profclems/glab/internal/glinstance"
"github.com/profclems/glab/internal/glrepo"

"github.com/MakeNowJust/heredoc"
Expand Down Expand Up @@ -76,13 +77,23 @@ func runCreateProject(cmd *cobra.Command, args []string, f *cmdutils.Factory) er
if err != nil {
return err
}
apiClient, err := f.HttpClient()
if err != nil {
return err
}

if len(args) == 1 {
projectPath = args[0]
if strings.Contains(projectPath, "/") {
pp := strings.Split(projectPath, "/")
projectPath = pp[1]
namespace = pp[0]
var host string
host, namespace, projectPath = projectPathFromArgs(args)
if host != "" {
glinstance.OverrideDefault(host)
}
user, err := api.CurrentUser(apiClient)
if err != nil {
return err
}
if user.Username == namespace {
namespace = ""
}
} else {
projectPath, err = git.ToplevelDir()
Expand All @@ -93,11 +104,6 @@ func runCreateProject(cmd *cobra.Command, args []string, f *cmdutils.Factory) er
isPath = true
}

apiClient, err := f.HttpClient()
if err != nil {
return err
}

group, err := cmd.Flags().GetString("group")
if err != nil {
return fmt.Errorf("could not parse group flag: %v", err)
Expand Down Expand Up @@ -254,3 +260,23 @@ func initialiseRepo(projectPath, remoteURL string) error {
}
return nil
}

func projectPathFromArgs(args []string) (host, namespace, project string) {
pathURL, err := url.Parse(args[0])
if err == nil {
host = pathURL.Host
pp := strings.Split(pathURL.Path, "/")
if len(pp) >= 3 {
project = pp[len(pp)-1]
namespace = strings.Join(pp[1:len(pp)-1], "/")
}
return
}
project = args[0]
if strings.Contains(project, "/") {
pp := strings.Split(project, "/")
project = pp[1]
namespace = pp[0]
}
return
}