Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
add language detection conditionals (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
vipul-rawat authored Jul 26, 2024
1 parent 41da2d7 commit 798e4b1
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
}
Expand All @@ -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,"+
Expand Down Expand Up @@ -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
}

0 comments on commit 798e4b1

Please sign in to comment.