From 835c7e125416d893209a274fa56b1cf32f3347f6 Mon Sep 17 00:00:00 2001 From: Sig Lange Date: Sun, 17 Jan 2016 22:15:32 -0800 Subject: [PATCH] add commands --- cmd/runitcmd/command.go | 175 ++++++++++++++++++++++++++++++++++++++++ cmd/runitcmd/main.go | 22 +++++ 2 files changed, 197 insertions(+) create mode 100644 cmd/runitcmd/command.go diff --git a/cmd/runitcmd/command.go b/cmd/runitcmd/command.go new file mode 100644 index 0000000..f6aea19 --- /dev/null +++ b/cmd/runitcmd/command.go @@ -0,0 +1,175 @@ +package main + +import ( + "os/exec" + "regexp" + + "github.com/codegangsta/cli" + "github.com/sigmonsays/runitcmd/runit" +) + +type simpleCommand struct { + service string + command string +} + +func (s *simpleCommand) run() error { + + cmdline := []string{ + "sv", + s.command, s.service, + } + log.Tracef("cmdline %s", cmdline) + + cmd := exec.Command(cmdline[0], cmdline[1:]...) + err := cmd.Start() + if err != nil { + return err + } + + err = cmd.Wait() + + return err +} + +func runCommand(service, command string) { + cmd := &simpleCommand{ + service: service, + command: command, + } + err := cmd.run() + if err != nil { + log.Warnf("%s %s: %s", command, service, err) + } +} + +func makeCommand(name string, fn func(*cli.Context)) cli.Command { + cmd := cli.Command{ + Name: name, + Usage: name + " a service", + Description: name + " a service", + Action: fn, + } + return cmd +} + +func (app *Application) MatchingServices(c *cli.Context) []*runit.Service { + services := make([]*runit.Service, 0) + args := c.Args() + + all_services, err := app.Runit.ListServices() + if err != nil { + log.Warnf("ListServices: %s", err) + return nil + } + for n := 0; n < len(args); n++ { + pattern := args.Get(n) + + match, err := regexp.Compile(pattern) + if err != nil { + log.Warnf("pattern %s: %s", pattern, err) + return nil + } + + for _, service := range all_services { + if match.MatchString(service.Name) == false { + continue + } + services = append(services, service) + } + } + return services +} + +func (app *Application) Up(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "up") + } +} +func (app *Application) Down(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "down") + } +} +func (app *Application) Once(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "once") + } +} +func (app *Application) Pause(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "pause") + } +} +func (app *Application) Cont(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "cont") + } +} +func (app *Application) Hup(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "hup") + } +} +func (app *Application) Alarm(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "alarm") + } +} +func (app *Application) Interrupt(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "interrupt") + } +} +func (app *Application) Quit(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "quit") + } +} +func (app *Application) Usr1(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "1") + } +} +func (app *Application) Usr2(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "2") + } +} +func (app *Application) Term(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "term") + } +} +func (app *Application) Kill(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "kill") + } +} + +// lsb compatible +func (app *Application) Start(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "start") + } +} +func (app *Application) Stop(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "stop") + } +} +func (app *Application) Reload(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "reload") + } +} +func (app *Application) Restart(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "restart") + } +} +func (app *Application) Shutdown(c *cli.Context) { + for _, service := range app.MatchingServices(c) { + runCommand(service.Name, "shutdown") + } +} diff --git a/cmd/runitcmd/main.go b/cmd/runitcmd/main.go index e17117e..5bf991c 100644 --- a/cmd/runitcmd/main.go +++ b/cmd/runitcmd/main.go @@ -58,6 +58,28 @@ func main() { app.Commands = []cli.Command{ initList(app), + + // commands + makeCommand("up", app.Up), + makeCommand("down", app.Down), + makeCommand("pause", app.Pause), + + makeCommand("cont", app.Cont), + makeCommand("hup", app.Cont), + makeCommand("alarm", app.Cont), + makeCommand("interrupt", app.Cont), + makeCommand("quit", app.Quit), + makeCommand("usr1", app.Usr1), + makeCommand("usr2", app.Usr2), + makeCommand("term", app.Term), + makeCommand("kill", app.Kill), + + // lsb + makeCommand("start", app.Start), + makeCommand("stop", app.Stop), + makeCommand("reload", app.Reload), + makeCommand("restart", app.Restart), + makeCommand("shutdown", app.Shutdown), } app.Run(os.Args)