Skip to content

Commit

Permalink
commands: mount - add NFS guest support
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Nov 19, 2023
1 parent 4205e1b commit c0f90c6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 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 @@ -293,6 +293,7 @@ func makeGuestCommands[
](host filesystem.Host,
) []command.Command {
guests := makeIPFSCommands[HC, HM](host)
guests = append(guests, makeNFSGuestCommand[HC, HM](host))
sortCommands(guests)
return guests
}
Expand Down
3 changes: 3 additions & 0 deletions internal/commands/mountpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"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/filesystem/nfs"
"github.com/djdv/go-filesystem-utils/internal/generic"
"github.com/djdv/p9/p9"
)
Expand Down Expand Up @@ -143,6 +144,8 @@ func makeMountPointGuests[
) mountPointGuests {
guests := make(mountPointGuests)
makeIPFSGuests[HC](guests, path)
// TODO: [build constraints] this pulls in nfs; split out.
guests[nfs.GuestID] = newMountPointFunc[HC, nfs.Guest](path)
return guests
}

Expand Down
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
60 changes: 57 additions & 3 deletions internal/commands/mountpoint_nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"flag"
"fmt"

"github.com/djdv/go-filesystem-utils/internal/command"
"github.com/djdv/go-filesystem-utils/internal/filesystem"
Expand All @@ -16,11 +17,20 @@ import (
)

type (
nfsHostSettings nfs.Host
nfsHostOption func(*nfsHostSettings) error
nfsHostOptions []nfsHostOption
nfsHostSettings nfs.Host
nfsHostOption func(*nfsHostSettings) error
nfsHostOptions []nfsHostOption
nfsGuestSettings nfs.Guest
nfsGuestOption func(*nfsGuestSettings) error
nfsGuestOptions []nfsGuestOption
)

const nfsServerFlagName = "server"

func (ns nfsGuestSettings) marshal(string) ([]byte, error) {
return json.Marshal(ns)
}

func makeNFSCommand() command.Command {
return makeMountSubcommand(
nfs.HostID,
Expand Down Expand Up @@ -73,3 +83,47 @@ func unmarshalNFS() (filesystem.Host, decodeFunc) {
return "", errors.New("maddr not present in parsed JSON") // TODO: real error value
}
}

func makeNFSGuestCommand[
HC mountCmdHost[HT, HM],
HM marshaller,
HT any,
](host filesystem.Host,
) command.Command {
return makeMountCommand[HC, HM, nfsGuestOptions, nfsGuestSettings](host, nfs.GuestID)
}

func (*nfsGuestOptions) usage(filesystem.Host) string {
return string(nfs.GuestID) + " attaches to an NFS file server"
}

func (no *nfsGuestOptions) BindFlags(flagSet *flag.FlagSet) {
var (
flagPrefix = prefixIDFlag(nfs.GuestID)
srvUsage = "NFS server `maddr`"
srvName = flagPrefix + nfsServerFlagName
)
flagSetFunc(flagSet, srvName, srvUsage, no,
func(value multiaddr.Multiaddr, settings *nfsGuestSettings) error {
settings.Maddr = value
return nil
})
}

func (no nfsGuestOptions) make() (nfsGuestSettings, error) {
settings, err := makeWithOptions(no...)
if err != nil {
return nfsGuestSettings{}, err
}
if settings.Maddr == nil {
var (
flagPrefix = prefixIDFlag(nfs.GuestID)
srvName = flagPrefix + nfsServerFlagName
)
return nfsGuestSettings{}, fmt.Errorf(
"flag `-%s` must be provided for NFS guests",
srvName,
)
}
return settings, nil
}

0 comments on commit c0f90c6

Please sign in to comment.