|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "runtime" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/akutz/goof" |
| 10 | +) |
| 11 | + |
| 12 | +// MountOption is a mount option. |
| 13 | +type MountOption int |
| 14 | + |
| 15 | +// MountOptions are a mount options string. |
| 16 | +type MountOptions []MountOption |
| 17 | + |
| 18 | +// String returns the string representation of the MountOption. |
| 19 | +func (o MountOption) String() string { |
| 20 | + if buf, err := o.MarshalText(); err == nil { |
| 21 | + return string(buf) |
| 22 | + } |
| 23 | + return "" |
| 24 | +} |
| 25 | + |
| 26 | +func (o MountOption) bytes() []byte { |
| 27 | + if v, ok := mountOptToStr[o]; ok { |
| 28 | + return []byte(v) |
| 29 | + } |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +// ParseMountOption parses a mount option. |
| 34 | +func ParseMountOption(text string) MountOption { |
| 35 | + o := MountOptUnknown |
| 36 | + o.UnmarshalText([]byte(text)) |
| 37 | + return o |
| 38 | +} |
| 39 | + |
| 40 | +// MarshalText marshals the MountOption to its string representation. |
| 41 | +func (o MountOption) MarshalText() ([]byte, error) { |
| 42 | + if buf := o.bytes(); buf != nil { |
| 43 | + return buf, nil |
| 44 | + } |
| 45 | + return nil, goof.WithField("opt", int(o), "invalid mount option") |
| 46 | +} |
| 47 | + |
| 48 | +// UnmarshalText marshals the MountOption from its string representation. |
| 49 | +func (o *MountOption) UnmarshalText(data []byte) error { |
| 50 | + text := string(data) |
| 51 | + if v, ok := mountStrToOpt[strings.ToLower(text)]; ok { |
| 52 | + *o = v |
| 53 | + return nil |
| 54 | + } |
| 55 | + return goof.WithField("opt", text, "invalid mount option") |
| 56 | +} |
| 57 | + |
| 58 | +const ( |
| 59 | + commaByteVal byte = 44 |
| 60 | + spaceByteVal byte = 32 |
| 61 | +) |
| 62 | + |
| 63 | +var ( |
| 64 | + commaSepBuf = []byte{commaByteVal} |
| 65 | + commaSpaceSepBuf = []byte{commaByteVal, spaceByteVal} |
| 66 | +) |
| 67 | + |
| 68 | +// ParseMountOptions parses a mount options string. |
| 69 | +func ParseMountOptions(text string) MountOptions { |
| 70 | + var opts MountOptions |
| 71 | + if err := opts.UnmarshalText([]byte(text)); err == nil { |
| 72 | + return opts |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// String returns the string representation of the MountOptions object. |
| 78 | +func (opts MountOptions) String() string { |
| 79 | + if s, err := opts.MarshalText(); err == nil { |
| 80 | + return string(s) |
| 81 | + } |
| 82 | + return "" |
| 83 | +} |
| 84 | + |
| 85 | +// MarshalText marshals the MountOptions to its string representation. |
| 86 | +func (opts MountOptions) MarshalText() ([]byte, error) { |
| 87 | + buf := &bytes.Buffer{} |
| 88 | + for x, o := range opts { |
| 89 | + if v, ok := mountOptToStr[o]; ok { |
| 90 | + buf.WriteString(v) |
| 91 | + if x < (len(opts) - 1) { |
| 92 | + switch runtime.GOOS { |
| 93 | + case "linux": |
| 94 | + buf.WriteString(",") |
| 95 | + case "darwin": |
| 96 | + buf.WriteString(", ") |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return buf.Bytes(), nil |
| 102 | +} |
| 103 | + |
| 104 | +// UnmarshalText marshals the MountOptions from its string representation. |
| 105 | +func (opts *MountOptions) UnmarshalText(text []byte) error { |
| 106 | + var sepBuf []byte |
| 107 | + switch runtime.GOOS { |
| 108 | + case "linux": |
| 109 | + sepBuf = commaSepBuf |
| 110 | + case "darwin": |
| 111 | + sepBuf = commaSpaceSepBuf |
| 112 | + } |
| 113 | + optBufs := bytes.Split(text, sepBuf) |
| 114 | + for _, optText := range optBufs { |
| 115 | + if o := ParseMountOption(string(optText)); o != MountOptUnknown { |
| 116 | + *opts = append(*opts, o) |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +// MarshalJSON marshals the MountOptions to its JSON representation. |
| 123 | +func (opts MountOptions) MarshalJSON() ([]byte, error) { |
| 124 | + strOpts := make([]string, len(opts)) |
| 125 | + for i, o := range opts { |
| 126 | + strOpts[i] = o.String() |
| 127 | + } |
| 128 | + return json.Marshal(strOpts) |
| 129 | +} |
| 130 | + |
| 131 | +// UnmarshalJSON marshals the MountOptions from its JSON representation. |
| 132 | +func (opts *MountOptions) UnmarshalJSON(text []byte) error { |
| 133 | + strOpts := []string{} |
| 134 | + if err := json.Unmarshal(text, &strOpts); err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + for _, optText := range strOpts { |
| 138 | + if o := ParseMountOption(optText); o != MountOptUnknown { |
| 139 | + *opts = append(*opts, o) |
| 140 | + } |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
0 commit comments