Skip to content

Commit 4402e35

Browse files
committed
commands: mount - add NFS guest support
1 parent d08775f commit 4402e35

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

internal/commands/flag.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"unicode/utf8"
1515
"unsafe"
1616

17+
"github.com/djdv/go-filesystem-utils/internal/filesystem"
1718
"github.com/djdv/go-filesystem-utils/internal/generic"
1819
"github.com/djdv/p9/p9"
1920
"github.com/multiformats/go-multiaddr"
@@ -579,3 +580,7 @@ func parseMultiaddrList(parameter string) ([]multiaddr.Multiaddr, error) {
579580
}
580581
return maddrs, nil
581582
}
583+
584+
func prefixIDFlag(system filesystem.ID) string {
585+
return strings.ToLower(string(system)) + "-"
586+
}

internal/commands/mount.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ func makeGuestCommands[
293293
](host filesystem.Host,
294294
) []command.Command {
295295
guests := makeIPFSCommands[HC, HM](host)
296+
guests = append(guests, makeNFSGuestCommand[HC, HM](host))
296297
sortCommands(guests)
297298
return guests
298299
}

internal/commands/mountpoint.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/djdv/go-filesystem-utils/internal/filesystem"
1111
p9fs "github.com/djdv/go-filesystem-utils/internal/filesystem/9p"
12+
"github.com/djdv/go-filesystem-utils/internal/filesystem/nfs"
1213
"github.com/djdv/go-filesystem-utils/internal/generic"
1314
"github.com/djdv/p9/p9"
1415
)
@@ -143,6 +144,8 @@ func makeMountPointGuests[
143144
) mountPointGuests {
144145
guests := make(mountPointGuests)
145146
makeIPFSGuests[HC](guests, path)
147+
// TODO: [build constraints] this pulls in nfs; split out.
148+
guests[nfs.GuestID] = newMountPointFunc[HC, nfs.Guest](path)
146149
return guests
147150
}
148151

internal/commands/mountpoint_ipfs.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ func guestOverlayText(overlay, overlaid filesystem.ID) string {
7171
return string(overlay) + " is an " + string(overlaid) + " overlay"
7272
}
7373

74-
func prefixIDFlag(system filesystem.ID) string {
75-
return strings.ToLower(string(system)) + "-"
76-
}
77-
7874
func (*ipfsOptions) usage(filesystem.Host) string {
7975
return string(ipfs.IPFSID) + " provides an empty root directory." +
8076
"\nChild paths are forwarded to the IPFS API."

internal/commands/mountpoint_nfs.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"flag"
9+
"fmt"
910

1011
"github.com/djdv/go-filesystem-utils/internal/command"
1112
"github.com/djdv/go-filesystem-utils/internal/filesystem"
@@ -16,11 +17,20 @@ import (
1617
)
1718

1819
type (
19-
nfsHostSettings nfs.Host
20-
nfsHostOption func(*nfsHostSettings) error
21-
nfsHostOptions []nfsHostOption
20+
nfsHostSettings nfs.Host
21+
nfsHostOption func(*nfsHostSettings) error
22+
nfsHostOptions []nfsHostOption
23+
nfsGuestSettings nfs.Guest
24+
nfsGuestOption func(*nfsGuestSettings) error
25+
nfsGuestOptions []nfsGuestOption
2226
)
2327

28+
const nfsServerFlagName = "server"
29+
30+
func (ns nfsGuestSettings) marshal(string) ([]byte, error) {
31+
return json.Marshal(ns)
32+
}
33+
2434
func makeNFSCommand() command.Command {
2535
return makeMountSubcommand(
2636
nfs.HostID,
@@ -73,3 +83,47 @@ func unmarshalNFS() (filesystem.Host, decodeFunc) {
7383
return "", errors.New("maddr not present in parsed JSON") // TODO: real error value
7484
}
7585
}
86+
87+
func makeNFSGuestCommand[
88+
HC mountCmdHost[HT, HM],
89+
HM marshaller,
90+
HT any,
91+
](host filesystem.Host,
92+
) command.Command {
93+
return makeMountCommand[HC, HM, nfsGuestOptions, nfsGuestSettings](host, nfs.GuestID)
94+
}
95+
96+
func (*nfsGuestOptions) usage(filesystem.Host) string {
97+
return string(nfs.GuestID) + " attaches to an NFS file server"
98+
}
99+
100+
func (no *nfsGuestOptions) BindFlags(flagSet *flag.FlagSet) {
101+
var (
102+
flagPrefix = prefixIDFlag(nfs.GuestID)
103+
srvUsage = "NFS server `maddr`"
104+
srvName = flagPrefix + nfsServerFlagName
105+
)
106+
flagSetFunc(flagSet, srvName, srvUsage, no,
107+
func(value multiaddr.Multiaddr, settings *nfsGuestSettings) error {
108+
settings.Maddr = value
109+
return nil
110+
})
111+
}
112+
113+
func (no nfsGuestOptions) make() (nfsGuestSettings, error) {
114+
settings, err := makeWithOptions(no...)
115+
if err != nil {
116+
return nfsGuestSettings{}, err
117+
}
118+
if settings.Maddr == nil {
119+
var (
120+
flagPrefix = prefixIDFlag(nfs.GuestID)
121+
srvName = flagPrefix + nfsServerFlagName
122+
)
123+
return nfsGuestSettings{}, fmt.Errorf(
124+
"flag `-%s` must be provided for NFS guests",
125+
srvName,
126+
)
127+
}
128+
return settings, nil
129+
}

0 commit comments

Comments
 (0)