Skip to content

Commit

Permalink
Replaced text/template by basic procfile rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Apr 24, 2017
1 parent 89c08d9 commit dd5715d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 75 deletions.
2 changes: 1 addition & 1 deletion common/init-exporter.spec
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Utility for exporting services described by Procfile to init system.
%package converter

Summary: Utility for converting procfiles from v1 to v2 format
Version: 0.5.0
Version: 0.6.0
Release: 0%{?dist}

%description converter
Expand Down
134 changes: 60 additions & 74 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package converter
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"runtime"
"text/template"

"pkg.re/essentialkaos/ek.v8/arg"
"pkg.re/essentialkaos/ek.v8/fmtc"
Expand All @@ -26,7 +25,7 @@ import (
// App props
const (
APP = "init-exporter-converter"
VER = "0.5.0"
VER = "0.6.0"
DESC = "Utility for converting procfiles from v1 to v2 format"
)

Expand Down Expand Up @@ -55,61 +54,12 @@ const (

// ////////////////////////////////////////////////////////////////////////////////// //

// PROCFILE_TEMPLATE is template used for generation v2 Procfile
const PROCFILE_TEMPLATE = `version: 2
start_on_runlevel: 2
stop_on_runlevel: 5
{{ if .Config.IsRespawnEnabled -}}
respawn:
count: {{ .Config.RespawnCount }}
interval: {{ .Config.RespawnInterval }}
{{ end -}}
limits:
nofile: {{ .Config.LimitFile }}
nproc: {{ .Config.LimitProc }}
{{ if not .HasCustomWorkingDirs -}}
working_directory: {{ .Config.WorkingDir }}
{{ end -}}
{{- $hasCustomWorkingDirs := .HasCustomWorkingDirs -}}
commands:
{{- range .Application.Services }}
{{ .Name }}:
{{ if .HasPreCmd -}}
pre: {{ .PreCmd }}
{{ end -}}
command: {{ .Cmd }}
{{ if .HasPostCmd -}}
post: {{ .PostCmd }}
{{ end -}}
{{ if $hasCustomWorkingDirs -}}
working_directory: {{ .Options.WorkingDir }}
{{ end -}}
{{ if .Options.IsCustomLogEnabled -}}
log: {{ .Options.LogPath }}
{{ end -}}
{{ if .Options.IsEnvSet -}}
env:
{{- range $k, $v := .Options.Env }}
{{ $k }}: {{ $v -}}
{{ end }}
{{ end -}}
{{ end -}}
`

// DEFAULT_WORKING_DIR is path to default working dir
const DEFAULT_WORKING_DIR = "/tmp"

// ////////////////////////////////////////////////////////////////////////////////// //

type templateData struct {
type procData struct {
Config *procfile.Config
Application *procfile.Application
HasCustomWorkingDirs bool
Expand Down Expand Up @@ -207,41 +157,77 @@ func convert(file string) error {

validateApplication(app)

v2data, err := renderTemplate(
"proc_v2", PROCFILE_TEMPLATE,
&templateData{config, app, hasCustomWorkingDirs},
)

if err != nil {
return err
}
data := renderProcfile(&procData{config, app, hasCustomWorkingDirs})

if !arg.GetB(ARG_IN_PLACE) {
fmtc.Println(v2data)
fmt.Printf(data)
return nil
}

return writeData(file, v2data)
return writeData(file, data)
}

// renderTemplate renders template data
func renderTemplate(name, templateData string, data interface{}) (string, error) {
templ, err := template.New(name).Parse(templateData)
// renderProcfile render procfile
func renderProcfile(data *procData) string {
var result string

result += "version: 2\n"
result += "\n"
result += "start_on_runlevel: 2\n"
result += "stop_on_runlevel: 5\n"
result += "\n"

if data.Config.IsRespawnEnabled {
result += "respawn:\n"
result += fmt.Sprintf(" count: %d\n", data.Config.RespawnCount)
result += fmt.Sprintf(" interval: %d\n", data.Config.RespawnInterval)
result += "\n"
}

if err != nil {
return "", fmtc.Errorf("Can't render template: %v", err)
result += "limits:\n"
result += fmt.Sprintf(" nofile: %d\n", data.Config.LimitFile)
result += fmt.Sprintf(" nproc: %d\n", data.Config.LimitProc)
result += "\n"

if !data.HasCustomWorkingDirs {
result += "working_directory: " + data.Config.WorkingDir + "\n"
result += "\n"
}

var buffer bytes.Buffer
result += "commands:\n"

ct := template.Must(templ, nil)
err = ct.Execute(&buffer, data)
for _, service := range data.Application.Services {
result += " " + service.Name + "\n"

if err != nil {
return "", fmtc.Errorf("Can't render template: %v", err)
if service.HasPreCmd() {
result += " pre: " + service.PreCmd + "\n"
}

result += " command: " + service.Cmd + "\n"

if service.HasPostCmd() {
result += " post: " + service.PostCmd + "\n"
}

if data.HasCustomWorkingDirs {
result += " working_directory: " + service.Options.WorkingDir + "\n"
}

if service.Options.IsCustomLogEnabled() {
result += " log: " + service.Options.LogFile + "\n"
}

if service.Options.IsEnvSet() {
result += " env:\n"
for k, v := range service.Options.Env {
result += fmt.Sprintf(" %s: %s\n", k, v)
}
}

result += "\n"
}

return buffer.String(), nil
return result
}

// getWorkingDir return path to default working dir and flag
Expand Down

0 comments on commit dd5715d

Please sign in to comment.