|
| 1 | +package configs |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/openopsdev/go-cli-commons/logger" |
| 12 | + "github.com/openopsdev/stitch/services" |
| 13 | + "github.com/openopsdev/stitch/templates" |
| 14 | + "github.com/openopsdev/stitch/utils" |
| 15 | + "gopkg.in/yaml.v2" |
| 16 | +) |
| 17 | + |
| 18 | +type Templater interface { |
| 19 | + Run() |
| 20 | +} |
| 21 | + |
| 22 | +type Applate struct { |
| 23 | + Source string |
| 24 | + Answer map[string]string |
| 25 | + Dependency DependencyConfig |
| 26 | +} |
| 27 | + |
| 28 | +func findCacheDir() string { |
| 29 | + homedir, _ := os.UserHomeDir() |
| 30 | + return path.Join(homedir, ".stitch/applates") |
| 31 | +} |
| 32 | + |
| 33 | +var cacheDir = findCacheDir() |
| 34 | + |
| 35 | +func NewApplate(source string, answers map[string]string) Applate { |
| 36 | + var promptConfig PromptConfig |
| 37 | + var dependencyConfig DependencyConfig |
| 38 | + applateDir := path.Join(cacheDir, source) |
| 39 | + |
| 40 | + if err := configExists(applateDir, ".stitch/applate.yaml"); os.IsNotExist(err) { |
| 41 | + err = services.GitClone(source, applateDir) |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + logger.Fatal(fmt.Errorf("failed to pull %s applate: %v", source, err.Error()).Error()) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // TODO: handle no prompt config file |
| 49 | + applatePromptConfigPath := path.Join(applateDir, ".stitch/prompts.yaml") |
| 50 | + applatePromptContent, err := ioutil.ReadFile(applatePromptConfigPath) |
| 51 | + |
| 52 | + if err != nil { |
| 53 | + logger.Fatal(fmt.Errorf("failed to read prompt config: %v", err).Error()) |
| 54 | + } |
| 55 | + |
| 56 | + err = yaml.Unmarshal(applatePromptContent, &promptConfig) |
| 57 | + |
| 58 | + if err != nil { |
| 59 | + logger.Fatal(fmt.Errorf("failed to build prompt config: %v", err).Error()) |
| 60 | + } |
| 61 | + prompts := promptConfig.Build() |
| 62 | + promptAnswers := prompts.Run() |
| 63 | + |
| 64 | + // TODO: split into a Go Routine |
| 65 | + applateConfigFilePath := path.Join(applateDir, ".stitch/applate.yaml") |
| 66 | + applateConfigContents, err := ioutil.ReadFile(applateConfigFilePath) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + logger.Fatal(fmt.Errorf("failed to read existing config: %v", err).Error()) |
| 70 | + } |
| 71 | + |
| 72 | + err = yaml.Unmarshal(applateConfigContents, &dependencyConfig) |
| 73 | + |
| 74 | + if err != nil { |
| 75 | + logger.Fatal(fmt.Errorf("failed to build applate config: %v", err).Error()) |
| 76 | + } |
| 77 | + |
| 78 | + if len(dependencyConfig.Version) > 0 { |
| 79 | + // Adding the global language version to answers |
| 80 | + promptAnswers["version"] = dependencyConfig.Version |
| 81 | + } |
| 82 | + |
| 83 | + dependencyConfig.Vars = promptAnswers |
| 84 | + |
| 85 | + err = dependencyConfig.Init() |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + logger.Fatal(fmt.Errorf("failed to init project: %v", err).Error()) |
| 89 | + } |
| 90 | + |
| 91 | + return Applate{ |
| 92 | + Source: applateDir, |
| 93 | + Dependency: dependencyConfig, |
| 94 | + Answer: promptAnswers, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (a Applate) Run() []error { |
| 99 | + var runErrors []error |
| 100 | + files, err := a.findTemplates() |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + runErrors = append(runErrors, fmt.Errorf("cannot find tempalte: %v", err.Error())) |
| 104 | + return runErrors |
| 105 | + } |
| 106 | + |
| 107 | + for _, f := range files { |
| 108 | + file := utils.File{ |
| 109 | + Source: f, |
| 110 | + } |
| 111 | + rawData, err := file.Read() |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + runErrors = append(runErrors, fmt.Errorf("failed to read %s: %v", f, err.Error())) |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + template := templates.HandlebarTemplate{ |
| 119 | + Raw: rawData, |
| 120 | + Destination: a.buildDestinationPath(f), |
| 121 | + } |
| 122 | + |
| 123 | + err = template.Save(a.Answer) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + runErrors = append(runErrors, fmt.Errorf("failed to generate %s template: %v", f, err.Error())) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if a.Dependency.Dependencies != nil { |
| 131 | + err = a.Dependency.InstallDependencies() |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + runErrors = append(runErrors, fmt.Errorf("failed to install: %v", err.Error())) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return runErrors |
| 139 | +} |
| 140 | + |
| 141 | +func (a Applate) findTemplates() ([]string, error) { |
| 142 | + var files []string |
| 143 | + |
| 144 | + err := filepath.Walk(a.Source, func(path string, info os.FileInfo, err error) error { |
| 145 | + isConfig := strings.Contains(path, "applate.yaml") |
| 146 | + gitDir := strings.Contains(path, ".git") |
| 147 | + isFileToAdd := !isConfig && !info.IsDir() && !gitDir |
| 148 | + if isFileToAdd { |
| 149 | + files = append(files, path) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | + }) |
| 154 | + |
| 155 | + return files, err |
| 156 | +} |
| 157 | + |
| 158 | +func (a Applate) buildDestinationPath(p string) string { |
| 159 | + s := strings.Split(p, a.Source) |
| 160 | + _, s = s[0], s[1:] |
| 161 | + updatedPath := strings.Join(s, "/") |
| 162 | + cwd, _ := os.Getwd() |
| 163 | + return path.Join(cwd, updatedPath) |
| 164 | +} |
0 commit comments