-
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.
- Loading branch information
1 parent
69056e7
commit 2134195
Showing
3 changed files
with
267 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
type EnumValue struct { | ||
Enum []string | ||
Default string | ||
selected string | ||
} | ||
|
||
func (e *EnumValue) Set(value string) error { | ||
for _, enum := range e.Enum { | ||
if enum == value { | ||
e.selected = value | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("allowed values are %s", strings.Join(e.Enum, ", ")) | ||
} | ||
|
||
func (e EnumValue) String() string { | ||
if e.selected == "" { | ||
return e.Default | ||
} | ||
return e.selected | ||
} | ||
|
||
var ( | ||
apiProjectName string | ||
) | ||
|
||
func validateRun(c *cli.Context) error { | ||
|
||
apiTmpl := &TmplData{ | ||
ApiProjectName: c.String("project-name"), | ||
ApiProtocol: c.String("api-type"), | ||
ApiEndpoints: c.String("api-endpoint"), | ||
LambdaFunctionName: "helloworld", | ||
Language: c.String("language"), | ||
} | ||
|
||
fmt.Println(apiTmpl) | ||
|
||
return nil | ||
} | ||
|
||
func runCLI(args []string) { | ||
app := cli.NewApp() | ||
app.Name = "api-scaffolder" | ||
app.HelpName = "api-scaffolder" | ||
app.UsageText = "api-scaffolder [command] [command options] [arguments...]" | ||
app.EnableBashCompletion = true | ||
app.Usage = "" | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "new-api", | ||
Usage: "Generates a new api project", | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "p, project-name", | ||
Usage: "name of your API project", | ||
Required: true, | ||
Destination: &apiProjectName, | ||
}, | ||
cli.GenericFlag{ | ||
Name: "t, api-type", | ||
Usage: "api type (either rest or websocket)", | ||
Value: &EnumValue{ | ||
Enum: []string{"rest", "websocket"}, | ||
Default: "rest", | ||
}, | ||
}, | ||
cli.GenericFlag{ | ||
Name: "e, api-endpoint", | ||
Usage: "which endpoint type (either regional, edge or private)", | ||
Value: &EnumValue{ | ||
Enum: []string{"regional", "edge", "private"}, | ||
Default: "regional", | ||
}, | ||
}, | ||
cli.GenericFlag{ | ||
Name: "l, language", | ||
Usage: "which language to be used (go, java, node, python, ruby)", | ||
Value: &EnumValue{ | ||
Enum: []string{"go", "java", "node", "python", "ruby"}, | ||
Default: "node", | ||
}, | ||
}, | ||
}, | ||
Action: validateRun, | ||
}, | ||
} | ||
|
||
err := app.Run(args) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(0) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,158 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"text/template" | ||
) | ||
|
||
var ( | ||
defaultAppPath = "src/helloworld/app" | ||
allowedAPIProtocols = []string{"rest", "websocket"} | ||
allowedRestAPIEndpoints = []string{"regional", "edge", "private"} | ||
) | ||
|
||
type TmplData struct { | ||
ApiProtocol string | ||
ApiEndpoints string | ||
LambdaFunctionName string | ||
ApiProjectName string | ||
Language string | ||
} | ||
|
||
type LanguageMapper struct { | ||
AppFile string | ||
DepsFile string | ||
TmplAppVar string | ||
TmplDepsVar string | ||
AppPath string | ||
DepsPath string | ||
} | ||
|
||
var languages = map[string]LanguageMapper{ | ||
"node": LanguageMapper{ | ||
AppFile: "index.js", | ||
DepsFile: "package.json", | ||
TmplAppVar: nodeFunction, | ||
TmplDepsVar: packageJson, | ||
AppPath: defaultAppPath, | ||
DepsPath: defaultAppPath, | ||
}, | ||
"java": LanguageMapper{ | ||
AppFile: "App.java", | ||
DepsFile: "pom.xml", | ||
TmplAppVar: "", | ||
TmplDepsVar: "", | ||
AppPath: defaultAppPath + "/com/api", | ||
DepsPath: defaultAppPath, | ||
}, | ||
"python": LanguageMapper{ | ||
AppFile: "app.py", | ||
DepsFile: "requirements.txt", | ||
TmplAppVar: "", | ||
TmplDepsVar: "", | ||
AppPath: defaultAppPath, | ||
DepsPath: defaultAppPath, | ||
}, | ||
"ruby": LanguageMapper{ | ||
AppFile: "app.rb", | ||
DepsFile: "Gemfile", | ||
TmplAppVar: "", | ||
TmplDepsVar: "", | ||
AppPath: defaultAppPath, | ||
DepsPath: defaultAppPath, | ||
}, | ||
"go": LanguageMapper{ | ||
AppFile: "main.go", | ||
DepsFile: "go.mod", | ||
TmplAppVar: "", | ||
TmplDepsVar: "", | ||
AppPath: defaultAppPath, | ||
DepsPath: defaultAppPath, | ||
}, | ||
} | ||
|
||
func createDir(path string) error { | ||
err := os.MkdirAll(path, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func contains(slice []string, contains string) bool { | ||
for _, item := range slice { | ||
if contains == item { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func createFileFromStruct(languageSpec LanguageMapper) error { | ||
// not pretty, fix later | ||
apa := &TmplData{} | ||
|
||
err := createDir(languageSpec.AppPath) | ||
if err != nil { | ||
return err | ||
} | ||
err = apa.createFileFromTemplate(languageSpec.TmplAppVar, languageSpec.AppPath, languageSpec.AppFile) | ||
if err != nil { | ||
return err | ||
} | ||
err = apa.createFileFromTemplate(languageSpec.TmplDepsVar, languageSpec.DepsPath, languageSpec.DepsFile) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (tmpl *TmplData) createFileFromTemplate(tmplVar string, path string, outName string) error { | ||
t := template.Must(template.New("").Parse(tmplVar)) | ||
var file *os.File | ||
var err error | ||
|
||
if path != "" { | ||
file, err = os.Create(path + "/" + outName) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
file, err = os.Create(outName) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
err = t.Execute(file, tmpl) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
import "os" | ||
|
||
func main() { | ||
|
||
apiTmpl := &TmplData{ | ||
ApiProtocol: "rest", | ||
ApiEndpoints: "regional", | ||
LambdaFunctionName: "helloworld", | ||
ApiProjectName: "Hello-World-API", | ||
Language: "node", | ||
} | ||
|
||
err := apiTmpl.createFileFromTemplate(apiGWConf, "", "apigw.yml") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = apiTmpl.createFileFromTemplate(swagger, "", "swagger-api.yml") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = createFileFromStruct(languages[apiTmpl.Language]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
runCLI(os.Args) | ||
} |
Oops, something went wrong.