From ad940974e0e51ab126916537461d71f9ae8ab035 Mon Sep 17 00:00:00 2001 From: Sig Lange Date: Mon, 18 Jan 2016 16:04:16 -0800 Subject: [PATCH] add export --- cmd/runitcmd/export.go | 41 +++++++++++++++++++++++++++++++++++++++++ cmd/runitcmd/main.go | 1 + example/job1.yaml | 10 ++++++++++ runit/export.go | 24 ++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 cmd/runitcmd/export.go create mode 100644 example/job1.yaml create mode 100644 runit/export.go diff --git a/cmd/runitcmd/export.go b/cmd/runitcmd/export.go new file mode 100644 index 0000000..d307d0f --- /dev/null +++ b/cmd/runitcmd/export.go @@ -0,0 +1,41 @@ +package main + +import ( + "path/filepath" + + "github.com/codegangsta/cli" +) + +func initExport(app *Application) cli.Command { + description := "export service" + usage := "export service" + + flags := []cli.Flag{} + + cmd := cli.Command{ + Name: "export", + Usage: usage, + Description: description, + Flags: flags, + Action: app.Export, + } + return cmd +} + +func (app *Application) Export(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + + cfg, err := app.Runit.Export(service.Name) + if err != nil { + log.Warnf("export %s: %s", service.Name, err) + continue + } + + destfile := filepath.Join("./", service.Name+".yaml") + err = cfg.SaveFile(destfile) + if err != nil { + log.Warnf("export %s: %s", service.Name, err) + continue + } + } +} diff --git a/cmd/runitcmd/main.go b/cmd/runitcmd/main.go index 89a0343..eb64862 100644 --- a/cmd/runitcmd/main.go +++ b/cmd/runitcmd/main.go @@ -118,6 +118,7 @@ func main() { initCreate(app), initApply(app), initStatus(app), + initExport(app), // more commands makeCommand("delete", app.Delete), diff --git a/example/job1.yaml b/example/job1.yaml new file mode 100644 index 0000000..207bd4f --- /dev/null +++ b/example/job1.yaml @@ -0,0 +1,10 @@ +name: job1 +exec: sleep 3600 +disabled: false +activated: true +logging: + directory: /var/log/job1 + size: 52428800 + num: 10 + timestamp: 2 +redirect_stderr: true diff --git a/runit/export.go b/runit/export.go new file mode 100644 index 0000000..b7020f6 --- /dev/null +++ b/runit/export.go @@ -0,0 +1,24 @@ +package runit + +import ( + "fmt" + "path/filepath" +) + +func (runit *Runit) Export(name string) (*ServiceConfig, error) { + sv := runit.GetService(name) + if sv.Exists() == false { + return nil, fmt.Errorf("no such service") + } + + configfile := filepath.Join(sv.ServiceDir, "service.yaml") + + cfg := &ServiceConfig{} + + err := cfg.LoadFile(configfile) + if err != nil { + return nil, err + } + return cfg, nil + +}