/
filemode.go
50 lines (38 loc) · 1.08 KB
/
filemode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package logs
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/Notifiarr/notifiarr/pkg/mnd"
"golift.io/rotatorr"
)
// These are used for custom logs.
//nolint:gochecknoglobals
var (
fileMode = rotatorr.FileMode
)
// FileMode is used to unmarshal a unix file mode from the config file.
type FileMode os.FileMode
// UnmarshalText turns a unix file mode, wrapped in quotes or not, into a usable os.FileMode.
func (f *FileMode) UnmarshalText(text []byte) error {
str := strings.TrimSpace(strings.Trim(string(text), `"'`))
fm, err := strconv.ParseUint(str, mnd.Base8, mnd.Bits32)
if err != nil {
return fmt.Errorf("file_mode (%s) is invalid: %w", str, err)
}
*f = FileMode(os.FileMode(fm))
return nil
}
// MarshalText satisfies an encoder.TextMarshaler interface.
func (f FileMode) MarshalText() ([]byte, error) {
return []byte(f.String()), nil
}
// String creates a unix-octal version of a file mode.
func (f FileMode) String() string {
return fmt.Sprintf("%04o", f)
}
// Mode returns the compatible os.FileMode.
func (f FileMode) Mode() os.FileMode {
return os.FileMode(f)
}