summaryrefslogtreecommitdiff
path: root/pkg/files.go
blob: eb3fa3864abed3d4c38b981c1e74fce98c2df15c (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package gonf

import (
	"bytes"
	"crypto/sha256"
	"errors"
	"io"
	"io/fs"
	"log/slog"
	"os"
	"path/filepath"
)

// ----- Globals ---------------------------------------------------------------
var files []*FilePromise

// ----- Init ------------------------------------------------------------------
func init() {
	files = make([]*FilePromise, 0)
}

// ----- Public ----------------------------------------------------------------
type FilePromise struct {
	chain          []Promise
	contents       Value
	dirPermissions *Permissions
	err            error
	filename       Value
	permissions    *Permissions
	status         Status
}

func File(filename any) *FilePromise {
	return &FilePromise{
		chain:          nil,
		contents:       nil,
		dirPermissions: nil,
		err:            nil,
		filename:       interfaceToTemplateValue(filename),
		permissions:    nil,
		status:         PROMISED,
	}
}

func (f *FilePromise) Contents(contents any) *FilePromise {
	f.contents = interfaceToValue(contents)
	return f
}

func (f *FilePromise) DirectoriesPermissions(p *Permissions) *FilePromise {
	f.dirPermissions = p
	return f
}

func (f *FilePromise) Permissions(p *Permissions) *FilePromise {
	f.permissions = p
	return f
}

func (f *FilePromise) Template(contents any) *FilePromise {
	f.contents = interfaceToTemplateValue(contents)
	return f
}

// We want to satisfy the Promise interface
func (f *FilePromise) IfRepaired(p ...Promise) Promise {
	f.chain = append(f.chain, p...)
	return f
}

func (f *FilePromise) Promise() Promise {
	files = append(files, f)
	return f
}

func (f *FilePromise) Resolve() {
	filename := f.filename.String()
	if f.dirPermissions != nil {
		if f.status, f.err = makeDirectoriesHierarchy(filepath.Dir(filename), f.dirPermissions); f.err != nil {
			return
		}
	}
	if f.contents != nil {
		var sumFile []byte
		sumFile, f.err = sha256sumOfFile(filename)
		if f.err != nil {
			if !errors.Is(f.err, fs.ErrNotExist) {
				slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err)
				f.status = BROKEN
				return
			}
		}
		contents := f.contents.Bytes()
		sumContents := sha256sum(contents)
		if !bytes.Equal(sumFile, sumContents) {
			if f.err = writeFile(filename, contents); f.err != nil {
				f.status = BROKEN
				slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err)
				return
			}
			f.status = REPAIRED
		}
	}
	if f.permissions != nil {
		var status Status
		status, f.err = f.permissions.resolve(filename)
		if f.status == PROMISED || status == BROKEN {
			f.status = status
		}
		if f.err != nil {
			slog.Error("file", "filename", f.filename, "status", f.status, "error", f.err)
			return
		}
	}
	if f.status == REPAIRED {
		slog.Info("file", "filename", f.filename, "status", f.status)
		for _, p := range f.chain {
			p.Resolve()
		}
	} else {
		f.status = KEPT
		slog.Debug("file", "filename", f.filename, "status", f.status)
	}
}

func (f FilePromise) Status() Status {
	return f.status
}

// ----- Internal --------------------------------------------------------------
func resolveFiles() (status Status) {
	status = KEPT
	for _, f := range files {
		if f.status == PROMISED {
			f.Resolve()
			switch f.status {
			case BROKEN:
				return BROKEN
			case REPAIRED:
				status = REPAIRED
			}
		}
	}
	return
}

func sha256sumOfFile(filename string) ([]byte, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	h := sha256.New()
	if _, err := io.Copy(h, f); err != nil {
		return nil, err
	}
	return h.Sum(nil), nil
}

func writeFile(filename string, contents []byte) error {
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.Write(contents)
	return err
}