Skip to content

Commit

Permalink
commands: mount - add 9P2000.L guest support
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Sep 13, 2023
1 parent 235e1c4 commit c597010
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
5 changes: 5 additions & 0 deletions internal/commands/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)) + "-"
}
1 change: 1 addition & 0 deletions internal/commands/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions internal/commands/mountpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func makeMountPointGuests[
) mountPointGuests {
guests := make(mountPointGuests)
makeIPFSGuests[HC](guests, path)
guests[p9fs.GuestID] = newMountPointFunc[HC, p9fs.Guest](path)
return guests
}

Expand Down
68 changes: 68 additions & 0 deletions internal/commands/mountpoint_9p.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 0 additions & 4 deletions internal/commands/mountpoint_ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit c597010

Please sign in to comment.