|
| 1 | +//go:build !nonfs |
| 2 | + |
| 3 | +package commands |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "flag" |
| 9 | + |
| 10 | + "github.com/djdv/go-filesystem-utils/internal/command" |
| 11 | + "github.com/djdv/go-filesystem-utils/internal/filesystem" |
| 12 | + p9fs "github.com/djdv/go-filesystem-utils/internal/filesystem/9p" |
| 13 | + "github.com/djdv/go-filesystem-utils/internal/filesystem/nfs" |
| 14 | + "github.com/djdv/go-filesystem-utils/internal/generic" |
| 15 | + "github.com/multiformats/go-multiaddr" |
| 16 | +) |
| 17 | + |
| 18 | +type ( |
| 19 | + nfsHostSettings nfs.Host |
| 20 | + nfsHostOption func(*nfsHostSettings) error |
| 21 | + nfsHostOptions []nfsHostOption |
| 22 | +) |
| 23 | + |
| 24 | +func makeNFSCommand() command.Command { |
| 25 | + return makeMountSubcommand( |
| 26 | + nfs.HostID, |
| 27 | + makeGuestCommands[nfsHostOptions, nfsHostSettings](nfs.HostID), |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +func makeNFSHost(path ninePath, autoUnlink bool) (filesystem.Host, p9fs.MakeGuestFunc) { |
| 32 | + guests := makeMountPointGuests[nfs.Host](path) |
| 33 | + return nfs.HostID, newMakeGuestFunc(guests, path, autoUnlink) |
| 34 | +} |
| 35 | + |
| 36 | +func (on *nfsHostOptions) BindFlags(flagSet *flag.FlagSet) { /* NOOP */ } |
| 37 | + |
| 38 | +func (on nfsHostOptions) make() (nfsHostSettings, error) { |
| 39 | + return makeWithOptions(on...) |
| 40 | +} |
| 41 | + |
| 42 | +func (*nfsHostOptions) usage(guest filesystem.ID) string { |
| 43 | + return string(nfs.HostID) + " hosts " + |
| 44 | + string(guest) + " as an NFS server" |
| 45 | +} |
| 46 | + |
| 47 | +func (set nfsHostSettings) marshal(arg string) ([]byte, error) { |
| 48 | + if arg == "" { |
| 49 | + err := command.UsageError{ |
| 50 | + Err: generic.ConstError( |
| 51 | + "expected server multiaddr", |
| 52 | + ), |
| 53 | + } |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + maddr, err := multiaddr.NewMultiaddr(arg) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + set.Maddr = maddr |
| 61 | + return json.Marshal(set) |
| 62 | +} |
| 63 | + |
| 64 | +func unmarshalNFS() (filesystem.Host, decodeFunc) { |
| 65 | + return nfs.HostID, func(b []byte) (string, error) { |
| 66 | + var host nfs.Host |
| 67 | + if err := json.Unmarshal(b, &host); err != nil { |
| 68 | + return "", err |
| 69 | + } |
| 70 | + if maddr := host.Maddr; maddr != nil { |
| 71 | + return host.Maddr.String(), nil |
| 72 | + } |
| 73 | + return "", errors.New("NFS host address was not present in the mountpoint data") |
| 74 | + } |
| 75 | +} |
0 commit comments