Skip to content

Commit

Permalink
add force and restart flags for creation
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmonsays committed Jan 19, 2016
1 parent c416723 commit e038a47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
17 changes: 16 additions & 1 deletion cmd/runitcmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func initCreate(app *Application) cli.Command {
Name: "disabled, d",
Usage: "create service but do not enable it",
},
cli.BoolFlag{
Name: "force, f",
Usage: "force update the service if it already exists",
},
cli.BoolFlag{
Name: "restart, r",
Usage: "restart the service after creation if it already exists",
},
}

cmd := cli.Command{
Expand All @@ -40,6 +48,8 @@ func (app *Application) Create(c *cli.Context) {
args := c.Args()
name := args.First()
exec := c.String("exec")
force := c.Bool("force")
restart := c.Bool("restart")
disabled := c.Bool("disabled")
log_dir := c.String("log-dir")

Expand Down Expand Up @@ -69,7 +79,12 @@ func (app *Application) Create(c *cli.Context) {
RedirectStderr: true,
}

err := app.Runit.Create(cfg)
copts := &runit.CreateOptions{
Force: force,
Restart: restart,
}

err := app.Runit.Create2(cfg, copts)
if err != nil {
log.Warnf("Create %s: %s", name, err)
}
Expand Down
30 changes: 29 additions & 1 deletion runit/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,41 @@ import (
"fmt"
)

func DefaultCreateOptions() *CreateOptions {
return &CreateOptions{}
}

type CreateOptions struct {
Force bool
Restart bool
}

func (runit *Runit) Create(cfg *ServiceConfig) error {

return runit.Create2(cfg, nil)

}

func (runit *Runit) Create2(cfg *ServiceConfig, opts *CreateOptions) error {

if opts == nil {
opts = DefaultCreateOptions()
}
sv := runit.GetService(cfg.Name)
if sv.Exists() {

if opts.Force == false && sv.Exists() {
return fmt.Errorf("service exists")
}

err := runit.Apply(cfg)
if err != nil {
return err
}

if opts.Restart {
err = runit.Restart(cfg.Name)
}

return err

}

0 comments on commit e038a47

Please sign in to comment.