Skip to content

Commit c597010

Browse files
committed
commands: mount - add 9P2000.L guest support
1 parent 235e1c4 commit c597010

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
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
@@ -292,6 +292,7 @@ func makeGuestCommands[
292292
](host filesystem.Host,
293293
) []command.Command {
294294
guests := makeIPFSCommands[HC, HM](host)
295+
guests = append(guests, makePlan9GuestCommand[HC, HM](host))
295296
sortCommands(guests)
296297
return guests
297298
}

internal/commands/mountpoint.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func makeMountPointGuests[
142142
) mountPointGuests {
143143
guests := make(mountPointGuests)
144144
makeIPFSGuests[HC](guests, path)
145+
guests[p9fs.GuestID] = newMountPointFunc[HC, p9fs.Guest](path)
145146
return guests
146147
}
147148

internal/commands/mountpoint_9p.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package commands
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
8+
"github.com/djdv/go-filesystem-utils/internal/command"
9+
"github.com/djdv/go-filesystem-utils/internal/filesystem"
10+
p9fs "github.com/djdv/go-filesystem-utils/internal/filesystem/9p"
11+
"github.com/multiformats/go-multiaddr"
12+
)
13+
14+
type (
15+
plan9GuestSettings p9fs.Guest
16+
plan9GuestOption func(*plan9GuestSettings) error
17+
plan9GuestOptions []plan9GuestOption
18+
)
19+
20+
const p9GuestSrvFlagName = "server"
21+
22+
func makePlan9GuestCommand[
23+
HC mountCmdHost[HT, HM],
24+
HM marshaller,
25+
HT any,
26+
](host filesystem.Host,
27+
) command.Command {
28+
return makeMountCommand[HC, HM, plan9GuestOptions, plan9GuestSettings](host, p9fs.GuestID)
29+
}
30+
31+
func (*plan9GuestOptions) usage(filesystem.Host) string {
32+
return string(p9fs.GuestID) + " attaches to a 9P file server"
33+
}
34+
35+
func (o9 *plan9GuestOptions) BindFlags(flagSet *flag.FlagSet) {
36+
var (
37+
flagPrefix = prefixIDFlag(p9fs.GuestID)
38+
srvUsage = "9P2000.L file system server `maddr`"
39+
srvName = flagPrefix + p9GuestSrvFlagName
40+
)
41+
flagSetFunc(flagSet, srvName, srvUsage, o9,
42+
func(value multiaddr.Multiaddr, settings *plan9GuestSettings) error {
43+
settings.Maddr = value
44+
return nil
45+
})
46+
}
47+
48+
func (o9 plan9GuestOptions) make() (plan9GuestSettings, error) {
49+
settings, err := makeWithOptions(o9...)
50+
if err != nil {
51+
return plan9GuestSettings{}, err
52+
}
53+
if settings.Maddr == nil {
54+
var (
55+
flagPrefix = prefixIDFlag(p9fs.GuestID)
56+
srvName = flagPrefix + p9GuestSrvFlagName
57+
)
58+
return plan9GuestSettings{}, fmt.Errorf(
59+
"flag `-%s` must be provided for 9P guests",
60+
srvName,
61+
)
62+
}
63+
return settings, nil
64+
}
65+
66+
func (s9 plan9GuestSettings) marshal(string) ([]byte, error) {
67+
return json.Marshal(s9)
68+
}

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."

0 commit comments

Comments
 (0)