diff --git a/internal/commands/mount.go b/internal/commands/mount.go index f75fec08..2555c7f3 100644 --- a/internal/commands/mount.go +++ b/internal/commands/mount.go @@ -272,6 +272,7 @@ func makeHostCommands() []command.Command { var ( commandMakers = []makeCommand{ makeFUSECommand, + makePlan9HostCommand, } commands = make([]command.Command, 0, len(commandMakers)) ) diff --git a/internal/commands/mountpoint.go b/internal/commands/mountpoint.go index f0926863..08f7e0c7 100644 --- a/internal/commands/mountpoint.go +++ b/internal/commands/mountpoint.go @@ -90,6 +90,7 @@ func makeMountPointHosts(path ninePath, autoUnlink bool) mountPointHosts { var ( hostMakers = []makeHostsFunc{ makeFUSEHost, + makePlan9Host, } hosts = make(mountPointHosts, len(hostMakers)) ) diff --git a/internal/commands/mountpoint_9p.go b/internal/commands/mountpoint_9p.go index 4734a4b6..901696a6 100644 --- a/internal/commands/mountpoint_9p.go +++ b/internal/commands/mountpoint_9p.go @@ -8,6 +8,7 @@ import ( "github.com/djdv/go-filesystem-utils/internal/command" "github.com/djdv/go-filesystem-utils/internal/filesystem" p9fs "github.com/djdv/go-filesystem-utils/internal/filesystem/9p" + "github.com/djdv/go-filesystem-utils/internal/generic" "github.com/multiformats/go-multiaddr" ) @@ -15,10 +16,74 @@ type ( plan9GuestSettings p9fs.Guest plan9GuestOption func(*plan9GuestSettings) error plan9GuestOptions []plan9GuestOption + + plan9HostSettings p9fs.Host + plan9HostOption func(*plan9HostSettings) error + plan9HostOptions []plan9HostOption ) const p9GuestSrvFlagName = "server" +func makePlan9HostCommand() command.Command { + return makeMountSubcommand( + p9fs.HostID, + makeGuestCommands[plan9HostOptions, plan9HostSettings](p9fs.HostID), + ) +} + +func makePlan9Host(path ninePath, autoUnlink bool) (filesystem.Host, p9fs.MakeGuestFunc) { + guests := makeMountPointGuests[p9fs.Host](path) + return p9fs.HostID, newMakeGuestFunc(guests, path, autoUnlink) +} + +func unmarshalPlan9() (filesystem.Host, decodeFunc) { + return p9fs.HostID, func(b []byte) (string, error) { + var host p9fs.Host + err := json.Unmarshal(b, &host) + return host.Maddr.String(), err + } +} + +func (*plan9HostOptions) usage(guest filesystem.ID) string { + return string(p9fs.HostID) + " hosts " + + string(guest) + " as a 9P file server" +} + +func (o9 *plan9HostOptions) BindFlags(flagSet *flag.FlagSet) { + // TODO: - dedupe with guest + var ( + flagPrefix = prefixIDFlag(p9fs.HostID) + srvUsage = "9P2000.L file system server `maddr`" + srvName = flagPrefix + p9GuestSrvFlagName + ) + flagSetFunc(flagSet, srvName, srvUsage, o9, + func(value multiaddr.Multiaddr, settings *plan9HostSettings) error { + settings.Maddr = value + return nil + }) +} + +func (o9 plan9HostOptions) make() (plan9HostSettings, error) { + return makeWithOptions(o9...) +} + +func (set plan9HostSettings) marshal(arg string) ([]byte, error) { + if arg == "" { + err := command.UsageError{ + Err: generic.ConstError( + "expected server multiaddr", + ), + } + return nil, err + } + maddr, err := multiaddr.NewMultiaddr(arg) + if err != nil { + return nil, err + } + set.Maddr = maddr + return json.Marshal(set) +} + func makePlan9GuestCommand[ HC mountCmdHost[HT, HM], HM marshaller,