diff --git a/helpers/api_types/go_swagger/generate_launcher.go b/helpers/api_types/go_swagger/generate_launcher.go index b347ef5..8a0d0f8 100644 --- a/helpers/api_types/go_swagger/generate_launcher.go +++ b/helpers/api_types/go_swagger/generate_launcher.go @@ -1,9 +1,7 @@ package go_swagger import ( - "fmt" "path/filepath" - "strings" "github.com/juju/errors" @@ -13,50 +11,39 @@ import ( log "github.com/sirupsen/logrus" ) -type launcherProcess struct { - Binary string - Name string -} - type launcherTemplate struct { - APIs []*launcherProcess - Workers []*launcherProcess + APIs []*models.Process + Crons []*models.Process + Workers []*models.Process } // GenerateLauncher will generate a launcher.sh based on the files found in cmd directory. func (a APIType) GenerateLauncher(path string, config *models.Config) error { log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName()) - cmdPath := filepath.Join(path, "cmd") templatePath := filepath.Join(templateDirectory, "launcher.sh.tmpl") savePath := filepath.Join(path, "launcher.sh") data := launcherTemplate{ - APIs: []*launcherProcess{}, - Workers: []*launcherProcess{}, + APIs: []*models.Process{}, + Crons: []*models.Process{}, + Workers: []*models.Process{}, } - cmdDirectories, err := filepath.Glob(filepath.Join(cmdPath, "*")) + processes, err := a.getProcesses(path) if err != nil { - log.Error("Fail to list cmd directory") + log.Error("Fail to list processes") return errors.Trace(err) } - for _, absDir := range cmdDirectories { - dir := strings.TrimPrefix(absDir, fmt.Sprintf("%s%c", cmdPath, filepath.Separator)) - - // if we have a suffix -server or -api we are on an API. - // if we have a prefix worker- we are on a worker. - if strings.HasSuffix(dir, "-server") || strings.HasSuffix(dir, "-api") { - data.APIs = append(data.APIs, &launcherProcess{ - Binary: dir, - Name: dir, - }) - } else if strings.HasPrefix(dir, "worker-") { - data.APIs = append(data.Workers, &launcherProcess{ - Binary: dir, - Name: strings.TrimPrefix(dir, "worker-"), - }) + for _, process := range processes { + switch process.Type { + case models.ProcessType_API: + data.APIs = append(data.APIs, process) + case models.ProcessType_CRON: + data.Crons = append(data.Crons, process) + case models.ProcessType_WORKER: + data.Workers = append(data.Workers, process) } } diff --git a/helpers/api_types/go_swagger/processes.go b/helpers/api_types/go_swagger/processes.go new file mode 100644 index 0000000..32e2299 --- /dev/null +++ b/helpers/api_types/go_swagger/processes.go @@ -0,0 +1,55 @@ +package go_swagger + +import ( + "fmt" + "path/filepath" + "strings" + + "github.com/juju/errors" + + "git.home.m-and-m.ovh/mderasse/gouick/models" + + log "github.com/sirupsen/logrus" +) + +// getProcesses will read cmd directory and guess the app processes. +func (a APIType) getProcesses(path string) ([]*models.Process, error) { + + cmdPath := filepath.Join(path, "cmd") + cmdDirectories, err := filepath.Glob(filepath.Join(cmdPath, "*")) + if err != nil { + log.Error("Fail to list cmd directory") + return nil, errors.Trace(err) + } + + processes := make([]*models.Process, 0) + + for _, absDir := range cmdDirectories { + dir := strings.TrimPrefix(absDir, fmt.Sprintf("%s%c", cmdPath, filepath.Separator)) + + // if we have a suffix -server or -api we are on an API. + // if we have a prefix worker- we are on a worker. + // if we have a prefix cron- we are on a cron. + if strings.HasSuffix(dir, "-server") || strings.HasSuffix(dir, "-api") { + processes = append(processes, &models.Process{ + Binary: dir, + Name: dir, + Type: models.ProcessType_API, + }) + } else if strings.HasPrefix(dir, "worker-") { + processes = append(processes, &models.Process{ + Binary: dir, + Name: strings.TrimPrefix(dir, "worker-"), + Type: models.ProcessType_WORKER, + }) + } else if strings.HasPrefix(dir, "cron-") { + processes = append(processes, &models.Process{ + Binary: dir, + Name: strings.TrimPrefix(dir, "cron-"), + Type: models.ProcessType_CRON, + }) + } + } + + return processes, nil +} diff --git a/models/api_type_name_enum.go b/models/api_type_name_enum.go index b80cf10..f331d03 100644 --- a/models/api_type_name_enum.go +++ b/models/api_type_name_enum.go @@ -9,7 +9,7 @@ import ( // APITypeName is a type used as an enum for possible API Type Name. type APITypeName string -//nolint:exported // keeping the enum simple and redeable. +//nolint:exported // keeping the enum simple and readable. const ( APITypeName_GIN_GONIC APITypeName = "Gin Gonic" APITypeName_GO_SWAGGER APITypeName = "Go Swagger" diff --git a/models/dependency_name_enum.go b/models/dependency_name_enum.go index 5696d00..ccebdc6 100644 --- a/models/dependency_name_enum.go +++ b/models/dependency_name_enum.go @@ -3,7 +3,7 @@ package models // DependencyName is a type used as an enum for possible Dependency Name. type DependencyName string -//nolint:exported // keeping the enum simple and redeable. +//nolint:exported // keeping the enum simple and readable. const ( DependencyName_GIT DependencyName = "Git" DependencyName_GOLANG DependencyName = "Golang" diff --git a/models/process.go b/models/process.go new file mode 100644 index 0000000..f263b53 --- /dev/null +++ b/models/process.go @@ -0,0 +1,18 @@ +package models + +// Process represent a cmd process. +type Process struct { + Binary string + Name string + Type ProcessType +} + +// ProcessType is a type used of process that can be launched with the launcher. +type ProcessType string + +//nolint:exported // keeping the enum simple and readable. +const ( + ProcessType_API ProcessType = "api" + ProcessType_CRON ProcessType = "cron" + ProcessType_WORKER ProcessType = "worker" +) diff --git a/templates/go-swagger/launcher.sh.tmpl b/templates/go-swagger/launcher.sh.tmpl index e7261df..7e59937 100644 --- a/templates/go-swagger/launcher.sh.tmpl +++ b/templates/go-swagger/launcher.sh.tmpl @@ -2,7 +2,7 @@ # the launcher file will launch the correct app / worker based on the given env variable # the main goal of that launcher is to be used as a Docker entrypoint -# APP_TYPE: Can be api or worker +# APP_TYPE: Can be api, worker or cron case "$APP_TYPE" in {{ if eq (len .APIs) 1 }} @@ -33,9 +33,29 @@ case "$APP_TYPE" in esac ;; {{end}} + +{{- if ne (len .Crons) 0 }} + # Crons + cron) + case "$CRON_NAME" in + + {{range .Crons}} + # Cron {{.Name}} + {{.Name}}) exec /app/{{.Binary}} ;; + {{end}} + + # otherwise + *) + echo "** invalid CRON_NAME='$CRON_NAME'. Please use a valid cron name!" + exit 1 + ;; + esac + ;; +{{end}} + # otherwise *) - echo "** invalid APP_TYPE='$APP_TYPE' ! Please use API or WORKER!" + echo "** invalid APP_TYPE='$APP_TYPE' ! Please use API, WORKER or CRON!" exit 1 ;; esac