|
| 1 | +package license |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/intelops/compage/internal/core" |
| 5 | + "github.com/intelops/compage/internal/languages/executor" |
| 6 | + "github.com/intelops/compage/internal/utils" |
| 7 | + log "github.com/sirupsen/logrus" |
| 8 | +) |
| 9 | + |
| 10 | +const File = "LICENSE.tmpl" |
| 11 | + |
| 12 | +// Copier integrations specific copier |
| 13 | +type Copier struct { |
| 14 | + ProjectDirectoryName string |
| 15 | + GitRepositoryName string |
| 16 | + TemplatesRootPath string |
| 17 | + Data map[string]interface{} |
| 18 | +} |
| 19 | + |
| 20 | +func NewCopier(project *core.Project) (*Copier, error) { |
| 21 | + // retrieve project named directory |
| 22 | + //gitPlatformUserName, gitRepositoryName, projectDirectoryName, templatesRootPath string |
| 23 | + // populate map to replace templates |
| 24 | + data := map[string]interface{}{ |
| 25 | + "GitRepositoryName": project.GitRepositoryName, |
| 26 | + "GitPlatformUserName": project.GitPlatformUserName, |
| 27 | + } |
| 28 | + |
| 29 | + templatesRootPath, err := utils.GetTemplatesRootPath("common-templates") |
| 30 | + if err != nil { |
| 31 | + log.Errorf("error while getting the project root path [" + err.Error() + "]") |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + return &Copier{ |
| 35 | + // TODO change this path to constant. Add language specific analysers in a generic way later. |
| 36 | + TemplatesRootPath: templatesRootPath, |
| 37 | + ProjectDirectoryName: utils.GetProjectDirectoryName(project.Name), |
| 38 | + GitRepositoryName: project.GitRepositoryName, |
| 39 | + Data: data, |
| 40 | + }, nil |
| 41 | +} |
| 42 | + |
| 43 | +// CreateLicenseFiles creates the required directory and copies files from language template. |
| 44 | +func (c Copier) CreateLicenseFiles() error { |
| 45 | + destDirectory := c.ProjectDirectoryName |
| 46 | + if err := utils.CreateDirectories(destDirectory); err != nil { |
| 47 | + log.Errorf("error while creating directories [" + err.Error() + "]") |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + var filePaths []*string |
| 52 | + // copy deployment files to generated license config files |
| 53 | + targetLicenseFileName := c.ProjectDirectoryName + "/" + File |
| 54 | + _, err := utils.CopyFile(targetLicenseFileName, c.TemplatesRootPath+"/"+File) |
| 55 | + if err != nil { |
| 56 | + log.Errorf("error while copying file [" + err.Error() + "]") |
| 57 | + return err |
| 58 | + } |
| 59 | + filePaths = append(filePaths, &targetLicenseFileName) |
| 60 | + return executor.Execute(filePaths, c.Data) |
| 61 | +} |
0 commit comments