-
Notifications
You must be signed in to change notification settings - Fork 82
/
conffile.go
149 lines (132 loc) · 3.14 KB
/
conffile.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Package conffile wraps the read and write of configuration files
package conffile
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/regclient/regclient/internal/rwfs"
)
type File struct {
// dir, name string
perms int
fullname string
fs rwfs.RWFS
}
type Opt func(*File)
// New returns a new File
func New(opts ...Opt) *File {
f := File{perms: 0600, fs: rwfs.OSNew("")}
for _, fn := range opts {
fn(&f)
}
if f.fullname == "" {
return nil
}
return &f
}
// WithDirName determines the filename from a subdirectory in the user's HOME
// e.g. dir=".app", name="config.json", sets the fullname to "$HOME/.app/config.json"
func WithDirName(dir, name string) Opt {
return func(f *File) {
f.fullname = filepath.Join(homedir(), dir, name)
}
}
// WithEnvFile sets the fullname to the environment value if defined
func WithEnvFile(envVar string) Opt {
return func(f *File) {
val := os.Getenv(envVar)
if val != "" {
f.fullname = val
}
}
}
// WithEnvDir sets the fullname to the environment value + filename if the environment variable is defined
func WithEnvDir(envVar, name string) Opt {
return func(f *File) {
val := os.Getenv(envVar)
if val != "" {
f.fullname = filepath.Join(val, name)
}
}
}
// WithFullname specifies the filename
func WithFullname(fullname string) Opt {
return func(f *File) {
f.fullname = fullname
}
}
// WithFS overrides the default OS filesystem
func WithFS(fs rwfs.RWFS) Opt {
return func(f *File) {
f.fs = fs
}
}
// WithPerms specifies the permissions to create a file with (default 0600)
func WithPerms(perms int) Opt {
return func(f *File) {
f.perms = perms
}
}
func (f *File) Name() string {
return f.fullname
}
func (f *File) Open() (io.ReadCloser, error) {
return f.fs.Open(f.fullname)
}
func (f *File) Write(rdr io.Reader) error {
// create temp file/open
dir := filepath.Dir(f.fullname)
if err := rwfs.MkdirAll(f.fs, dir, 0700); err != nil {
return err
}
tmp, err := rwfs.CreateTemp(f.fs, dir, filepath.Base(f.fullname))
if err != nil {
return err
}
tmpStat, err := tmp.Stat()
if err != nil {
return err
}
tmpName := tmpStat.Name()
tmpFullname := filepath.Join(dir, tmpName)
defer f.fs.Remove(tmpFullname)
// copy from rdr to temp file
_, err = io.Copy(tmp, rdr)
errC := tmp.Close()
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
if errC != nil {
return fmt.Errorf("failed to close config: %w", errC)
}
// adjust file ownership/permissions
mode := os.FileMode(0600)
uid := os.Getuid()
gid := os.Getgid()
// adjust defaults based on existing file if available
stat, err := rwfs.Stat(f.fs, f.fullname)
if err == nil {
// adjust mode to existing file
if stat.Mode().IsRegular() {
mode = stat.Mode()
}
uid, gid, _ = getFileOwner(stat)
} else if !errors.Is(err, fs.ErrNotExist) {
return err
}
// update mode and owner of temp file
fPerm, ok := f.fs.(rwfs.RWPerms)
if ok {
if err := fPerm.Chmod(tmpFullname, mode); err != nil {
return err
}
if uid > 0 && gid > 0 {
_ = fPerm.Chown(tmpFullname, uid, gid)
}
}
// move temp file to target filename
return f.fs.Rename(tmpFullname, f.fullname)
}