From 798e4b18fff139594ee0e988b8c5dc72210b54d8 Mon Sep 17 00:00:00 2001 From: Vipul Rawat Date: Fri, 26 Jul 2024 11:50:14 +0530 Subject: [PATCH] add language detection conditionals (#9) --- deploy.go | 70 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/deploy.go b/deploy.go index 31a38f8..1f2b5da 100644 --- a/deploy.go +++ b/deploy.go @@ -13,6 +13,12 @@ import ( "kops.dev/internal/templates" ) +const ( + golang = "golang" + java = "java" + js = "js" +) + var ( errDepKeyNotProvided = errors.New("KOPS_DEPLOYMENT_KEY not provided, " + "please download the key form https://kops.dev") @@ -37,20 +43,7 @@ func Deploy(ctx *gofr.Context) (interface{}, error) { if fi != nil { fmt.Println("Dockerfile present, using already created dockerfile") } else { - // removing the cloud-specific logic from cli to hosted service - lang := ctx.Param("lang") - if lang == "" { - ctx.Logger.Errorf("%v", errLanguageNotProvided) - - return nil, errLanguageNotProvided - } - - port := ctx.Param("p") - if port == "" { - port = "8000" - } - - if err := createDockerFile(ctx, lang, port); err != nil { + if err := createDockerFile(ctx); err != nil { return nil, err } } @@ -61,16 +54,34 @@ func Deploy(ctx *gofr.Context) (interface{}, error) { return "Successful", nil } -func createDockerFile(ctx *gofr.Context, lang, port string) error { - var content string +func createDockerFile(ctx *gofr.Context) error { + var content, lang, port string + + // removing the cloud-specific logic from cli to hosted service + lang = ctx.Param("lang") + if lang == "" { + lang = detect() + if lang == "" { + ctx.Logger.Errorf("%v", errLanguageNotProvided) + + return errLanguageNotProvided + } + + fmt.Println("detected language is", lang) + } + + port = ctx.Param("p") + if port == "" { + port = "8000" + } // get the template content for dockerFile based on the language switch strings.ToLower(lang) { - case "go": + case golang: content = templates.Golang - case "java": + case java: content = templates.Java - case "js": + case js: content = templates.Js default: ctx.Logger.Errorf("creating DockerFile for %s is not supported yet,"+ @@ -106,3 +117,24 @@ func createDockerFile(ctx *gofr.Context, lang, port string) error { return nil } + +func detect() string { + switch { + case checkFile("go.mod"): + return golang + case checkFile("package.json"): + return js + case checkFile("pom.xml") || checkFile("build.gradle"): + return java + } + + return "" +} + +func checkFile(fileName string) bool { + if _, err := os.Stat(fileName); err != nil { + return false + } + + return true +}