Update --svcout to allow you to specify the service directory name (#165)

* Update so code functions inside svcName-service, template generation updated

* Update docs path to be relative to inside service dir

* Modify svcout flag to be directory where files inside NAME-service are created

* Add svcout flag ending in trailing / create NAME-service directory

* Test new svcout flag features
This commit is contained in:
Adam Ryman 2017-03-29 11:12:34 -07:00 коммит произвёл GitHub
Родитель 09b206d71c
Коммит df5873b83f
10 изменённых файлов: 206 добавлений и 176 удалений

Просмотреть файл

@ -57,12 +57,20 @@ func TestMain(m *testing.M) {
os.Exit(1)
}
err = createTrussService(filepath.Join(basePath, "0-basic"))
path := filepath.Join(basePath, "0-basic")
err = createTrussService(path)
if err != nil {
fmt.Printf("cannot create truss service: %v", err)
os.Exit(1)
}
err = buildTestService(filepath.Join(path, "test-service"))
if err != nil {
fmt.Printf("cannot build truss service: %v", err)
os.Exit(1)
}
defer func() {
os.RemoveAll(filepath.Join(basePath, "0-basic"))
os.Exit(exitCode)
@ -88,9 +96,29 @@ func TestBasicTypesWithRelPBOutFlag(t *testing.T) {
}
func TestBasicTypesWithRelSVCOutFlag(t *testing.T) {
testEndToEnd("1-basic", "getbasic", t,
"--svcout",
".")
svcOut := "./tunelab"
path := filepath.Join(basePath, "1-basic")
err := createTrussService(path, "--svcout", svcOut)
if err != nil {
t.Fatal(err)
}
err = buildTestService(filepath.Join(path, svcOut))
if err != nil {
t.Fatal(err)
}
}
func TestBasicTypesWithTrailingSlashSVCOutFlag(t *testing.T) {
svcOut := "./tunelab/"
path := filepath.Join(basePath, "1-basic")
err := createTrussService(path, "--svcout", svcOut)
if err != nil {
t.Fatal(err)
}
err = buildTestService(filepath.Join(path, svcOut, "test-service"))
if err != nil {
t.Fatal(err)
}
}
func TestMultipleFiles(t *testing.T) {
@ -114,7 +142,7 @@ func _TestMapTypes(t *testing.T) {
// Ensure that environment variables are used
func TestPortVariable(t *testing.T) {
path := filepath.Join(basePath, "0-basic")
path := filepath.Join(basePath, "0-basic", "test-service")
grpcPort := strconv.Itoa(FindFreePort())
httpPort := strconv.Itoa(FindFreePort())
debugPort := strconv.Itoa(FindFreePort())
@ -146,7 +174,15 @@ func TestPortVariable(t *testing.T) {
func testEndToEnd(defDir string, subcmd string, t *testing.T, trussOptions ...string) {
path := filepath.Join(basePath, defDir)
createTrussService(path)
err := createTrussService(path)
if err != nil {
t.Fatal(err)
}
path = filepath.Join(path, "test-service")
err = buildTestService(path)
if err != nil {
t.Fatal(err)
}
grpcPort := strconv.Itoa(FindFreePort())
httpPort := strconv.Itoa(FindFreePort())
@ -189,11 +225,6 @@ func createTrussService(path string, trussFlags ...string) error {
return errors.Errorf("Truss generation FAILED - %v\nTruss Output:\n%v", path, trussOut)
}
// Build the service to be tested
err = buildTestService(path)
if err != nil {
return errors.Errorf("Could not build service. Error: %v", err)
}
return nil
}
@ -244,8 +275,8 @@ func buildTestService(serviceDir string) (err error) {
return err
}
const serverPath = "/test-service/test-server"
const clientPath = "/test-service/test-cli-client"
const serverPath = "/test-server"
const clientPath = "/test-cli-client"
// Build server and client
errChan := make(chan error)

Просмотреть файл

@ -25,7 +25,7 @@ import (
var (
pbPackageFlag = flag.String("pbout", "", "Go package path where the protoc-gen-go .pb.go files will be written")
svcPackageFlag = flag.String("svcout", "", "Go package path where the generated Go service will be written")
svcPackageFlag = flag.String("svcout", "", "Go package path where the generated Go service will be written. Trailing slash will create a NAME-service directory")
verboseFlag = flag.BoolP("verbose", "v", false, "Verbose output")
helpFlag = flag.BoolP("help", "h", false, "Print usage")
)
@ -133,10 +133,10 @@ func parseInput() (*truss.Config, error) {
if err != nil {
return nil, errors.Wrap(err, "cannot parse service name from the provided definition files")
}
svcFolderName := svcName + "-service"
svcDirName := svcName + "-service"
if *svcPackageFlag == "" {
svcPath := filepath.Join(filepath.Dir(cfg.DefPaths[0]), svcFolderName)
svcPath := filepath.Join(filepath.Dir(cfg.DefPaths[0]), svcDirName)
p, err := build.Default.ImportDir(svcPath, build.FindOnly)
if err != nil {
return nil, err
@ -148,19 +148,31 @@ func parseInput() (*truss.Config, error) {
cfg.ServicePackage = p.ImportPath
cfg.ServicePath = p.Dir
} else {
baseSVCPackage := *svcPackageFlag
p, err := build.Default.Import(baseSVCPackage, wd, build.FindOnly)
p, err := build.Default.Import(*svcPackageFlag, wd, build.FindOnly)
if err != nil {
return nil, err
}
if p.Root == "" {
return nil, errors.New("svcout not in GOPATH")
}
if !fileExists(p.Dir) {
return nil, errors.Errorf("specified package path for service output directory does not exist: %q", p.Dir)
cfg.ServicePath = p.Dir
cfg.ServicePackage = p.ImportPath
// If the package flag ends in a seperator, file will be "".
// In this case, append the svcDirName to the path and package
_, file := filepath.Split(*svcPackageFlag)
if file == "" {
cfg.ServicePath = filepath.Join(cfg.ServicePath, svcDirName)
cfg.ServicePackage = filepath.Join(cfg.ServicePackage, svcDirName)
}
if !fileExists(cfg.ServicePath) {
err := os.MkdirAll(cfg.ServicePath, 0777)
if err != nil {
return nil, errors.Errorf("specified package path for service output directory cannot be created: %q", p.Dir)
}
}
cfg.ServicePackage = filepath.Join(p.ImportPath, svcFolderName)
cfg.ServicePath = filepath.Join(p.Dir, svcFolderName)
}
log.WithField("Service Package", cfg.ServicePackage).Debug()
log.WithField("Service Path", cfg.ServicePath).Debug()
@ -300,13 +312,7 @@ func combineFiles(group ...map[string]io.Reader) map[string]io.Reader {
// writeGenFile writes a file at relPath relative to serviceDir to the filesystem
func writeGenFile(file io.Reader, relPath, serviceDir string) error {
// the serviceDir contains /NAME-service so we want to write to the
// directory above
outDir := filepath.Dir(serviceDir)
// i.e. NAME-service/generated/endpoint.go
fullPath := filepath.Join(outDir, relPath)
fullPath := filepath.Join(serviceDir, relPath)
err := os.MkdirAll(filepath.Dir(fullPath), 0777)
if err != nil {
return err
@ -365,7 +371,6 @@ func readPreviousGeneration(serviceDir string) (map[string]io.Reader, error) {
return nil, nil
}
dir, _ := filepath.Split(serviceDir)
files := make(map[string]io.Reader)
addFileToFiles := func(path string, info os.FileInfo, err error) error {
@ -380,7 +385,7 @@ func readPreviousGeneration(serviceDir string) (map[string]io.Reader, error) {
}
// trim the prefix of the path to the proto files from the full path to the file
relPath, err := filepath.Rel(dir, path)
relPath, err := filepath.Rel(serviceDir, path)
if err != nil {
return err
}
@ -392,7 +397,7 @@ func readPreviousGeneration(serviceDir string) (map[string]io.Reader, error) {
err := filepath.Walk(serviceDir, addFileToFiles)
if err != nil {
return nil, errors.Wrapf(err, "cannot fully walk directory %v", dir)
return nil, errors.Wrapf(err, "cannot fully walk directory %v", serviceDir)
}
return files, nil

Просмотреть файл

@ -7,7 +7,6 @@ import (
"strings"
"github.com/TuneLab/go-truss/deftree"
"github.com/golang/protobuf/protoc-gen-go/generator"
)
func findServiceName(md *deftree.MicroserviceDefinition) string {
@ -33,11 +32,9 @@ func GenerateDocs(dt deftree.Deftree) map[string]io.Reader {
response = "Error, could not cast Deftree to MicroserviceDefinition"
}
files := make(map[string]io.Reader)
md := dt.(*deftree.MicroserviceDefinition)
// Normalize the service to prevent diversion from convention
svcname := strings.ToLower(generator.CamelCase(findServiceName(md)))
files[svcname+"-service/docs/docs.md"] = strings.NewReader(response)
files := map[string]io.Reader{
"docs/docs.md": strings.NewReader(response),
}
return files
}

Просмотреть файл

@ -6,7 +6,6 @@ import (
"go/format"
"io"
"io/ioutil"
"path"
"strings"
log "github.com/Sirupsen/logrus"
@ -32,7 +31,7 @@ func GenerateGokit(sd *svcdef.Svcdef, conf gengokit.Config) (map[string]io.Reade
codeGenFiles := make(map[string]io.Reader)
// Remove the suffix "-service" since it's added back in by templatePathToActual
svcname := strings.TrimSuffix(path.Base(conf.GoPackage), "-service")
svcname := strings.ToLower(sd.Service.Name)
for _, templPath := range templFiles.AssetNames() {
// Re-derive the actual path for this file based on the service output
// path provided by the truss main.go
@ -110,7 +109,7 @@ func generateResponseFile(templFP string, data *gengokit.Data, prevFile io.Reade
// disk
func templatePathToActual(templFilePath, svcName string) string {
// Switch "NAME" in path with svcName.
// i.e. for svcName = addsvc; /NAME-service/NAME-server -> /addsvc-service/addsvc-server
// i.e. for svcName = addsvc; /NAME-server -> /addsvc-service/addsvc-server
actual := strings.Replace(templFilePath, "NAME", svcName, -1)
actual = strings.TrimSuffix(actual, "template")

Просмотреть файл

@ -87,7 +87,7 @@ func TestApplyTemplateFromPath(t *testing.T) {
t.Fatal(err)
}
end, err := applyTemplateFromPath("NAME-service/generated/endpoints.gotemplate", te)
end, err := applyTemplateFromPath("generated/endpoints.gotemplate", te)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -23,7 +23,7 @@ import (
const ignoredFunc = "NewService"
// ServerHadlerPath is the relative path to the server handler template file
const ServerHandlerPath = "NAME-service/handlers/server/server_handler.gotemplate"
const ServerHandlerPath = "handlers/server/server_handler.gotemplate"
// New returns a truss.Renderable capable of updating server handlers.
// New should be passed the previous version of the server handler to parse.

Просмотреть файл

@ -7,7 +7,7 @@ import (
"github.com/TuneLab/go-truss/gengokit"
)
const HookPath = "NAME-service/handlers/server/hooks.gotemplate"
const HookPath = "handlers/server/hooks.gotemplate"
// NewHook returns a new HookRender
func NewHook(prev io.Reader) gengokit.Renderable {

Просмотреть файл

@ -1,4 +1,4 @@
// Package middlewares renders the service and endpointe middleware files in NAME-service/middlewares/.
// Package middlewares renders the service and endpointe middleware files in middlewares/.
package middlewares
import (
@ -11,10 +11,10 @@ import (
)
// EndpointsPath is the path to the Endpoints middleware file that package middlewares renders
const EndpointsPath = "NAME-service/middlewares/endpoints.gotemplate"
const EndpointsPath = "middlewares/endpoints.gotemplate"
// ServicePath is the path to the Service middleware file that package middlewares renders
const ServicePath = "NAME-service/middlewares/service.gotemplate"
const ServicePath = "middlewares/service.gotemplate"
// New returns a Middleware which can render EndpointsFile and ServiceFile as
// well as read in previous versions of each respective file

Просмотреть файл

@ -1,4 +1,4 @@
//go:generate go-bindata -mode 0644 -modtime 1464111000 -o template.go -pkg template -ignore swp NAME-service/...
//go:generate go-bindata -mode 0644 -modtime 1464111000 -o template.go -pkg template -ignore swp -prefix NAME-service/ NAME-service/...
/*
This file is here to hold the `go generate` command above.

Различия файлов скрыты, потому что одна или несколько строк слишком длинны