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
|
package go_swagger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
|
|
||||||
@ -13,50 +11,39 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type launcherProcess struct {
|
|
||||||
Binary string
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type launcherTemplate struct {
|
type launcherTemplate struct {
|
||||||
APIs []*launcherProcess
|
APIs []*models.Process
|
||||||
Workers []*launcherProcess
|
Crons []*models.Process
|
||||||
|
Workers []*models.Process
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateLauncher will generate a launcher.sh based on the files found in cmd directory.
|
// GenerateLauncher will generate a launcher.sh based on the files found in cmd directory.
|
||||||
func (a APIType) GenerateLauncher(path string, config *models.Config) error {
|
func (a APIType) GenerateLauncher(path string, config *models.Config) error {
|
||||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||||
|
|
||||||
cmdPath := filepath.Join(path, "cmd")
|
|
||||||
templatePath := filepath.Join(templateDirectory, "launcher.sh.tmpl")
|
templatePath := filepath.Join(templateDirectory, "launcher.sh.tmpl")
|
||||||
savePath := filepath.Join(path, "launcher.sh")
|
savePath := filepath.Join(path, "launcher.sh")
|
||||||
|
|
||||||
data := launcherTemplate{
|
data := launcherTemplate{
|
||||||
APIs: []*launcherProcess{},
|
APIs: []*models.Process{},
|
||||||
Workers: []*launcherProcess{},
|
Crons: []*models.Process{},
|
||||||
|
Workers: []*models.Process{},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdDirectories, err := filepath.Glob(filepath.Join(cmdPath, "*"))
|
processes, err := a.getProcesses(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail to list cmd directory")
|
log.Error("Fail to list processes")
|
||||||
return errors.Trace(err)
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, absDir := range cmdDirectories {
|
for _, process := range processes {
|
||||||
dir := strings.TrimPrefix(absDir, fmt.Sprintf("%s%c", cmdPath, filepath.Separator))
|
switch process.Type {
|
||||||
|
case models.ProcessType_API:
|
||||||
// if we have a suffix -server or -api we are on an API.
|
data.APIs = append(data.APIs, process)
|
||||||
// if we have a prefix worker- we are on a worker.
|
case models.ProcessType_CRON:
|
||||||
if strings.HasSuffix(dir, "-server") || strings.HasSuffix(dir, "-api") {
|
data.Crons = append(data.Crons, process)
|
||||||
data.APIs = append(data.APIs, &launcherProcess{
|
case models.ProcessType_WORKER:
|
||||||
Binary: dir,
|
data.Workers = append(data.Workers, process)
|
||||||
Name: dir,
|
|
||||||
})
|
|
||||||
} else if strings.HasPrefix(dir, "worker-") {
|
|
||||||
data.APIs = append(data.Workers, &launcherProcess{
|
|
||||||
Binary: dir,
|
|
||||||
Name: strings.TrimPrefix(dir, "worker-"),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
// APITypeName is a type used as an enum for possible API Type Name.
|
||||||
type APITypeName string
|
type APITypeName string
|
||||||
|
|
||||||
//nolint:exported // keeping the enum simple and redeable.
|
//nolint:exported // keeping the enum simple and readable.
|
||||||
const (
|
const (
|
||||||
APITypeName_GIN_GONIC APITypeName = "Gin Gonic"
|
APITypeName_GIN_GONIC APITypeName = "Gin Gonic"
|
||||||
APITypeName_GO_SWAGGER APITypeName = "Go Swagger"
|
APITypeName_GO_SWAGGER APITypeName = "Go Swagger"
|
||||||
|
@ -3,7 +3,7 @@ package models
|
|||||||
// DependencyName is a type used as an enum for possible Dependency Name.
|
// DependencyName is a type used as an enum for possible Dependency Name.
|
||||||
type DependencyName string
|
type DependencyName string
|
||||||
|
|
||||||
//nolint:exported // keeping the enum simple and redeable.
|
//nolint:exported // keeping the enum simple and readable.
|
||||||
const (
|
const (
|
||||||
DependencyName_GIT DependencyName = "Git"
|
DependencyName_GIT DependencyName = "Git"
|
||||||
DependencyName_GOLANG DependencyName = "Golang"
|
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 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
|
# 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
|
case "$APP_TYPE" in
|
||||||
{{ if eq (len .APIs) 1 }}
|
{{ if eq (len .APIs) 1 }}
|
||||||
@ -33,9 +33,29 @@ case "$APP_TYPE" in
|
|||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
{{- if ne (len .Crons) 0 }}
|
||||||
|
# Crons
|
||||||
|
cron)
|
||||||
|
case "$CRON_NAME" in
|
||||||
|
|
||||||
|
{{range .Crons}}
|
||||||
|
# Cron {{.Name}}
|
||||||
|
{{.Name}}) exec /app/{{.Binary}} ;;
|
||||||
|
{{end}}
|
||||||
|
|
||||||
# otherwise
|
# otherwise
|
||||||
*)
|
*)
|
||||||
echo "** invalid APP_TYPE='$APP_TYPE' ! Please use API or WORKER!"
|
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, WORKER or CRON!"
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
Loading…
Reference in New Issue
Block a user