File: write.go

package info (click to toggle)
incus 6.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,788 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (212 lines) | stat: -rw-r--r-- 4,866 bytes parent folder | download | duplicates (8)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package file

import (
	_ "embed"
	"errors"
	"fmt"
	"os"
	"strings"

	"github.com/lxc/incus/v6/cmd/generate-database/lex"
)

const codeGeneratedByLine = `// Code generated by generate-database from the incus project - DO NOT EDIT.`

//go:embed boilerplate/boilerplate.go
var boilerplate string

// Boilerplate writes the general boilerplate code for mapper to the target package.
func Boilerplate(path string) error {
	boilerplate = strings.Replace(boilerplate, "package boilerplate", fmt.Sprintf("package %s", os.Getenv("GOPACKAGE")), 1)
	content := codeGeneratedByLine + "\n\n" + boilerplate

	bytes := []byte(content)

	var err error

	if path == "-" {
		_, err = os.Stdout.Write(bytes)
	} else {
		err = os.WriteFile(path, []byte(content), 0o644)
	}

	if err != nil {
		return fmt.Errorf("Mapper boilerplate file %q: %w", path, err)
	}

	return nil
}

// Reset an auto-generated source file, writing a new empty file header.
func Reset(path string, imports []string, buildComment string, iface bool) error {
	// A new line needs to be appended after the build comment.
	if buildComment != "" {
		buildComment = fmt.Sprintf(`%s

`, buildComment)
	}

	if iface {
		err := resetInterface(path, buildComment)
		if err != nil {
			return err
		}
	}

	content := fmt.Sprintf(`%s%s

package %s

import (
`, buildComment, codeGeneratedByLine, os.Getenv("GOPACKAGE"))

	for _, uri := range imports {
		content += fmt.Sprintf("\t%q\n", uri)
	}

	content += ")\n\n"

	bytes := []byte(content)

	var err error

	if path == "-" {
		_, err = os.Stdout.Write(bytes)
	} else {
		err = os.WriteFile(path, []byte(content), 0o644)
	}

	if err != nil {
		return fmt.Errorf("Reset target source file %q: %w", path, err)
	}

	return nil
}

func resetInterface(path string, buildComment string) error {
	if strings.HasSuffix(path, "mapper.go") {
		parts := strings.Split(path, ".")
		interfacePath := strings.Join(parts[:len(parts)-2], ".") + ".interface.mapper.go"
		content := fmt.Sprintf("%spackage %s", buildComment, os.Getenv("GOPACKAGE"))
		err := os.WriteFile(interfacePath, []byte(content), 0o644)
		return err
	}

	return nil
}

// Append a code snippet to a file.
func Append(entity string, path string, snippet Snippet, iface bool) error {
	if iface {
		err := appendInterface(entity, path, snippet)
		if err != nil {
			return err
		}
	}

	buffer := newBuffer()
	buffer.N()

	err := snippet.Generate(buffer)
	if err != nil {
		return fmt.Errorf("Generate code snippet: %w", err)
	}

	var file *os.File

	if path == "-" {
		file = os.Stdout
	} else {
		file, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0o644)
		if err != nil {
			return fmt.Errorf("Open target source code file %q: %w", path, err)
		}

		defer func() { _ = file.Close() }()
	}

	bytes, err := buffer.code()
	if err != nil {
		return err
	}

	_, err = file.Write(bytes)
	if err != nil {
		return fmt.Errorf("Append snippet to target source code file %q: %w", path, err)
	}

	// Return any errors on close if file is not stdout.
	if path != "-" {
		return file.Close()
	}

	return nil
}

func appendInterface(entity string, path string, snippet Snippet) error {
	if !strings.HasSuffix(path, ".mapper.go") {
		return nil
	}

	parts := strings.Split(path, ".")
	interfacePath := strings.Join(parts[:len(parts)-2], ".") + ".interface.mapper.go"

	stat, err := os.Stat(interfacePath)
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			return nil
		}

		return fmt.Errorf("could not get file info for path %q: %w", interfacePath, err)
	}

	buffer := newBuffer()

	file, err := os.OpenFile(interfacePath, os.O_RDWR, 0o644)
	if err != nil {
		return fmt.Errorf("Open target source code file %q: %w", interfacePath, err)
	}

	defer func() { _ = file.Close() }()

	err = snippet.GenerateSignature(buffer)
	if err != nil {
		return fmt.Errorf("Generate interface snippet: %w", err)
	}

	bytes, err := buffer.code()
	if err != nil {
		return err
	}

	declaration := fmt.Sprintf("type %sGenerated interface {", lex.PascalCase(entity))
	content := make([]byte, stat.Size())
	_, err = file.Read(content)
	if err != nil {
		return fmt.Errorf("Could not read interface file %q: %w", interfacePath, err)
	}

	firstWrite := !strings.Contains(string(content), declaration)
	if firstWrite {
		// If this is the first signature write to the file, append the whole thing.
		_, err = file.WriteAt(bytes, stat.Size())
	} else {
		// If an interface already exists, just append the method, omitting everything before the first '{'.
		startIndex := 0
		for i := range bytes {
			// type ObjectGenerated interface {
			if string(bytes[i]) == "{" {
				startIndex = i + 1
				break
			}
		}
		// overwrite the closing brace.
		_, err = file.WriteAt(bytes[startIndex:], stat.Size()-2)
	}

	if err != nil {
		return fmt.Errorf("Append snippet to target source code file %q: %w", interfacePath, err)
	}

	return file.Close()
}