-
Notifications
You must be signed in to change notification settings - Fork 8
/
src.go
112 lines (84 loc) · 3.08 KB
/
src.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package scaffold
import (
"bytes"
"context"
"fmt"
"io"
"text/template"
"cuelang.org/go/cue/format"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
)
func generateCueSource(ctx context.Context, fsfs fnfs.ReadWriteFS, filePath string, t *template.Template, data interface{}) error {
return fnfs.WriteWorkspaceFile(ctx, console.Stdout(ctx), fsfs, filePath, func(w io.Writer) error {
var body bytes.Buffer
if err := t.Execute(&body, data); err != nil {
return fnerrors.InternalError("failed to apply template: %w", err)
}
var src bytes.Buffer
if _, err := body.WriteTo(&src); err != nil {
return err
}
formatted, err := format.Source(src.Bytes())
if err != nil {
fmt.Fprintln(console.Debug(ctx), "The input sources were:")
fmt.Fprintln(console.Debug(ctx), src.String())
return fnerrors.InternalError("failed to format generated Cue file: %w", err)
}
_, err = w.Write(formatted)
return err
})
}
func createProtoScaffold(ctx context.Context, fsfs fnfs.ReadWriteFS, filePath string, t *template.Template, data interface{}) error {
return fnfs.WriteWorkspaceFile(ctx, console.Stdout(ctx), fsfs, filePath, func(w io.Writer) error {
var body bytes.Buffer
if err := t.Execute(&body, data); err != nil {
return fnerrors.InternalError("failed to apply template: %w", err)
}
var src bytes.Buffer
if _, err := body.WriteTo(&src); err != nil {
return err
}
// TODO run proto formatter
_, err := w.Write(src.Bytes())
return err
})
}
func generateWebSource(ctx context.Context, fsfs fnfs.ReadWriteFS, filePath string, t *template.Template, data interface{}) error {
return fnfs.WriteWorkspaceFile(ctx, console.Stdout(ctx), fsfs, filePath, func(w io.Writer) error {
var body bytes.Buffer
if err := t.Execute(&body, data); err != nil {
return fnerrors.InternalError("failed to apply template: %w", err)
}
var src bytes.Buffer
if _, err := body.WriteTo(&src); err != nil {
return err
}
_, err := w.Write(src.Bytes())
return err
})
}
func generateGoSource(ctx context.Context, fsfs fnfs.ReadWriteFS, filePath string, t *template.Template, data interface{}) error {
return fnfs.WriteWorkspaceFile(ctx, console.Stdout(ctx), fsfs, filePath, func(w io.Writer) error {
var body bytes.Buffer
if err := t.Execute(&body, data); err != nil {
return fnerrors.InternalError("failed to apply template: %w", err)
}
var src bytes.Buffer
if _, err := body.WriteTo(&src); err != nil {
return err
}
formatted, err := format.Source(src.Bytes())
if err != nil {
fmt.Fprintln(console.Debug(ctx), "The input sources were:")
fmt.Fprintln(console.Debug(ctx), src.String())
return fnerrors.InternalError("failed to format generated Go file %s\nwith code:\n%s\nerr: %w", filePath, src.String(), err)
}
_, err = w.Write(formatted)
return err
})
}