diff --git a/internal/commands/flag.go b/internal/commands/flag.go index 796685c8..8e34c2a9 100644 --- a/internal/commands/flag.go +++ b/internal/commands/flag.go @@ -14,6 +14,7 @@ import ( "unicode/utf8" "unsafe" + "github.com/djdv/go-filesystem-utils/internal/filesystem" "github.com/djdv/go-filesystem-utils/internal/generic" "github.com/djdv/p9/p9" "github.com/multiformats/go-multiaddr" @@ -579,3 +580,7 @@ func parseMultiaddrList(parameter string) ([]multiaddr.Multiaddr, error) { } return maddrs, nil } + +func prefixIDFlag(system filesystem.ID) string { + return strings.ToLower(string(system)) + "-" +} diff --git a/internal/commands/mount.go b/internal/commands/mount.go index 4629d06f..f75fec08 100644 --- a/internal/commands/mount.go +++ b/internal/commands/mount.go @@ -292,6 +292,7 @@ func makeGuestCommands[ ](host filesystem.Host, ) []command.Command { guests := makeIPFSCommands[HC, HM](host) + guests = append(guests, makePlan9GuestCommand[HC, HM](host)) sortCommands(guests) return guests } diff --git a/internal/commands/mountpoint.go b/internal/commands/mountpoint.go index ba3ec88a..f0926863 100644 --- a/internal/commands/mountpoint.go +++ b/internal/commands/mountpoint.go @@ -142,6 +142,7 @@ func makeMountPointGuests[ ) mountPointGuests { guests := make(mountPointGuests) makeIPFSGuests[HC](guests, path) + guests[p9fs.GuestID] = newMountPointFunc[HC, p9fs.Guest](path) return guests } diff --git a/internal/commands/mountpoint_9p.go b/internal/commands/mountpoint_9p.go new file mode 100644 index 00000000..4734a4b6 --- /dev/null +++ b/internal/commands/mountpoint_9p.go @@ -0,0 +1,68 @@ +package commands + +import ( + "encoding/json" + "flag" + "fmt" + + "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/multiformats/go-multiaddr" +) + +type ( + plan9GuestSettings p9fs.Guest + plan9GuestOption func(*plan9GuestSettings) error + plan9GuestOptions []plan9GuestOption +) + +const p9GuestSrvFlagName = "server" + +func makePlan9GuestCommand[ + HC mountCmdHost[HT, HM], + HM marshaller, + HT any, +](host filesystem.Host, +) command.Command { + return makeMountCommand[HC, HM, plan9GuestOptions, plan9GuestSettings](host, p9fs.GuestID) +} + +func (*plan9GuestOptions) usage(filesystem.Host) string { + return string(p9fs.GuestID) + " attaches to a 9P file server" +} + +func (o9 *plan9GuestOptions) BindFlags(flagSet *flag.FlagSet) { + var ( + flagPrefix = prefixIDFlag(p9fs.GuestID) + srvUsage = "9P2000.L file system server `maddr`" + srvName = flagPrefix + p9GuestSrvFlagName + ) + flagSetFunc(flagSet, srvName, srvUsage, o9, + func(value multiaddr.Multiaddr, settings *plan9GuestSettings) error { + settings.Maddr = value + return nil + }) +} + +func (o9 plan9GuestOptions) make() (plan9GuestSettings, error) { + settings, err := makeWithOptions(o9...) + if err != nil { + return plan9GuestSettings{}, err + } + if settings.Maddr == nil { + var ( + flagPrefix = prefixIDFlag(p9fs.GuestID) + srvName = flagPrefix + p9GuestSrvFlagName + ) + return plan9GuestSettings{}, fmt.Errorf( + "flag `-%s` must be provided for 9P guests", + srvName, + ) + } + return settings, nil +} + +func (s9 plan9GuestSettings) marshal(string) ([]byte, error) { + return json.Marshal(s9) +} diff --git a/internal/commands/mountpoint_ipfs.go b/internal/commands/mountpoint_ipfs.go index 9561207a..87e7a899 100644 --- a/internal/commands/mountpoint_ipfs.go +++ b/internal/commands/mountpoint_ipfs.go @@ -71,10 +71,6 @@ func guestOverlayText(overlay, overlaid filesystem.ID) string { return string(overlay) + " is an " + string(overlaid) + " overlay" } -func prefixIDFlag(system filesystem.ID) string { - return strings.ToLower(string(system)) + "-" -} - func (*ipfsOptions) usage(filesystem.Host) string { return string(ipfs.IPFSID) + " provides an empty root directory." + "\nChild paths are forwarded to the IPFS API."