Skip to content

Commit

Permalink
Cleanup (#3)
Browse files Browse the repository at this point in the history
* cleanup argument parsing to remove duplicates
  • Loading branch information
sigmonsays authored Feb 24, 2024
1 parent 6b9ac13 commit 4a6ba6a
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 216 deletions.
7 changes: 7 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ use nix

export WORKSPACE="$(pwd)"
export GOFLAGS="-count=1"

PATH_add "$(pwd)/dev"

export SERVICE_DIR="/tmp/services/sv"
export ACTIVE_SERVICE_DIR="/tmp/services/active"
export LOG_DIR="/tmp/services/log"

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
manage runit

# features

- manage multiple service matching via patterns
- easily create and manage runit service
- import/export service configuration
Expand Down
169 changes: 15 additions & 154 deletions cmd/runitcmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ import (
"github.com/urfave/cli/v2"
)

func makeCommand(name string, fn func(*cli.Context) error) *cli.Command {
func makeCommand(app *Application, name, action, description string) *cli.Command {
if action == "" {
action = name
}
cmd := &cli.Command{
Name: name,
Usage: name + " a service",
Description: name + " a service",
Action: fn,
Description: description,
Action: app.MakeCommandFn(action),
}
return cmd

}

func (app *Application) MakeCommandFn(action string) func(*cli.Context) error {
fn := func(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, action)
}
return nil
}
return fn
}
func (app *Application) MatchingServices(c *cli.Context) []*runit.Service {
services := make([]*runit.Service, 0)
args := c.Args()
Expand Down Expand Up @@ -143,153 +154,3 @@ func (app *Application) runCommand(name, action string) error {
}
return err
}

// some helpful commands
func (app *Application) Delete(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "delete")
}
return nil
}
func (app *Application) Activate(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "activate")
}
return nil
}
func (app *Application) Deactivate(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "deactivate")
}
return nil
}
func (app *Application) Enable(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "enable")
}
return nil
}
func (app *Application) Disable(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "disable")
}
return nil
}
func (app *Application) Reset(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "reset")
}
return nil
}

// sv commands
func (app *Application) Up(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "up")
}
return nil
}
func (app *Application) Down(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "down")
}
return nil
}
func (app *Application) Once(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "once")
}
return nil
}
func (app *Application) Pause(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "pause")
}
return nil
}
func (app *Application) Cont(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "cont")
}
return nil
}
func (app *Application) Hup(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "hup")
}
return nil
}
func (app *Application) Alarm(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "alarm")
}
return nil
}
func (app *Application) Interrupt(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "interrupt")
}
return nil
}
func (app *Application) Quit(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "quit")
}
return nil
}
func (app *Application) Usr1(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "1")
}
return nil
}
func (app *Application) Usr2(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "2")
}
return nil
}
func (app *Application) Term(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "term")
}
return nil
}
func (app *Application) Kill(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "kill")
}
return nil
}

// lsb compatible
func (app *Application) Start(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "start")
}
return nil
}
func (app *Application) Stop(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "stop")
}
return nil
}
func (app *Application) Reload(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "reload")
}
return nil
}
func (app *Application) Restart(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "restart")
}
return nil
}
func (app *Application) Shutdown(c *cli.Context) error {
for _, service := range app.MatchingServices(c) {
app.runCommand(service.Name, "shutdown")
}
return nil
}
64 changes: 37 additions & 27 deletions cmd/runitcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ func main() {
Usage: "change log level",
},
&cli.StringFlag{
Name: "service-dir",
Usage: "change service dir",
Name: "log-dir",
Usage: "change service dir",
EnvVars: []string{"LOG_DIR"},
Value: runit.DefaultLogDir,
},
&cli.StringFlag{
Name: "active-dir",
Usage: "change active service dir",
Name: "service-dir",
Usage: "change service dir",
EnvVars: []string{"SERVICE_DIR"},
},
&cli.StringFlag{
Name: "active-dir",
Usage: "change active service dir",
EnvVars: []string{"ACTIVE_SERVICE_DIR"},
},
}

