|
1 | | -// ./cmd/generate/resource/generate.go |
2 | 1 | package resource |
3 | 2 |
|
4 | 3 | import ( |
| 4 | + "bytes" |
5 | 5 | "fmt" |
| 6 | + "io/fs" |
6 | 7 | "os" |
7 | 8 | "path/filepath" |
8 | 9 | "strings" |
| 10 | + "text/template" |
9 | 11 | "time" |
10 | 12 |
|
11 | 13 | "github.com/briandowns/spinner" |
| 14 | + "github.com/golang-programming/gincli/embedded" |
12 | 15 | "github.com/golang-programming/gincli/utils" |
13 | 16 | ) |
14 | 17 |
|
15 | 18 | func generateResourceFromTemplate() { |
16 | | - templatePath := fmt.Sprintf("templates/others/%s/endpoints", strings.ToLower(transport)) |
17 | | - resource := resourcePath + "/" + resourceName |
18 | | - if !createEndpoint { |
19 | | - templatePath = fmt.Sprintf("templates/others/%s/blank", strings.ToLower(transport)) |
20 | | - } |
| 19 | + embeddedRoot := fmt.Sprintf("templates/others/%s/%s", strings.ToLower(transport), |
| 20 | + func() string { |
| 21 | + if createEndpoint { |
| 22 | + return "endpoints" |
| 23 | + } |
| 24 | + return "blank" |
| 25 | + }(), |
| 26 | + ) |
21 | 27 |
|
22 | 28 | s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) |
23 | 29 | s.Suffix = " Creating resource..." |
24 | 30 | s.Start() |
25 | 31 | defer s.Stop() |
26 | 32 |
|
27 | | - err := filepath.Walk(templatePath, func(path string, info os.FileInfo, err error) error { |
| 33 | + if err := fs.WalkDir(embedded.TemplatesFS, embeddedRoot, func(path string, d fs.DirEntry, err error) error { |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + relPath, err := filepath.Rel(embeddedRoot, path) |
28 | 39 | if err != nil { |
29 | 40 | return err |
30 | 41 | } |
| 42 | + targetPath := filepath.Join(resourcePath, relPath) |
31 | 43 |
|
32 | | - relativePath := strings.TrimPrefix(path, templatePath) |
33 | | - targetPath := filepath.Join(resource, relativePath) |
| 44 | + if d.IsDir() { |
| 45 | + return os.MkdirAll(targetPath, os.ModePerm) |
| 46 | + } |
34 | 47 |
|
35 | | - if strings.HasSuffix(info.Name(), ".tpl") { |
| 48 | + if strings.HasSuffix(d.Name(), ".tpl") { |
36 | 49 | targetFile := strings.TrimSuffix(targetPath, ".tpl") |
37 | | - fmt.Print(path, targetFile) |
38 | | - utils.GenerateFileFromTemplate(path, targetFile, getConfig()) |
| 50 | + data, err := embedded.TemplatesFS.ReadFile(path) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("failed to read template %s: %v", path, err) |
| 53 | + } |
| 54 | + |
| 55 | + tmpl, err := template.New("").Parse(string(data)) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("failed to parse template %s: %v", path, err) |
| 58 | + } |
| 59 | + |
| 60 | + var buf bytes.Buffer |
| 61 | + if err := tmpl.Execute(&buf, getConfig()); err != nil { |
| 62 | + return fmt.Errorf("failed to execute template %s: %v", targetFile, err) |
| 63 | + } |
| 64 | + |
| 65 | + if err := os.WriteFile(targetFile, buf.Bytes(), 0644); err != nil { |
| 66 | + return fmt.Errorf("failed to write file %s: %v", targetFile, err) |
| 67 | + } |
39 | 68 | } |
40 | 69 |
|
41 | 70 | return nil |
42 | | - }) |
43 | | - |
44 | | - if err != nil { |
| 71 | + }); err != nil { |
45 | 72 | utils.LogError(fmt.Sprintf("Error while copying templates: %v", err)) |
46 | 73 | } |
47 | 74 | } |
48 | 75 |
|
49 | 76 | func getConfig() map[string]string { |
50 | 77 | return map[string]string{ |
51 | | - "CapitalizeResourceName": utils.Capitalize(resourceName), |
| 78 | + "CapitalizeResourceName": utils.ToPascalCase(resourceName), |
52 | 79 | "ResourceName": utils.ConvertToSnakeCase(resourceName), |
53 | 80 | "Module": utils.DetectModuleName(), |
54 | 81 | } |
|
0 commit comments