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:
@ -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
|
||||
}
|
Reference in New Issue
Block a user