Expand Down Expand Up @@ -141,33 +149,35 @@ func main() {
initExport(app),

// more commands
makeCommand("delete", app.Delete),
makeCommand("activate", app.Activate),
makeCommand("deactivate", app.Deactivate),
makeCommand("enable", app.Enable),
makeCommand("disable", app.Disable),
makeCommand("reset", app.Reset),
makeCommand(app, "delete", "", "delete service"),
makeCommand(app, "activate", "", "create service symlink"),
makeCommand(app, "deactivate", "", "delete service symlink"),
makeCommand(app, "enable", "", "enable service at boot"),
makeCommand(app, "disable", "", "disable service at boot"),
makeCommand(app, "reset", "", "reset service state"),

// 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),
makeCommand(app, "up", "", "bring service up"),
makeCommand(app, "down", "", "bring service down"),
makeCommand(app, "pause", "", "pause service"),

// signals
makeCommand(app, "cont", "", "send service CONT signal"),
makeCommand(app, "hup", "", "send service HUP signal"),
makeCommand(app, "alarm", "", "send service ALRM signal"),
makeCommand(app, "interrupt", "", "send service INT signal"),
makeCommand(app, "quit", "", "send service QUIT signal"),
makeCommand(app, "usr1", "1", "send service USR1 signal"),
makeCommand(app, "usr2", "2", "send service USR2 signal"),
makeCommand(app, "term", "", "send service TERM signal"),
makeCommand(app, "kill", "", "send service KILL signal"),

// lsb
makeCommand("start", app.Start),
makeCommand("stop", app.Stop),
makeCommand("reload", app.Reload),
makeCommand("restart", app.Restart),
makeCommand("shutdown", app.Shutdown),
makeCommand(app, "start", "", "start service"),
makeCommand(app, "stop", "", "stop service"),
makeCommand(app, "reload", "", "reload service config"),
makeCommand(app, "restart", "", "restsart service"),
makeCommand(app, "shutdown", "", "shutdown service"),
}

app.Run(os.Args)
Expand Down
35 changes: 5 additions & 30 deletions cmd/runitcmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,10 @@ func initSetup(app *Application) *cli.Command {
usage := "setup a service"

flags := []cli.Flag{
&cli.StringFlag{
Name: "log-level, l",
Usage: "log level",
Value: "warn",
},
&cli.BoolFlag{
Name: "verbose, v",
Usage: "be verbose",
},
&cli.StringFlag{
Name: "service-dir",
Usage: "service directory",
Value: runit.DefaultServiceDir,
},
&cli.StringFlag{
Name: "active-service-dir",
Usage: "active service directory",
Value: runit.DefaultActiveDir,
},
&cli.StringFlag{
Name: "log-dir",
Usage: "log to directory",
},
&cli.BoolFlag{
Name: "enable, e",
Usage: "setup service and enable it",
Expand Down Expand Up @@ -88,10 +69,9 @@ func initSetup(app *Application) *cli.Command {
}

func (app *Application) Setup(c *cli.Context) error {
log_level := c.String("log-level")
verbose := c.Bool("verbose")
service_dir := c.String("service-dir")
active_dir := c.String("active-service-dir")
active_dir := c.String("active-dir")
log_dir := c.String("log-dir")
enable := c.Bool("enable")
disable := c.Bool("disable")
Expand All @@ -106,13 +86,12 @@ func (app *Application) Setup(c *cli.Context) error {
args := c.Args()
name := args.First()

log.Tracef("setup service-dir:%s active-dir:%s log-dir:%s",
service_dir, active_dir, log_dir)

if verbose {
gologging.SetLogLevel("trace")
}
if log_level != "" {
gologging.SetLogLevel(log_level)
}

// template does nothing

// make the runit api locally for these flags to function
Expand Down Expand Up @@ -141,11 +120,7 @@ func (app *Application) Setup(c *cli.Context) error {
log.Tracef("setup %s", name)

lcfg := runit.DefaultLoggingConfig()
if log_dir == "" {
lcfg.Directory = filepath.Join(runit.DefaultLogDir, name)
} else {
lcfg.Directory = log_dir
}
lcfg.Directory = filepath.Join(log_dir, name)

var exec string
if uid != 0 || gid != 0 {
Expand Down
3 changes: 3 additions & 0 deletions dev/gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -x
go install github.com/sigmonsays/runitcmd/...
3 changes: 3 additions & 0 deletions dev/mkdirs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

mkdir -pv $SERVICE_DIR $ACTIVE_SERVICE_DIR $LOG_DIR
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module github.com/sigmonsays/runitcmd
go 1.13

require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sigmonsays/go-logging v0.0.0-20170415192813-93f4813d2694
github.com/urfave/cli/v2 v2.2.0
github.com/urfave/cli/v2 v2.27.1
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
gopkg.in/yaml.v2 v2.2.8
)
Loading

0 comments on commit 4a6ba6a

Please sign in to comment.