-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: app command should support dashes in app name (#26)
* cmd: app command should support dashes in app name also we now have a strict validation check for user given app name both against go.mod module name rules and go package name rules for generated x/ pkg. * Remove dashes from appName in startServe * cmd/app: keep binary name same as the user give one also refactor the parts related to handling user given app path/name. * templates/app/templates/Makefile: rm dashes from conf dirs Co-authored-by: Denis Fadeev <[email protected]> Co-authored-by: Denis Fadeev <[email protected]>
- Loading branch information
Showing
11 changed files
with
153 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package gomodulepath | ||
|
||
import ( | ||
"fmt" | ||
"go/parser" | ||
"go/token" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"golang.org/x/mod/module" | ||
) | ||
|
||
// Path represents a Go module's path. | ||
type Path struct { | ||
// Path is Go module's full path. | ||
// e.g.: github.com/tendermint/starport. | ||
RawPath string | ||
|
||
// Root is the root directory name of Go module. | ||
// e.g.: starport for github.com/tendermint/starport. | ||
Root string | ||
|
||
// Package is the default package name for the Go module that can be used | ||
// to host main functionality of the module. | ||
// e.g.: starport for github.com/tendermint/starport. | ||
Package string | ||
} | ||
|
||
// Parse parses rawpath into a module Path. | ||
func Parse(rawpath string) (Path, error) { | ||
if err := validateModulePath(rawpath); err != nil { | ||
return Path{}, err | ||
} | ||
rootName := root(rawpath) | ||
// package name cannot contain "-" so gracefully remove them | ||
// if they present. | ||
packageName := strings.ReplaceAll(rootName, "-", "") | ||
if err := validatePackageName(packageName); err != nil { | ||
return Path{}, err | ||
} | ||
p := Path{ | ||
RawPath: rawpath, | ||
Root: rootName, | ||
Package: packageName, | ||
} | ||
return p, nil | ||
} | ||
|
||
func validateModulePath(path string) error { | ||
if err := module.CheckPath(path); err != nil { | ||
return fmt.Errorf("app name is an invalid go module name: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func validatePackageName(name string) error { | ||
fset := token.NewFileSet() | ||
src := fmt.Sprintf("package %s", name) | ||
if _, err := parser.ParseFile(fset, "", src, parser.PackageClauseOnly); err != nil { | ||
// parser error is very low level here so let's hide it from the user | ||
// completely. | ||
return errors.New("app name is an invalid go package name") | ||
} | ||
return nil | ||
} | ||
|
||
func root(path string) string { | ||
sp := strings.Split(path, "/") | ||
return sp[len(sp)-1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package gomodulepath | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
rawpath string | ||
path Path | ||
err error | ||
}{ | ||
{"standard", | ||
"github.com/a/b", Path{"github.com/a/b", "b", "b"}, nil, | ||
}, | ||
{"with dash", | ||
"github.com/a/b-c", Path{"github.com/a/b-c", "b-c", "bc"}, nil, | ||
}, | ||
{"long", | ||
"github.com/a/b/c", Path{"github.com/a/b/c", "c", "c"}, nil, | ||
}, | ||
{"invalid as go.mod module name", | ||
"github.com/a/b/c@", Path{}, fmt.Errorf("app name is an invalid go module name: %w", | ||
errors.New(`malformed module path "github.com/a/b/c@": invalid char '@'`)), | ||
}, | ||
} | ||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
path, err := Parse(tt.rawpath) | ||
require.Equal(t, tt.err, err) | ||
if err != nil { | ||
return | ||
} | ||
require.Equal(t, tt.path, path) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.