Skip to content

Commit

Permalink
be forgiving on export if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmonsays committed Jan 19, 2016
1 parent 076f21f commit e808308
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
18 changes: 16 additions & 2 deletions cmd/runitcmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ func initExport(app *Application) cli.Command {
description := "export service"
usage := "export service"

flags := []cli.Flag{}
flags := []cli.Flag{
cli.BoolFlag{
Name: "forgiving, f",
Usage: "be forgiving and try to parse the runit files",
},
}

cmd := cli.Command{
Name: "export",
Expand All @@ -23,13 +28,22 @@ func initExport(app *Application) cli.Command {
}

func (app *Application) Export(c *cli.Context) {
forgiving := c.Bool("forgiving")

for _, service := range app.MatchingServices(c) {

cfg, err := app.Runit.Export(service.Name)
if err != nil {
if forgiving == false && err != nil {
log.Warnf("export %s: %s", service.Name, err)
continue
}
if forgiving && err != nil {
cfg, err = app.Runit.LoadFromDisk(service.Name)
if err != nil {
log.Warnf("load service config %s: %s", service.Name, err)
continue
}
}

destfile := filepath.Join("./", service.Name+".yaml")
err = cfg.SaveFile(destfile)
Expand Down
42 changes: 42 additions & 0 deletions runit/export.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package runit

import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

func (runit *Runit) Export(name string) (*ServiceConfig, error) {
Expand All @@ -22,3 +26,41 @@ func (runit *Runit) Export(name string) (*ServiceConfig, error) {
return cfg, nil

}

// do the best we can loading service config from disk (for when we do not have a service.yaml file)
// for things we can't parse we will fill in sane defaults
func (runit *Runit) LoadFromDisk(name string) (*ServiceConfig, error) {
sv := runit.GetService(name)
if sv.Exists() == false {
return nil, fmt.Errorf("not found")
}
cfg := &ServiceConfig{
Name: name,
Activated: true,
}

// load the service/run file
runfile := filepath.Join(sv.ServiceDir, "run")
if f, err := os.Open(runfile); err == nil {
fin := bufio.NewReader(f)
for {
line, err := fin.ReadBytes('\n')
if err == io.EOF {
break
}
if err != nil {
log.Warnf("%s", err)
break
}
sline := string(line)
if strings.HasPrefix(sline, "exec ") {
cfg.Exec = sline[5:]
log.Infof("found exec %s", cfg.Exec)
}
}
f.Close()

}

return cfg, nil
}

0 comments on commit e808308

Please sign in to comment.