diff --git a/cmd/runitcmd/export.go b/cmd/runitcmd/export.go index d307d0f..8199b53 100644 --- a/cmd/runitcmd/export.go +++ b/cmd/runitcmd/export.go @@ -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", @@ -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) diff --git a/runit/export.go b/runit/export.go index b7020f6..262435c 100644 --- a/runit/export.go +++ b/runit/export.go @@ -1,8 +1,12 @@ package runit import ( + "bufio" "fmt" + "io" + "os" "path/filepath" + "strings" ) func (runit *Runit) Export(name string) (*ServiceConfig, error) { @@ -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 +}