|
| 1 | +package copy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "gopkg.in/yaml.v2" |
| 12 | +) |
| 13 | + |
| 14 | +type Resource struct { |
| 15 | + Examples []Example `yaml:"examples"` |
| 16 | +} |
| 17 | + |
| 18 | +type Example struct { |
| 19 | + Name string `yaml:"name"` |
| 20 | + ConfigPath string `yaml:"config_path,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +func ProcessResourceFile(filePath, serviceName, examplesSourceDir, samplesDestDir string) error { |
| 24 | + originalBytes, err := ioutil.ReadFile(filePath) |
| 25 | + if err != nil { |
| 26 | + return fmt.Errorf("failed to read YAML file: %w", err) |
| 27 | + } |
| 28 | + |
| 29 | + var resource Resource |
| 30 | + err = yaml.Unmarshal(originalBytes, &resource) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("failed to unmarshal YAML: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + if len(resource.Examples) == 0 { |
| 36 | + return nil |
| 37 | + } |
| 38 | + |
| 39 | + // fmt.Printf("Found %d examples in %s for service '%s'\n", len(resource.Examples), filepath.Base(filePath), serviceName) |
| 40 | + |
| 41 | + for _, example := range resource.Examples { |
| 42 | + if example.Name == "" { |
| 43 | + log.Printf("Skipping example with empty name in file: %s\n", filePath) |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + // Determine the source file name. Use config_path if it exists, |
| 48 | + // otherwise fall back to the example's name. |
| 49 | + var sourceFileName string |
| 50 | + if example.ConfigPath != "" { |
| 51 | + sourceFileName = filepath.Base(example.ConfigPath) |
| 52 | + } else { |
| 53 | + sourceFileName = fmt.Sprintf("%s.tf.tmpl", example.Name) |
| 54 | + } |
| 55 | + |
| 56 | + sourcePath := filepath.Join(examplesSourceDir, sourceFileName) |
| 57 | + |
| 58 | + // Check if the source template file actually exists. |
| 59 | + if _, err := os.Stat(sourcePath); os.IsNotExist(err) { |
| 60 | + // Skip if the template doesn't exist. |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + destDir := filepath.Join(samplesDestDir, serviceName) |
| 65 | + destPath := filepath.Join(destDir, sourceFileName) |
| 66 | + |
| 67 | + err := copyFile(sourcePath, destPath) |
| 68 | + if err != nil { |
| 69 | + // Log the error but don't stop processing other examples. |
| 70 | + log.Printf("Failed to copy '%s' to '%s': %v\n", sourcePath, destPath, err) |
| 71 | + } else { |
| 72 | + // fmt.Printf(" - Copied '%s'\n", sourceFileName) |
| 73 | + fmt.Print() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func copyFile(src, dst string) error { |
| 81 | + input, err := ioutil.ReadFile(src) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("could not read source file %s: %w", src, err) |
| 84 | + } |
| 85 | + |
| 86 | + // Switch existing Vars to ResourceIdVars |
| 87 | + output := strings.ReplaceAll(string(input), "$.Vars", "$.ResourceIdVars") |
| 88 | + |
| 89 | + if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { |
| 90 | + return fmt.Errorf("could not create destination directory for %s: %w", dst, err) |
| 91 | + } |
| 92 | + |
| 93 | + // Get the source file's permissions to apply to the destination file. |
| 94 | + info, err := os.Stat(src) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("could not stat source file %s: %w", src, err) |
| 97 | + } |
| 98 | + |
| 99 | + // Write the modified content to the destination file. |
| 100 | + err = ioutil.WriteFile(dst, []byte(output), info.Mode()) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("could not write to destination file %s: %w", dst, err) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
0 commit comments