Skip to content

Commit

Permalink
add commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmonsays committed Jan 18, 2016
1 parent 1ad1c61 commit 835c7e1
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
175 changes: 175 additions & 0 deletions cmd/runitcmd/command.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
22 changes: 22 additions & 0 deletions cmd/runitcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 835c7e1

Please sign in to comment.