Skip to content

Commit

Permalink
Merge pull request #17 from funbox/develop
Browse files Browse the repository at this point in the history
Version 0.9.0.2
  • Loading branch information
andyone authored Apr 3, 2017
2 parents 43442f3 + b13eee3 commit ce08552
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
7 changes: 5 additions & 2 deletions common/init-exporter.spec
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
Summary: Utility for exporting services described by Procfile to init system
Name: init-exporter
Version: 0.9.0
Release: 1%{?dist}
Release: 2%{?dist}
Group: Development/Tools
License: MIT
URL: https://github.com/funbox/init-exporter
Expand All @@ -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.1.1
Version: 0.3.0
Release: 0%{?dist}

%description converter
Expand Down Expand Up @@ -132,6 +132,9 @@ rm -rf %{buildroot}
###############################################################################

%changelog
* Mon Apr 03 2017 Anton Novojilov <[email protected]> - 0.9.0-2
- [converter] Fixed bug with wrong path to working dir

* Mon Apr 03 2017 Anton Novojilov <[email protected]> - 0.9.0-1
- Format converter moved to separate package
- Minor fixes in format converter
Expand Down
62 changes: 52 additions & 10 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// App props
const (
APP = "init-exporter-converter"
VER = "0.2.0"
VER = "0.3.0"
DESC = "Utility for converting procfiles from v1 to v2 format"
)

Expand Down Expand Up @@ -72,29 +72,44 @@ 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 }}
{{ if .HasPreCmd -}}
pre: {{ .PreCmd }}
{{ end -}}
command: {{ .Cmd }}
{{- if .HasPostCmd }}pre: {{ .PostCmd }}{{ end }}
{{- if .Options.IsCustomLogEnabled }}log: {{ .Options.LogPath }}{{ end }}
{{- if .Options.IsEnvSet}}
{{ 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 -}}
{{ end -}}
`

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

type templateData struct {
Config *procfile.Config
Application *procfile.Application
Config *procfile.Config
Application *procfile.Application
HasCustomWorkingDirs bool
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -162,6 +177,8 @@ func process(file string) {

// convert read procfile in v1 format and print v2 data or save it to file
func convert(file string) error {
var hasCustomWorkingDirs bool

config := &procfile.Config{
Name: "",
WorkingDir: knf.GetS(PATHS_WORKING_DIR, "/tmp"),
Expand All @@ -180,10 +197,15 @@ func convert(file string) error {
}

if app.ProcVersion != 1 {
printErrorAndExit("Given procfile already converted to v2 format.")
printErrorAndExit("Given procfile already converted to v2 format")
}

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

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

if err != nil {
return err
Expand Down Expand Up @@ -217,6 +239,26 @@ func renderTemplate(name, templateData string, data interface{}) (string, error)
return buffer.String(), nil
}

// getWorkingDir return path to default working dir and flag
// if custom working dirs is used
func getWorkingDir(app *procfile.Application) (string, bool) {
var dir = ""

for _, service := range app.Services {
if dir == "" {
dir = service.Options.WorkingDir
continue
}

if dir != service.Options.WorkingDir {
return "/tmp", true
}
}

return dir, false
}

// writeData write procfile data to file
func writeData(file, data string) error {
return ioutil.WriteFile(file, []byte(data), 0644)
}
Expand Down

0 comments on commit ce08552

Please sign in to comment.