forked from micro/go-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Gomu files after the fact (micro#2258)
* Move file generation to new package * Use text/template instead of html/template * Make config variables more consistent * Combine generate files and print comments there * Add gomu generate command * Refactor project templating to file library * Determine client earlier
- Loading branch information
1 parent
01b7b44
commit e23006b
Showing
16 changed files
with
300 additions
and
124 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package generate | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/asim/go-micro/cmd/gomu/cmd" | ||
"github.com/asim/go-micro/cmd/gomu/file" | ||
"github.com/asim/go-micro/cmd/gomu/file/generator" | ||
tmpl "github.com/asim/go-micro/cmd/gomu/file/template" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func init() { | ||
cmd.Register(&cli.Command{ | ||
Name: "generate", | ||
Usage: "Generate project template files after the fact", | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "skaffold", | ||
Usage: "Generate Skaffold project template files", | ||
Action: Skaffold, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// Skaffold generates Skaffold project template files in the current directory. | ||
// Exits on error. | ||
func Skaffold(ctx *cli.Context) error { | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
service := dir[strings.LastIndex(dir, "/")+1:] | ||
|
||
vendor, err := getServiceVendor(service) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
g := generator.New( | ||
generator.Service(service), | ||
generator.Vendor(vendor), | ||
generator.Directory("."), | ||
generator.Client(strings.HasSuffix(service, "-client")), | ||
generator.Skaffold(true), | ||
) | ||
|
||
files := []file.File{ | ||
{".dockerignore", tmpl.DockerIgnore}, | ||
{"go.mod", tmpl.Module}, | ||
{"plugins.go", tmpl.Plugins}, | ||
{"resources/clusterrole.yaml", tmpl.KubernetesClusterRole}, | ||
{"resources/configmap.yaml", tmpl.KubernetesEnv}, | ||
{"resources/deployment.yaml", tmpl.KubernetesDeployment}, | ||
{"resources/rolebinding.yaml", tmpl.KubernetesRoleBinding}, | ||
{"skaffold.yaml", tmpl.SkaffoldCFG}, | ||
} | ||
|
||
if err := g.Generate(files); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("skaffold project template files generated") | ||
|
||
return nil | ||
} | ||
|
||
func getServiceVendor(s string) (string, error) { | ||
f, err := os.Open("go.mod") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
line := "" | ||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
if strings.HasPrefix(scanner.Text(), "module ") { | ||
line = scanner.Text() | ||
break | ||
|
||
} | ||
} | ||
if line == "" { | ||
return "", nil | ||
} | ||
|
||
module := line[strings.LastIndex(line, " ")+1:] | ||
if module == s { | ||
return "", nil | ||
} | ||
|
||
return module[:strings.LastIndex(module, "/")] + "/", nil | ||
} |
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,6 @@ | ||
package file | ||
|
||
type File struct { | ||
Path string | ||
Template string | ||
} |
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 generator | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/asim/go-micro/cmd/gomu/file" | ||
) | ||
|
||
type Generator interface { | ||
Generate([]file.File) error | ||
} | ||
|
||
type generator struct { | ||
opts Options | ||
} | ||
|
||
// Generate generates project template files. | ||
func (g *generator) Generate(files []file.File) error { | ||
for _, file := range files { | ||
fp := filepath.Join(g.opts.Directory, file.Path) | ||
dir := filepath.Dir(fp) | ||
|
||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
if err := os.MkdirAll(dir, 0755); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
f, err := os.Create(fp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fn := template.FuncMap{ | ||
"dehyphen": func(s string) string { | ||
return strings.ReplaceAll(s, "-", "") | ||
}, | ||
"lower": strings.ToLower, | ||
"title": func(s string) string { | ||
return strings.ReplaceAll(strings.Title(s), "-", "") | ||
}, | ||
} | ||
t, err := template.New(fp).Funcs(fn).Parse(file.Template) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = t.Execute(f, g.opts) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// New returns a new generator struct. | ||
func New(opts ...Option) Generator { | ||
var options Options | ||
for _, o := range opts { | ||
o(&options) | ||
} | ||
|
||
return &generator{ | ||
opts: options, | ||
} | ||
} |
Oops, something went wrong.