6
6
"encoding/json"
7
7
"errors"
8
8
"flag"
9
+ "fmt"
9
10
10
11
"github.com/djdv/go-filesystem-utils/internal/command"
11
12
"github.com/djdv/go-filesystem-utils/internal/filesystem"
@@ -16,11 +17,20 @@ import (
16
17
)
17
18
18
19
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
22
26
)
23
27
28
+ const nfsServerFlagName = "server"
29
+
30
+ func (ns nfsGuestSettings ) marshal (string ) ([]byte , error ) {
31
+ return json .Marshal (ns )
32
+ }
33
+
24
34
func makeNFSCommand () command.Command {
25
35
return makeMountSubcommand (
26
36
nfs .HostID ,
@@ -73,3 +83,47 @@ func unmarshalNFS() (filesystem.Host, decodeFunc) {
73
83
return "" , errors .New ("maddr not present in parsed JSON" ) // TODO: real error value
74
84
}
75
85
}
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