diff --git a/.vscode/launch.json b/.vscode/launch.json index 2521939ab..b55cd23f1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "mode": "debug", "program": "${workspaceRoot}/main.go", "cwd": "${workspaceRoot}", - "args": ["pullTemplates"] + "args": ["generate"] } ] } diff --git a/cmd/generate.go b/cmd/generate.go index cd6b2bf60..3ce8d18a8 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -62,21 +62,26 @@ func GenerateCode() error { return err } - // assign absolute path to the license file Path if it's not - absPath, err := filepath.Abs(coreProject.License.Path) - if err != nil { - log.Errorf("error while getting absolute path [" + err.Error() + "]") - return err - } - coreProject.License.Path = absPath - // assign absolute path to the license file path if it's not (if supplied for the nodes) - for _, node := range coreProject.CompageJSON.Nodes { - absPath, err = filepath.Abs(node.License.Path) + if len(coreProject.License.Path) > 0 { + // assign absolute path to the license file Path if it's not + absPath, err := filepath.Abs(coreProject.License.Path) if err != nil { log.Errorf("error while getting absolute path [" + err.Error() + "]") return err } - node.License.Path = absPath + coreProject.License.Path = absPath + } + + // assign absolute path to the license file path if it's not (if supplied for the nodes) + for _, node := range coreProject.CompageJSON.Nodes { + if len(node.License.Path) > 0 { + absPath, err := filepath.Abs(node.License.Path) + if err != nil { + log.Errorf("error while getting absolute path [" + err.Error() + "]") + return err + } + node.License.Path = absPath + } } // pull all required templates diff --git a/internal/languages/dotnet/generator.go b/internal/languages/dotnet/generator.go index 62e996d37..1ce29aa81 100644 --- a/internal/languages/dotnet/generator.go +++ b/internal/languages/dotnet/generator.go @@ -11,6 +11,7 @@ import ( "github.com/intelops/compage/internal/languages/dotnet/integrations/docker" "github.com/intelops/compage/internal/languages/dotnet/integrations/githubactions" "github.com/intelops/compage/internal/languages/dotnet/integrations/kubernetes" + "github.com/intelops/compage/internal/languages/dotnet/integrations/license" "github.com/intelops/compage/internal/languages/templates" "github.com/intelops/compage/internal/utils" log "github.com/sirupsen/logrus" @@ -113,6 +114,14 @@ func generateIntegrationConfig(dotNetValues *DotNetValues) error { log.Errorf("error while getting the integrations copier [" + err.Error() + "]") return err } + + // license files need to be generated for the whole project so, it should be here. + licenseCopier := m["license"].(*license.Copier) + if err = licenseCopier.CreateLicenseFiles(); err != nil { + log.Errorf("err : %s", err) + return err + } + // dockerfile needs to be generated for the whole project, so it should be here. dockerCopier := m["docker"].(*docker.Copier) if err = dockerCopier.CreateDockerFile(); err != nil { @@ -159,6 +168,9 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{}, restServerPort = "" } + // create dotnet specific licenseCopier + licenseCopier := license.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, dotNetValues.LDotNetLangNode.License) + // create dotnet specific dockerCopier dockerCopier := docker.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, isRestServer, restServerPort) @@ -172,5 +184,6 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{}, "docker": dockerCopier, "k8s": k8sCopier, "githubActions": githubActionsCopier, + "license": licenseCopier, }, nil } diff --git a/internal/languages/dotnet/integrations/license/copier.go b/internal/languages/dotnet/integrations/license/copier.go new file mode 100644 index 000000000..84352cff0 --- /dev/null +++ b/internal/languages/dotnet/integrations/license/copier.go @@ -0,0 +1,56 @@ +package license + +import ( + corenode "github.com/intelops/compage/internal/core/node" + "github.com/intelops/compage/internal/utils" + log "github.com/sirupsen/logrus" +) + +// Copier integrations specific copier +type Copier struct { + NodeName string + NodeDirectoryName string + TemplatesRootPath string + License *corenode.License + Data map[string]interface{} +} + +func NewCopier(gitRepositoryName, gitPlatformUserName, nodeName, nodeDirectoryName, templatesRootPath string, license *corenode.License) *Copier { + // populate map to replace templates + data := map[string]interface{}{ + "GitRepositoryName": gitRepositoryName, + "GitPlatformUserName": gitPlatformUserName, + } + + return &Copier{ + TemplatesRootPath: templatesRootPath, + NodeDirectoryName: nodeDirectoryName, + NodeName: nodeName, + Data: data, + License: license, + } +} + +// CreateLicenseFiles creates the required directory and copies files from language template. +func (c Copier) CreateLicenseFiles() error { + destDirectory := c.NodeDirectoryName + if err := utils.CreateDirectories(destDirectory); err != nil { + log.Errorf("error while creating directories [" + err.Error() + "]") + return err + } + // copy license file if it's been supplied + if c.License != nil && len(c.License.URL) > 0 { + // read file from url in c.License.URL. This is applicable for both config.yaml file and ui flow. + return utils.DownloadFile(c.NodeDirectoryName+"/LICENCE", c.License.URL) + } else if c.License != nil && len(c.License.Path) > 0 { + // local license file sent via config.yaml file. + // get the absolute path of the license file + _, err := utils.CopyFile(c.NodeDirectoryName+"/LICENCE", c.License.Path) + if err != nil { + log.Errorf("error while copying file [" + err.Error() + "]") + return err + } + } + // return from here as the license file has been copied + return nil +} diff --git a/internal/languages/languages.go b/internal/languages/languages.go index 1fbaacbdd..64285c029 100644 --- a/internal/languages/languages.go +++ b/internal/languages/languages.go @@ -25,10 +25,10 @@ type LanguageNode struct { Metadata map[string]interface{} `json:"metadata,omitempty"` Annotations map[string]string `json:"annotations,omitempty"` Language string `json:"language"` - - RestConfig *corenode.RestConfig `json:"addRestConfig"` - GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"` - WsConfig *corenode.WsConfig `json:"wsConfig"` + License *corenode.License `json:"license"` + RestConfig *corenode.RestConfig `json:"addRestConfig"` + GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"` + WsConfig *corenode.WsConfig `json:"wsConfig"` } // NewLanguageNode converts node to LanguageNode struct @@ -39,6 +39,7 @@ func NewLanguageNode(compageJSON *core.CompageJSON, node *corenode.Node) (*Langu Metadata: node.Metadata, Annotations: node.Annotations, Language: node.Language, + License: node.License, } addRestConfig(node, languageNode) diff --git a/internal/utils/license-helper.go b/internal/utils/license-helper.go index a025f5715..468ed193c 100644 --- a/internal/utils/license-helper.go +++ b/internal/utils/license-helper.go @@ -1,7 +1,6 @@ package utils import ( - "fmt" log "github.com/sirupsen/logrus" "io" "net/http" @@ -34,6 +33,6 @@ func DownloadFile(destination, src string) error { defer func(file *os.File) { _ = file.Close() }(file) - fmt.Printf("Downloaded a file %s with size %d", src, size) + log.Debugf("Downloaded a file %s with size %d", src, size) return nil }