feat(go-swagger): Small refacto on launcher
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
cda86019bb
commit
b7f0d796e4
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
55
helpers/api_types/go_swagger/processes.go
Normal file
55
helpers/api_types/go_swagger/processes.go
Normal file
@ -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
|
||||
}
|
@ -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"
|
||||
|
@ -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"
|
||||
|
18
models/process.go
Normal file
18
models/process.go
Normal file
@ -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"
|
||||
)
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user