Skip to content

Commit

Permalink
commands: add system service commands
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Jul 11, 2023
1 parent 8b5e240 commit 5d3b6a5
Show file tree
Hide file tree
Showing 10 changed files with 774 additions and 23 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/ipfs/go-ipld-format v0.5.0
github.com/ipfs/kubo v0.21.0
github.com/jaevor/go-nanoid v1.3.0
github.com/kardianos/service v1.2.2
github.com/mattn/go-colorable v0.1.4
github.com/muesli/termenv v0.15.1
github.com/multiformats/go-multiaddr v0.9.0
Expand Down Expand Up @@ -97,3 +98,5 @@ require (
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
lukechampine.com/blake3 v1.2.1 // indirect
)

replace github.com/kardianos/service => github.com/djdv/service v1.2.1-0.20230705182121-36b49552050b
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etly
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/djdv/p9 v0.2.1-0.20230601152255-7d74b92b80b0 h1:TmRbQZzEz+AbtudHs+4OtcggEd6mgbcf1UA3DdUMg/M=
github.com/djdv/p9 v0.2.1-0.20230601152255-7d74b92b80b0/go.mod h1:TGzUXNk2SONYuJnhbmn6w308jdHeBqWwQUqr3yng5XQ=
github.com/djdv/service v1.2.1-0.20230705182121-36b49552050b h1:yGeAxK6k9g1N0T33iw2yTlVNel7otQonAAyft46Z59U=
github.com/djdv/service v1.2.1-0.20230705182121-36b49552050b/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 h1:BBso6MBKW8ncyZLv37o+KNyy0HrrHgfnOaGQC2qvN+A=
Expand Down Expand Up @@ -342,6 +344,7 @@ golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
60 changes: 37 additions & 23 deletions internal/commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ const (
errShutdownDisposition = generic.ConstError("invalid shutdown disposition")
)

// Daemon constructs the command which
// hosts the file system service server.
func Daemon() command.Command {
const (
name = daemonCommandName
synopsis = "Host system services."
)
usage := header("File system service daemon.") +
"\n\n" + synopsis
return command.MakeVariadicCommand[daemonOptions](
name, synopsis, usage, daemonExecute,
command.WithSubcommands(Service()),
)
}

func (do *daemonOptions) BindFlags(flagSet *flag.FlagSet) {
const (
verboseName = "verbose"
Expand All @@ -112,9 +127,12 @@ func (do *daemonOptions) BindFlags(flagSet *flag.FlagSet) {
}
return nil
})
}

func bindDaemonFlags(flagSet *flag.FlagSet, options *daemonOptions) {
const serverUsage = "listening socket `maddr`" +
"\ncan be specified multiple times and/or comma separated"
flagSetFunc(flagSet, serverFlagName, serverUsage, do,
flagSetFunc(flagSet, serverFlagName, serverUsage, options,
func(value []multiaddr.Multiaddr, settings *daemonSettings) error {
settings.serverMaddrs = append(settings.serverMaddrs, value...)
return nil
Expand All @@ -129,7 +147,7 @@ func (do *daemonOptions) BindFlags(flagSet *flag.FlagSet) {
exitName = exitAfterFlagName
exitUsage = "check every `interval` (e.g. \"30s\") and shutdown the daemon if its idle"
)
flagSetFunc(flagSet, exitName, exitUsage, do,
flagSetFunc(flagSet, exitName, exitUsage, options,
func(value time.Duration, settings *daemonSettings) error {
settings.exitInterval = value
return nil
Expand All @@ -138,7 +156,7 @@ func (do *daemonOptions) BindFlags(flagSet *flag.FlagSet) {
uidName = apiFlagPrefix + "uid"
uidUsage = "file owner's `uid`"
)
flagSetFunc(flagSet, uidName, uidUsage, do,
flagSetFunc(flagSet, uidName, uidUsage, options,
func(value p9.UID, settings *daemonSettings) error {
settings.nineIDs.uid = value
return nil
Expand All @@ -149,7 +167,7 @@ func (do *daemonOptions) BindFlags(flagSet *flag.FlagSet) {
gidName = apiFlagPrefix + "gid"
gidUsage = "file owner's `gid`"
)
flagSetFunc(flagSet, gidName, gidUsage, do,
flagSetFunc(flagSet, gidName, gidUsage, options,
func(value p9.GID, settings *daemonSettings) error {
settings.nineIDs.gid = value
return nil
Expand All @@ -161,7 +179,7 @@ func (do *daemonOptions) BindFlags(flagSet *flag.FlagSet) {
permissionsUsage = "`permissions` to use when creating service files"
)
apiPermissions := fs.FileMode(apiPermissionsDefault)
flagSetFunc(flagSet, permissionsName, permissionsUsage, do,
flagSetFunc(flagSet, permissionsName, permissionsUsage, options,
func(value string, settings *daemonSettings) error {
permissions, err := parsePOSIXPermissions(apiPermissions, value)
if err != nil {
Expand All @@ -176,13 +194,7 @@ func (do *daemonOptions) BindFlags(flagSet *flag.FlagSet) {
}

func (do daemonOptions) make() (daemonSettings, error) {
settings := daemonSettings{
nineIDs: nineIDs{
uid: apiUIDDefault,
gid: apiGIDDefault,
},
permissions: apiPermissionsDefault,
}
settings := makeDaemonSettings()
if err := generic.ApplyOptions(&settings, do...); err != nil {
return daemonSettings{}, err
}
Expand All @@ -199,26 +211,28 @@ func (do daemonOptions) make() (daemonSettings, error) {
return settings, nil
}

// Daemon constructs the command which
// hosts the file system service server.
func Daemon() command.Command {
const (
name = daemonCommandName
synopsis = "Host system services."
)
usage := header("File system service daemon.") +
"\n\n" + synopsis
return command.MakeVariadicCommand[daemonOptions](name, synopsis, usage, daemonExecute)
func makeDaemonSettings() daemonSettings {
return daemonSettings{
nineIDs: nineIDs{
uid: apiUIDDefault,
gid: apiGIDDefault,
},
permissions: apiPermissionsDefault,
}
}

func daemonExecute(ctx context.Context, options ...daemonOption) error {
settings, err := daemonOptions(options).make()
if err != nil {
return err
}
return daemonRun(ctx, &settings)
}

func daemonRun(ctx context.Context, settings *daemonSettings) error {
dCtx, cancel := context.WithCancel(ctx)
defer cancel()
system, err := newSystem(dCtx, &settings)
system, err := newSystem(dCtx, settings)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 5d3b6a5

Please sign in to comment.