Skip to content

Commit

Permalink
Updates to Atlas CLI (#2)
Browse files Browse the repository at this point in the history
* Determine project root based on user's GOPATH
* Fix issue that causes incomplete import resolution
* Remove unused imports from service .proto file
* Print helpful error message command-line calls fail
* Replace 'ngp.api.toolkit' with 'atlas.app.toolkit' in CLI README file
* Add debug flag to help users determine why CLI failed
  • Loading branch information
ZachEddy authored and dmacthedestroyer committed Apr 12, 2018
1 parent 1c541f7 commit be14802
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 61 deletions.
2 changes: 1 addition & 1 deletion cli/atlas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ You're all set! Alternatively, you can clone the repository and install the bina

```sh
$ git clone https://github.com/infobloxopen/atlas-app-toolkit.git
$ cd ngp.api.toolkit/cli/atlas
$ cd atlas-app-toolkit/cli/atlas
$ go install
```

Expand Down
29 changes: 18 additions & 11 deletions cli/atlas/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
var (
errEmptyServiceName = formatError{"empty service name"}
errInvalidFirstRune = formatError{"leading non-letter in service name"}
errInvalidProjectRoot = formatError{"project must be initialized inside $GOPATH/src directory"}
errInvalidProjectRoot = formatError{"project must be initialized inside $GOPATH/src directory or below"}
errMissingGOPATH = formatError{"$GOPATH environment variable not set"}
)

// ServiceName takes a string and formats it into a valid gRPC service name
Expand Down Expand Up @@ -40,17 +41,23 @@ func ServerURL(str string) (string, error) {
}

// ProjectRoot determines the root directory of an application. The project
// root is considered to be anything after go/src/...
func ProjectRoot(dirString string) (string, error) {
dirs := strings.Split(dirString, "/")
for i, dir := range dirs {
if strings.ToLower(dir) == "go" && i+1 < len(dirs) {
if i+2 < len(dirs) && strings.ToLower(dirs[i+1]) == "src" {
return strings.Join(dirs[i+2:], "/"), nil
}
}
// root is considered to be anything in $GOPATH/src or below
func ProjectRoot(gopath, workdir string) (string, error) {
if gopath == "" {
return "", errMissingGOPATH
}
return "", errInvalidProjectRoot
if len(workdir) < len(gopath) {
return "", errInvalidProjectRoot
}
if workdir[:len(gopath)] != gopath {
return "", errInvalidProjectRoot
}
projectpath := workdir[len(gopath):]
dirs := strings.Split(projectpath, "/")
if len(dirs) < 2 {
return "", errInvalidProjectRoot
}
return strings.Join(dirs[2:], "/"), nil
}

// isSpecial checks if rune is non-alphanumeric
Expand Down
64 changes: 47 additions & 17 deletions cli/atlas/formatting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,68 @@ func TestServerURL(t *testing.T) {

func TestProjectRoot(t *testing.T) {
var tests = []struct {
path string
gopath string
workdir string
expected string
err error
}{
{
"go/src/ProjectRoot",
"ProjectRoot",
nil,
gopath: "/Users/person/go",
workdir: "/Users/person/go/src",
expected: "",
err: nil,
},
{
"go/src/github.com/secret_project",
"github.com/secret_project",
nil,
gopath: "/Users/person/go",
workdir: "/Users/person/go/src/SomeProject",
expected: "SomeProject",
err: nil,
},
{
"/Users/john/go/src/github.com/infobloxopen/helloWorld",
"github.com/infobloxopen/helloWorld",
nil,
gopath: "/Users/person/go_projects",
workdir: "/Users/person/go_projects/src/SomeProject",
expected: "SomeProject",
err: nil,
},
{
"/Users/john/go",
"",
errInvalidProjectRoot,
gopath: "go",
workdir: "go/src/github.com/SomeProject",
expected: "github.com/SomeProject",
err: nil,
},
{
"go/src",
"",
errInvalidProjectRoot,
gopath: "/Users/person/go",
workdir: "/Users/person/go/src/github.com/infobloxopen/SomeProject",
expected: "github.com/infobloxopen/SomeProject",
err: nil,
},
{
gopath: "/Users/person/go",
workdir: "/Users/python",
expected: "",
err: errInvalidProjectRoot,
},
{
gopath: "/Users/person/go",
workdir: "/Users/person/hi/src/SomeProject",
expected: "",
err: errInvalidProjectRoot,
},
{
gopath: "/Users/person/go",
workdir: "/Users/person/go",
expected: "",
err: errInvalidProjectRoot,
},
{
gopath: "",
workdir: "/Users/person/go/src/SomeProject",
expected: "",
err: errMissingGOPATH,
},
}
for _, test := range tests {
root, err := ProjectRoot(test.path)
root, err := ProjectRoot(test.gopath, test.workdir)
if root != test.expected {
t.Errorf("Unexpected service name: %s - expected %s", root, test.expected)
}
Expand Down
29 changes: 20 additions & 9 deletions cli/atlas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
FLAG_NAME = "name"
FLAG_REGISTRY = "registry"
FLAG_GATEWAY = "gateway"
FLAG_DEBUG = "debug"
)

var (
Expand All @@ -27,6 +28,7 @@ var (
initializeName = initialize.String(FLAG_NAME, "", "the application name (required)")
initializeRegistry = initialize.String(FLAG_REGISTRY, "", "the Docker registry (optional)")
initializeGateway = initialize.Bool(FLAG_GATEWAY, false, "generate project with a gRPC gateway (default false)")
initializeDebug = initialize.Bool(FLAG_DEBUG, false, "print debug statements during intialization (default false)")
)

func main() {
Expand Down Expand Up @@ -154,7 +156,7 @@ func initializeApplication() {
if err != nil {
printErr(err)
}
root, err := ProjectRoot(wd)
root, err := ProjectRoot(os.Getenv("GOPATH"), wd)
if err != nil {
printErr(err)
}
Expand Down Expand Up @@ -188,10 +190,10 @@ func initializeApplication() {
if err := generateProtobuf(); err != nil {
printErr(err)
}
if err := resolveImports(app.directories()); err != nil {
if err := initDep(); err != nil {
printErr(err)
}
if err := initDep(); err != nil {
if err := resolveImports(app.directories()); err != nil {
printErr(err)
}
}
Expand All @@ -204,8 +206,7 @@ func printErr(err error) {
// initDep calls "dep init" to generate .toml files
func initDep() error {
fmt.Print("Starting dep project... ")
err := exec.Command("dep", "init").Run()
if err != nil {
if err := runCommand("dep", "init"); err != nil {
return err
}
fmt.Println("done!")
Expand All @@ -215,8 +216,7 @@ func initDep() error {
// generateProtobuf calls "make protobuf" to render initial .pb files
func generateProtobuf() error {
fmt.Print("Generating protobuf files... ")
err := exec.Command("make", "protobuf").Run()
if err != nil {
if err := runCommand("make", "protobuf"); err != nil {
return err
}
fmt.Println("done!")
Expand All @@ -227,11 +227,22 @@ func generateProtobuf() error {
func resolveImports(dirs []string) error {
fmt.Print("Resolving imports... ")
for _, dir := range dirs {
err := exec.Command("goimports", "-w", dir).Run()
if err != nil {
if err := runCommand("goimports", "-w", dir); err != nil {
return err
}
}
fmt.Println("done!")
return nil
}

func runCommand(command string, args ...string) error {
cmd := exec.Command(command, args...)
if *initializeDebug {
cmd.Stderr = os.Stdout
cmd.Stdout = os.Stderr
}
if err := cmd.Run(); err != nil {
return err
}
return nil
}
Loading

0 comments on commit be14802

Please sign in to comment.