createMainGoFile creates main.go file in tmp dir that content is mainGoTpl
(b *buildingMaterial)
| 153 | |
| 154 | // createMainGoFile creates main.go file in tmp dir that content is mainGoTpl |
| 155 | func createMainGoFile(b *buildingMaterial) (err error) { |
| 156 | fmt.Printf("[build] build dir: %s\n", b.tmpDir) |
| 157 | err = dir.CreateDirIfNotExist(b.tmpDir) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | |
| 162 | var ( |
| 163 | remotePlugins []string |
| 164 | ) |
| 165 | for _, p := range b.plugins { |
| 166 | remotePlugins = append(remotePlugins, versionedModulePath(p.Name, p.Version)) |
| 167 | } |
| 168 | |
| 169 | mainGoFile := &bytes.Buffer{} |
| 170 | tmpl, err := template.New("main").Parse(mainGoTpl) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | err = tmpl.Execute(mainGoFile, map[string]any{ |
| 175 | "remote_plugins": remotePlugins, |
| 176 | }) |
| 177 | if err != nil { |
| 178 | return err |
| 179 | } |
| 180 | |
| 181 | err = writer.WriteFile(filepath.Join(b.tmpDir, "main.go"), mainGoFile.String()) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | |
| 186 | err = writer.WriteFile(filepath.Join(b.tmpDir, "go.mod"), goModTpl) |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | |
| 191 | for _, p := range b.plugins { |
| 192 | // If user set a path, use it to replace the module with local path |
| 193 | if len(p.Path) > 0 { |
| 194 | var replacement string |
| 195 | if len(p.Version) > 0 { |
| 196 | replacement = fmt.Sprintf("%s@%s=%s", p.Name, p.Version, p.Path) |
| 197 | } else { |
| 198 | replacement = fmt.Sprintf("%s=%s", p.Name, p.Path) |
| 199 | } |
| 200 | err = b.newExecCmd("go", "mod", "edit", "-replace", replacement).Run() |
| 201 | } else if len(p.Version) > 0 { |
| 202 | // If user specify a version, use it to get specific version of the module |
| 203 | err = b.newExecCmd("go", "get", fmt.Sprintf("%s@%s", p.Name, p.Version)).Run() |
| 204 | } |
| 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | } |
| 209 | return |
| 210 | } |
| 211 | |
| 212 | // downloadGoModFile run go mod commands to download dependencies |
nothing calls this directly
no test coverage detected