Skip to content

Commit 71d5603

Browse files
committed
feat: Add generate command
1 parent 9e6c91f commit 71d5603

40 files changed

+1270
-879
lines changed

cmd/iacker/main.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,12 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/spf13/cobra"
6+
"github.com/GuanceCloud/iacker/internal/cmd"
77
)
88

99
func main() {
10-
rootCmd := NewRootCmd()
10+
rootCmd := cmd.NewRootCmd()
1111
if err := rootCmd.Execute(); err != nil {
1212
fmt.Println(err)
1313
}
1414
}
15-
16-
func NewRootCmd() *cobra.Command {
17-
return &cobra.Command{
18-
Use: "iacker",
19-
Short: "The IaC Provider Development Framework",
20-
Long: `Help you to develop IaC provider easily.`,
21-
Run: func(cmd *cobra.Command, args []string) {
22-
fmt.Println("Hello, world!")
23-
},
24-
}
25-
}

cmd/iacker/main_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package main
33
import (
44
"bytes"
55
"testing"
6+
7+
"github.com/GuanceCloud/iacker/internal/cmd"
68
)
79

810
func TestMain(t *testing.T) {
911
var buf bytes.Buffer
10-
rootCmd := NewRootCmd()
12+
rootCmd := cmd.NewRootCmd()
1113
rootCmd.SetOutput(&buf)
1214
rootCmd.SetArgs([]string{"--help"})
1315
if err := rootCmd.Execute(); err != nil {
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/simple/manifest.cue

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package allinone
2+
3+
import (
4+
template "github.com/GuanceCloud/iacker/pkg/template/v1"
5+
)
6+
7+
// Pet is the pet resource definition
8+
resources: "Pet": {
9+
plural: "pets"
10+
title: {
11+
zh: "宠物"
12+
en: "Pet"
13+
}
14+
description: {
15+
zh: "宠物很可爱,包括猫猫狗狗等"
16+
en: "Pets are cute, including cats, dogs, etc."
17+
}
18+
model: "Pet"
19+
}
20+
21+
// Pet model
22+
resources: "Pet": models: "Pet": {
23+
title: {
24+
zh: "宠物"
25+
en: "Pet"
26+
}
27+
properties: [
28+
{
29+
name: "id"
30+
title: {
31+
zh: "ID"
32+
en: "ID"
33+
}
34+
schema: {
35+
type: "integer"
36+
required: true
37+
}
38+
},
39+
]
40+
}
41+
42+
// Template definition
43+
templates: "foo": {
44+
name: "basic"
45+
46+
inputs: template.#Inputs
47+
48+
diagnostics: [...template.#Diagnostic]
49+
50+
outputs: files: "README.md": {
51+
content: "Hello, World!"
52+
}
53+
}
54+
55+
// Template options
56+
options: templates: [
57+
{
58+
template: "foo"
59+
outdir: ".build"
60+
},
61+
]

go.mod

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,33 @@ module github.com/GuanceCloud/iacker
33
go 1.19
44

55
require (
6+
cuelang.org/go v0.6.0
7+
github.com/fatih/color v1.15.0
68
github.com/hashicorp/go-multierror v1.1.1
79
github.com/magefile/mage v1.15.0
10+
github.com/sergi/go-diff v1.3.1
811
github.com/spf13/cobra v1.7.0
12+
github.com/stretchr/testify v1.8.4
913
google.golang.org/protobuf v1.31.0
1014
)
1115

1216
require (
13-
github.com/google/go-cmp v0.5.9 // indirect
17+
github.com/cockroachdb/apd/v3 v3.2.0 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/emicklei/proto v1.10.0 // indirect
20+
github.com/google/uuid v1.2.0 // indirect
1421
github.com/hashicorp/errwrap v1.0.0 // indirect
1522
github.com/inconshreveable/mousetrap v1.1.0 // indirect
23+
github.com/mattn/go-colorable v0.1.13 // indirect
24+
github.com/mattn/go-isatty v0.0.17 // indirect
25+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
26+
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
27+
github.com/pmezard/go-difflib v1.0.0 // indirect
28+
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 // indirect
1629
github.com/spf13/pflag v1.0.5 // indirect
30+
golang.org/x/mod v0.9.0 // indirect
31+
golang.org/x/net v0.8.0 // indirect
32+
golang.org/x/sys v0.6.0 // indirect
33+
golang.org/x/text v0.8.0 // indirect
34+
gopkg.in/yaml.v3 v3.0.1 // indirect
1735
)

go.sum

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,79 @@
1+
cuelang.org/go v0.6.0 h1:dJhgKCog+FEZt7OwAYV1R+o/RZPmE8aqFoptmxSWyr8=
2+
cuelang.org/go v0.6.0/go.mod h1:9CxOX8aawrr3BgSdqPj7V0RYoXo7XIb+yDFC6uESrOQ=
3+
github.com/cockroachdb/apd/v3 v3.2.0 h1:79kHCn4tO0VGu3W0WujYrMjBDk8a2H4KEUYcXf7whcg=
4+
github.com/cockroachdb/apd/v3 v3.2.0/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
15
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw=
10+
github.com/emicklei/proto v1.10.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
11+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
12+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
13+
github.com/go-quicktest/qt v1.100.0 h1:I7iSLgIwNp0E0UnSvKJzs7ig0jg/Iq83zsZjtQNW7jY=
214
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
315
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
416
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
5-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
17+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
18+
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
619
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
720
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
821
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
922
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
1023
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
1124
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
25+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
26+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
27+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
28+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
29+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
30+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
31+
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
1232
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
1333
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
34+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
35+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
36+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
37+
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
38+
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
39+
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
40+
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
41+
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de h1:D5x39vF5KCwKQaw+OC9ZPiLVHXz3UFw2+psEX+gYcto=
42+
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuHZgRjZUWWuH1DTxCtxbHDOIJsudS8jzY=
43+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
44+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
45+
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 h1:sadMIsgmHpEOGbUs6VtHBXRR1OHevnj7hLx9ZcdNGW4=
46+
github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c=
47+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
1448
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
49+
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
50+
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
1551
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
1652
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
1753
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
1854
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
55+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
56+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
57+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
58+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
59+
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
60+
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
61+
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
62+
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
63+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
64+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
65+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
66+
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
67+
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
68+
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
1969
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2070
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
2171
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
2272
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
2373
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
74+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
75+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
76+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
77+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
78+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2479
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/cmd/generate.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/GuanceCloud/iacker/internal/cue"
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// NewGenerateCmd returns a new generate command.
12+
func NewGenerateCmd() *cobra.Command {
13+
generateCmd := &cobra.Command{
14+
Use: "generate",
15+
Aliases: []string{"gen"},
16+
Short: "Generate code from CUE",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
var mErr error
19+
if len(args) == 0 {
20+
args = []string{"."}
21+
}
22+
23+
// Generate all packages.
24+
for _, root := range args {
25+
rms, err := cue.ParsePackage(root)
26+
if err != nil {
27+
return err
28+
}
29+
if err := cue.Generate(rms); err != nil {
30+
mErr = multierror.Append(mErr, fmt.Errorf("generate %s error: %w", root, err))
31+
}
32+
}
33+
return mErr
34+
},
35+
}
36+
return generateCmd
37+
}

internal/cmd/init.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// NewInitCmd returns a new init command.
10+
func NewInitCmd() *cobra.Command {
11+
initCmd := &cobra.Command{
12+
Use: "init",
13+
Short: "Initialize a new IaC provider project",
14+
RunE: func(cmd *cobra.Command, args []string) error {
15+
return fmt.Errorf("not implemented")
16+
},
17+
}
18+
return initCmd
19+
}

internal/cmd/root.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// NewRootCmd returns a new root command.
8+
func NewRootCmd() *cobra.Command {
9+
root := &cobra.Command{
10+
Use: "iacker",
11+
Short: "The IaC Provider Development Framework",
12+
Long: `Help you to develop IaC provider easily.`,
13+
}
14+
root.AddCommand(
15+
NewInitCmd(),
16+
NewGenerateCmd(),
17+
)
18+
return root
19+
}

internal/cue/generator.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package cue
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"path/filepath"
7+
8+
"cuelang.org/go/cue"
9+
"github.com/fatih/color"
10+
"github.com/hashicorp/go-multierror"
11+
12+
"github.com/GuanceCloud/iacker/internal/helpers/filediff"
13+
templateV1 "github.com/GuanceCloud/iacker/pkg/template/v1"
14+
)
15+
16+
type generator struct{}
17+
18+
// GenerateOption is a function that configures a generator.
19+
type GenerateOption func(*generator) error
20+
21+
// Generate will generate code files into the output folder
22+
func Generate(p *Package, options ...GenerateOption) error {
23+
plan, err := GeneratePlan(p, options...)
24+
if err != nil {
25+
return fmt.Errorf("failed to generate plan: %w", err)
26+
}
27+
if err := plan.Pretty(); err != nil {
28+
return fmt.Errorf("failed to pretty print plan: %w", err)
29+
}
30+
return plan.Save()
31+
}
32+
33+
// GeneratePlan will generate a plan for generating code files into the output folder
34+
func GeneratePlan(p *Package, options ...GenerateOption) (*Plan, error) {
35+
var mErr error
36+
sourceFiles := make(map[string]string)
37+
targetFiles := make(map[string]string)
38+
rms := p.Manifest()
39+
40+
// Create generator
41+
g := &generator{}
42+
for _, option := range options {
43+
if err := option(g); err != nil {
44+
mErr = multierror.Append(mErr, fmt.Errorf("generate option failed: %w", err))
45+
}
46+
}
47+
48+
// Generate files from templates
49+
for i, templateOptions := range rms.Options.Templates {
50+
templateName := templateOptions.Template
51+
52+
color.Magenta("Generate template %q to folder %q", templateName, templateOptions.Outdir)
53+
54+
// Get the template
55+
templateValue := p.value.LookupPath(cue.MakePath(cue.Str("templates"), cue.Str(templateName)))
56+
if err := templateValue.Err(); err != nil {
57+
return nil, fmt.Errorf("lookup path failed: %w", err)
58+
}
59+
60+
// Fill the template with the input RMS data
61+
concretTemplateValue := templateValue.FillPath(cue.MakePath(cue.Str("inputs")), &templateV1.Inputs{
62+
Resources: rms.Resources,
63+
Errors: rms.Errors,
64+
Vars: templateOptions.Vars,
65+
})
66+
if err := concretTemplateValue.Err(); err != nil {
67+
return nil, fmt.Errorf("fill path failed: %w", err)
68+
}
69+
70+
// Check the template diagnostics
71+
var concretTemplate templateV1.Manifest
72+
if err := concretTemplateValue.Decode(&concretTemplate); err != nil {
73+
return nil, fmt.Errorf("decode template %s(%d): %w", templateName, i, err)
74+
}
75+
for _, diag := range concretTemplate.Diagnostics {
76+
mErr = multierror.Append(mErr, errors.New(diag.Message))
77+
}
78+
79+
// Build the generate plan
80+
files, err := filediff.ReadFiles(templateOptions.Outdir)
81+
if err != nil {
82+
return nil, fmt.Errorf("read existed files from outdir failed: %w", err)
83+
}
84+
for fileName, file := range files {
85+
sourceFiles[filepath.Join(templateOptions.Outdir, fileName)] = file
86+
}
87+
88+
for fileName, file := range concretTemplate.Outputs.Files {
89+
targetFiles[filepath.Join(templateOptions.Outdir, fileName)] = file.Content
90+
}
91+
}
92+
if mErr != nil {
93+
return nil, mErr
94+
}
95+
96+
return &Plan{sources: sourceFiles, targets: targetFiles}, nil
97+
}

0 commit comments

Comments
 (0)