Skip to content

Commit

Permalink
(project create) parse group from path
Browse files Browse the repository at this point in the history
  • Loading branch information
zaquestion authored and bmeneg committed Dec 22, 2020
1 parent 76e7bcd commit 22b09ca
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
25 changes: 19 additions & 6 deletions cmd/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ lab project create myproject # user/myproject named myproject
lab project create myproject -n "new proj" # user/myproject named "new proj"
lab project create -n "new proj" # user/new-proj named "new proj"
lab project create mygroup/myproject # mygroup/myproject named myproject
lab project create -g mygroup myproject # mygroup/myproject named myproject`,
Args: cobra.MaximumNArgs(1),
PersistentPreRun: LabPersistentPreRun,
Expand All @@ -43,19 +44,26 @@ lab project create -g mygroup myproject # mygroup/myproject named myproject`,
group, _ = cmd.Flags().GetString("group")
)

path := determinePath(args, name)
g, path := determineNamespacePath(args, name)
if path == "" && name == "" {
log.Fatal("path or name must be set")
}
if g != "" && group != "" {

log.Fatalf("group can be passed by flag or in path not both\n%s", labUsageFormat(cmd))
}
if g != "" {
group = g
}

var namespaceID *int
if group != "" {
list, err := lab.NamespaceSearch(group)
list, err := lab.GroupSearch(group)
if err != nil {
log.Fatal(err)
}

if len(list) < 0 {
if len(list) == 0 {
log.Fatalf("no namespace found with such name: %s", group)
}

Expand Down Expand Up @@ -102,10 +110,15 @@ lab project create -g mygroup myproject # mygroup/myproject named myproject`,
},
}

func determinePath(args []string, name string) string {
func determineNamespacePath(args []string, name string) (string, string) {
var path string
if len(args) > 0 {
path = args[0]
ps := strings.Split(args[0], "/")
if len(ps) == 1 {
return "", ps[0]
} else {
return strings.Join(ps[:len(ps)-1], "/"), ps[len(ps)-1]
}
}
if path == "" && name == "" && git.InsideGitRepo() {
wd, err := git.WorkingDir()
Expand All @@ -115,7 +128,7 @@ func determinePath(args []string, name string) string {
p := strings.Split(wd, "/")
path = p[len(p)-1]
}
return path
return "", path
}

func init() {
Expand Down
20 changes: 13 additions & 7 deletions cmd/project_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
Expand Down Expand Up @@ -52,26 +53,31 @@ func Test_projectCreateCmd(t *testing.T) {
}
}

func Test_determinePath(t *testing.T) {
func Test_determineNamespacePath(t *testing.T) {
t.Parallel()
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
tests := []struct {
desc string
args []string
expected string
desc string
args []string
expectedNamespace string
expectedPath string
}{
{"arguemnt", []string{"new_project"}, "new_project"},
{"git working dir", []string{}, filepath.Base(wd)},
{"arg", []string{"new_project"}, "", "new_project"},
{"git working dir", []string{}, "", filepath.Base(wd)},
{"namespace", []string{"group/new_project"}, "group", "new_project"},
{"namespace", []string{"company/group/new_project"}, "company/group", "new_project"},
}

for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
require.Equal(t, test.expected, determinePath(test.args, ""))
group, path := determineNamespacePath(test.args, "")
assert.Equal(t, test.expectedNamespace, group)
assert.Equal(t, test.expectedPath, path)
})
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/stretchr/testify v1.6.1
github.com/tcnksm/go-gitconfig v0.1.2
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad // indirect
github.com/xanzy/go-gitlab v0.39.0
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/yuin/goldmark v1.2.1 // indirect
Expand All @@ -55,6 +56,7 @@ require (
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
golang.org/x/tools v0.0.0-20200930213115-e57f6d466a48 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
)
Expand Down
6 changes: 3 additions & 3 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,9 @@ func (s JobSorter) Less(i, j int) bool {
return time.Time(*s.Jobs[i].CreatedAt).Before(time.Time(*s.Jobs[j].CreatedAt))
}

// NamespaceSearch searches for a namespace on GitLab
func NamespaceSearch(query string) ([]*gitlab.Namespace, error) {
list, _, err := lab.Namespaces.SearchNamespace(query)
// GroupSearch searches for a namespace on GitLab
func GroupSearch(query string) ([]*gitlab.Group, error) {
list, _, err := lab.Groups.SearchGroup(query)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 22b09ca

Please sign in to comment.