Skip to content

Commit

Permalink
Merge pull request #26 from josegonzalez/master
Browse files Browse the repository at this point in the history
Release 0.10.1
  • Loading branch information
josegonzalez authored Oct 27, 2020
2 parents f275569 + 89c5f83 commit b7d6bc7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ MAINTAINER_NAME = Jose Diaz-Gonzalez
REPOSITORY = go-procfile-util
HARDWARE = $(shell uname -m)
SYSTEM_NAME = $(shell uname -s | tr '[:upper:]' '[:lower:]')
BASE_VERSION ?= 0.10.0
BASE_VERSION ?= 0.10.1
IMAGE_NAME ?= $(MAINTAINER)/$(REPOSITORY)
PACKAGECLOUD_REPOSITORY ?= dokku/dokku-betafish

Expand Down
16 changes: 8 additions & 8 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"text/template"
)

func exportLaunchd(app string, entries []procfileEntry, formations map[string]formationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
func exportLaunchd(app string, entries []ProcfileEntry, formations map[string]FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
l, err := loadTemplate("launchd", "templates/launchd/launchd.plist.tmpl")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down Expand Up @@ -37,7 +37,7 @@ func exportLaunchd(app string, entries []procfileEntry, formations map[string]fo
return true
}

func exportRunit(app string, entries []procfileEntry, formations map[string]formationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
func exportRunit(app string, entries []ProcfileEntry, formations map[string]FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
r, err := loadTemplate("run", "templates/runit/run.tmpl")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down Expand Up @@ -118,7 +118,7 @@ func exportRunit(app string, entries []procfileEntry, formations map[string]form
return true
}

func exportSystemd(app string, entries []procfileEntry, formations map[string]formationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
func exportSystemd(app string, entries []ProcfileEntry, formations map[string]FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
t, err := loadTemplate("target", "templates/systemd/default/control.target.tmpl")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down Expand Up @@ -165,7 +165,7 @@ func exportSystemd(app string, entries []procfileEntry, formations map[string]fo
return true
}

func exportSystemdUser(app string, entries []procfileEntry, formations map[string]formationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
func exportSystemdUser(app string, entries []ProcfileEntry, formations map[string]FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
s, err := loadTemplate("service", "templates/systemd-user/default/program.service.tmpl")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down Expand Up @@ -197,7 +197,7 @@ func exportSystemdUser(app string, entries []procfileEntry, formations map[strin
return true
}

func exportSysv(app string, entries []procfileEntry, formations map[string]formationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
func exportSysv(app string, entries []ProcfileEntry, formations map[string]FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
l, err := loadTemplate("launchd", "templates/sysv/default/init.sh.tmpl")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down Expand Up @@ -226,7 +226,7 @@ func exportSysv(app string, entries []procfileEntry, formations map[string]forma

return true
}
func exportUpstart(app string, entries []procfileEntry, formations map[string]formationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
func exportUpstart(app string, entries []ProcfileEntry, formations map[string]FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool {
p, err := loadTemplate("program", "templates/upstart/default/program.conf.tmpl")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down Expand Up @@ -277,7 +277,7 @@ func exportUpstart(app string, entries []procfileEntry, formations map[string]fo
config := vars
return writeOutput(c, fmt.Sprintf("%s/etc/init/%s.conf", location, app), config)
}
func processCount(entry procfileEntry, formations map[string]formationEntry) int {
func processCount(entry ProcfileEntry, formations map[string]FormationEntry) int {
count := 0
if f, ok := formations["all"]; ok {
count = f.Count
Expand All @@ -292,7 +292,7 @@ func portFor(processIndex int, instance int, base int) int {
return 5000 + (processIndex * 100) + (instance - 1)
}

func templateVars(app string, entry procfileEntry, processName string, num int, port int, vars map[string]interface{}) map[string]interface{} {
func templateVars(app string, entry ProcfileEntry, processName string, num int, port int, vars map[string]interface{}) map[string]interface{} {
config := vars
config["args"] = entry.args()
config["args_escaped"] = entry.argsEscaped()
Expand Down
72 changes: 38 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@ import (
"gopkg.in/alessio/shellescape.v1"
)

type procfileEntry struct {
// ProcfileEntry is a struct containing a process type and the corresponding command
type ProcfileEntry struct {
Name string
Command string
}

type formationEntry struct {
// FormationEntry is a struct containing a process type and the corresponding count
type FormationEntry struct {
Name string
Count int
}

type exportFunc func(string, []procfileEntry, map[string]formationEntry, string, int, map[string]interface{}) bool
type exportFunc func(string, []ProcfileEntry, map[string]FormationEntry, string, int, map[string]interface{}) bool

func (p *procfileEntry) commandList() []string {
func (p *ProcfileEntry) commandList() []string {
return strings.Fields(p.Command)
}

func (p *procfileEntry) program() string {
func (p *ProcfileEntry) program() string {
return strings.Fields(p.Command)[0]
}

func (p *procfileEntry) args() string {
func (p *ProcfileEntry) args() string {
return strings.Join(strings.Fields(p.Command)[1:], " ")
}

func (p *procfileEntry) argsEscaped() string {
func (p *ProcfileEntry) argsEscaped() string {
return shellescape.Quote(p.args())
}

Expand Down Expand Up @@ -72,7 +74,8 @@ func infoMessage(message string) {
logMessage(message, "info")
}

func getProcfile(path string) (string, error) {
// GetProcfileContent returns the content at a path as a string
func GetProcfileContent(path string) (string, error) {
debugMessage(fmt.Sprintf("Attempting to read input from file: %v", path))
f, err := os.Open(path)
if err != nil {
Expand All @@ -97,7 +100,7 @@ func getProcfile(path string) (string, error) {
return strings.Join(lines, "\n"), err
}

func outputProcfile(path string, writePath string, delimiter string, stdout bool, entries []procfileEntry) bool {
func outputProcfile(path string, writePath string, delimiter string, stdout bool, entries []ProcfileEntry) bool {
if writePath != "" && stdout {
fmt.Fprintf(os.Stderr, "cannot specify both --stdout and --write-path flags\n")
return false
Expand Down Expand Up @@ -126,7 +129,7 @@ func outputProcfile(path string, writePath string, delimiter string, stdout bool
return true
}

func writeProcfile(path string, delimiter string, entries []procfileEntry) error {
func writeProcfile(path string, delimiter string, entries []ProcfileEntry) error {
debugMessage(fmt.Sprintf("Attempting to write output to file: %v", path))
file, err := os.Create(path)
if err != nil {
Expand All @@ -141,18 +144,19 @@ func writeProcfile(path string, delimiter string, entries []procfileEntry) error
return w.Flush()
}

func parseProcfile(path string, delimiter string, strict bool) ([]procfileEntry, error) {
var entries []procfileEntry
text, err := getProcfile(path)
func parseProcfile(path string, delimiter string, strict bool) ([]ProcfileEntry, error) {
var entries []ProcfileEntry
text, err := GetProcfileContent(path)
if err != nil {
return entries, err
}

return ParseProcfile(text, delimiter, strict)
}

func ParseProcfile(text string, delimiter string, strict bool) ([]procfileEntry, error) {
var entries []procfileEntry
// ParseProcfile parses text as a procfile and returns a list of procfile entries
func ParseProcfile(text string, delimiter string, strict bool) ([]ProcfileEntry, error) {
var entries []ProcfileEntry
reCmd, _ := regexp.Compile(`^([a-z0-9]([-a-z0-9]*[a-z0-9])?)` + delimiter + `\s*(.+)$`)
reOldCmd, _ := regexp.Compile(`^([A-Za-z0-9_-]+)` + delimiter + `\s*(.+)$`)

Expand Down Expand Up @@ -220,7 +224,7 @@ func ParseProcfile(text string, delimiter string, strict bool) ([]procfileEntry,
return entries, fmt.Errorf("no command specified, line %d", lineNumber)
}

entries = append(entries, procfileEntry{name, cmd})
entries = append(entries, ProcfileEntry{name, cmd})
}

if scanner.Err() != nil {
Expand All @@ -238,7 +242,7 @@ func ParseProcfile(text string, delimiter string, strict bool) ([]procfileEntry,
return entries, nil
}

func expandEnv(e procfileEntry, envPath string, allowEnv bool, defaultPort int) (string, error) {
func expandEnv(e ProcfileEntry, envPath string, allowEnv bool, defaultPort int) (string, error) {
baseExpandFunc := func(key string) string {
if key == "PS" {
return os.Getenv("PS")
Expand Down Expand Up @@ -298,7 +302,7 @@ func expandEnv(e procfileEntry, envPath string, allowEnv bool, defaultPort int)
return os.Expand(s, expandFunc), nil
}

func checkCommand(entries []procfileEntry) bool {
func checkCommand(entries []ProcfileEntry) bool {
if len(entries) == 0 {
fmt.Fprintf(os.Stderr, "no processes defined\n")
return false
Expand All @@ -314,7 +318,7 @@ func checkCommand(entries []procfileEntry) bool {
return true
}

func existsCommand(entries []procfileEntry, processType string) bool {
func existsCommand(entries []ProcfileEntry, processType string) bool {
for _, entry := range entries {
if processType == entry.Name {
return true
Expand All @@ -325,9 +329,9 @@ func existsCommand(entries []procfileEntry, processType string) bool {
return false
}

func expandCommand(entries []procfileEntry, envPath string, allowGetenv bool, processType string, defaultPort int, delimiter string) bool {
func expandCommand(entries []ProcfileEntry, envPath string, allowGetenv bool, processType string, defaultPort int, delimiter string) bool {
hasErrors := false
var expandedEntries []procfileEntry
var expandedEntries []ProcfileEntry
for _, entry := range entries {
command, err := expandEnv(entry, envPath, allowGetenv, defaultPort)
if err != nil {
Expand All @@ -352,7 +356,7 @@ func expandCommand(entries []procfileEntry, envPath string, allowGetenv bool, pr
return true
}

func exportCommand(entries []procfileEntry, app string, description string, envPath string, format string, formation string, group string, home string, limitCoredump string, limitCputime string, limitData string, limitFileSize string, limitLockedMemory string, limitOpenFiles string, limitUserProcesses string, limitPhysicalMemory string, limitStackSize string, location string, logPath string, nice string, prestart string, workingDirectoryPath string, runPath string, timeout int, processUser string, defaultPort int) bool {
func exportCommand(entries []ProcfileEntry, app string, description string, envPath string, format string, formation string, group string, home string, limitCoredump string, limitCputime string, limitData string, limitFileSize string, limitLockedMemory string, limitOpenFiles string, limitUserProcesses string, limitPhysicalMemory string, limitStackSize string, location string, logPath string, nice string, prestart string, workingDirectoryPath string, runPath string, timeout int, processUser string, defaultPort int) bool {
if format == "" {
fmt.Fprintf(os.Stderr, "no format specified\n")
return false
Expand Down Expand Up @@ -480,8 +484,8 @@ func ulimitShell(limitCoredump string, limitCputime string, limitData string, li
return strings.Join(s, "\n")
}

func parseFormation(formation string) (map[string]formationEntry, error) {
entries := make(map[string]formationEntry)
func parseFormation(formation string) (map[string]FormationEntry, error) {
entries := make(map[string]FormationEntry)
for _, formation := range strings.Split(formation, ",") {
parts := strings.Split(formation, "=")
if len(parts) != 2 {
Expand All @@ -493,7 +497,7 @@ func parseFormation(formation string) (map[string]formationEntry, error) {
return entries, fmt.Errorf("invalid formation: %s", err)
}

entries[parts[0]] = formationEntry{
entries[parts[0]] = FormationEntry{
Name: parts[0],
Count: i,
}
Expand All @@ -502,8 +506,8 @@ func parseFormation(formation string) (map[string]formationEntry, error) {
return entries, nil
}

func deleteCommand(entries []procfileEntry, processType string, writePath string, stdout bool, delimiter string, path string) bool {
var validEntries []procfileEntry
func deleteCommand(entries []ProcfileEntry, processType string, writePath string, stdout bool, delimiter string, path string) bool {
var validEntries []ProcfileEntry
for _, entry := range entries {
if processType == entry.Name {
continue
Expand All @@ -514,16 +518,16 @@ func deleteCommand(entries []procfileEntry, processType string, writePath string
return outputProcfile(path, writePath, delimiter, stdout, validEntries)
}

func listCommand(entries []procfileEntry) bool {
func listCommand(entries []ProcfileEntry) bool {
for _, entry := range entries {
fmt.Printf("%v\n", entry.Name)
}
return true
}

func setCommand(entries []procfileEntry, processType string, command string, writePath string, stdout bool, delimiter string, path string) bool {
var validEntries []procfileEntry
validEntries = append(validEntries, procfileEntry{processType, command})
func setCommand(entries []ProcfileEntry, processType string, command string, writePath string, stdout bool, delimiter string, path string) bool {
var validEntries []ProcfileEntry
validEntries = append(validEntries, ProcfileEntry{processType, command})
for _, entry := range entries {
if processType == entry.Name {
continue
Expand All @@ -534,16 +538,16 @@ func setCommand(entries []procfileEntry, processType string, command string, wri
return outputProcfile(path, writePath, delimiter, stdout, validEntries)
}

func showCommand(entries []procfileEntry, envPath string, allowGetenv bool, processType string, defaultPort int) bool {
var foundEntry procfileEntry
func showCommand(entries []ProcfileEntry, envPath string, allowGetenv bool, processType string, defaultPort int) bool {
var foundEntry ProcfileEntry
for _, entry := range entries {
if processType == entry.Name {
foundEntry = entry
break
}
}

if foundEntry == (procfileEntry{}) {
if foundEntry == (ProcfileEntry{}) {
fmt.Fprintf(os.Stderr, "no matching process entry found\n")
return false
}
Expand Down

0 comments on commit b7d6bc7

Please sign in to comment.