Skip to content

Commit 7ebfa8f

Browse files
feat: added project level configurable license
1 parent 23f0f17 commit 7ebfa8f

File tree

7 files changed

+94
-1
lines changed

7 files changed

+94
-1
lines changed

cmd/generate.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
log "github.com/sirupsen/logrus"
1010
"github.com/spf13/cobra"
1111
"os"
12+
"path/filepath"
1213
)
1314

1415
// generateCmd represents the generate command
@@ -61,6 +62,23 @@ func GenerateCode() error {
6162
return err
6263
}
6364

65+
// assign absolute path to the license file Path if it's not
66+
absPath, err := filepath.Abs(coreProject.License.Path)
67+
if err != nil {
68+
log.Errorf("error while getting absolute path [" + err.Error() + "]")
69+
return err
70+
}
71+
coreProject.License.Path = absPath
72+
// assign absolute path to the license file path if it's not (if supplied for the nodes)
73+
for _, node := range coreProject.CompageJSON.Nodes {
74+
absPath, err = filepath.Abs(node.License.Path)
75+
if err != nil {
76+
log.Errorf("error while getting absolute path [" + err.Error() + "]")
77+
return err
78+
}
79+
node.License.Path = absPath
80+
}
81+
6482
// pull all required templates
6583
// pull the common templates
6684
err = ociregistry.PullOCIArtifact("common", project.Version)

cmd/models/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ type GitDetails struct {
2222
Platform Platform `yaml:"platform,omitempty"`
2323
}
2424

25+
type License struct {
26+
Name string `yaml:"name,omitempty"`
27+
URL string `yaml:"url,omitempty"`
28+
Path string `yaml:"path,omitempty"`
29+
}
30+
2531
type Project struct {
2632
Name string `yaml:"name"`
2733
Version string `yaml:"version"`
34+
License License `yaml:"license"`
2835
GitDetails GitDetails `yaml:"git"`
2936
CompageJSON map[string]interface{} `yaml:"compageJSON"`
3037
ProjectMetadata string `yaml:"projectMetadata"`

internal/converter/cmd/converter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func GetProject(input *models.Project) (*core.Project, error) {
2121
CompageJSON: compageJSON,
2222
Name: input.Name,
2323
Version: input.Version,
24+
License: &input.License,
2425
GitPlatformName: input.GitDetails.Platform.Name,
2526
GitPlatformURL: input.GitDetails.Platform.URL,
2627
GitPlatformUserName: input.GitDetails.Platform.UserName,

internal/core/models.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"github.com/intelops/compage/cmd/models"
45
coreedge "github.com/intelops/compage/internal/core/edge"
56
corenode "github.com/intelops/compage/internal/core/node"
67
"time"
@@ -19,6 +20,7 @@ type ModificationDetails struct {
1920
type Project struct {
2021
Name string `json:"name"`
2122
Version string `json:"version"`
23+
License *models.License `json:"license"`
2224
CompageJSON *CompageJSON `json:"compageJSON"`
2325
GitRepositoryName string `json:"gitRepositoryName"`
2426
GitRepositoryURL string `json:"gitRepositoryURL"`

internal/core/node/node.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ type Node struct {
77
Language string `json:"language"`
88
// Name of a component (required, this will be service and deployment name).
99
Name string `json:"name"`
10+
// License of the component.
11+
License *License `json:"license"`
1012
// RestConfig holds all config related to REST. If nil, it means that the node is not REST server or has REST clients.
1113
RestConfig *RestConfig `json:"restConfig,omitempty"`
1214
// GrpcConfig holds all config related to gRPC. If nil, it means that the node is not gRPC server or has gRPC clients.
@@ -19,6 +21,12 @@ type Node struct {
1921
Annotations map[string]string `json:"annotations,omitempty"`
2022
}
2123

24+
type License struct {
25+
Name string `yaml:"name,omitempty"`
26+
URL string `yaml:"url,omitempty"`
27+
Path string `yaml:"path,omitempty"`
28+
}
29+
2230
type RestServer struct {
2331
Port string `json:"port"`
2432
SQLDB string `json:"sqlDB"`

internal/integrations/license/copier.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package license
22

33
import (
4+
"github.com/intelops/compage/cmd/models"
45
"github.com/intelops/compage/internal/core"
56
"github.com/intelops/compage/internal/languages/executor"
67
"github.com/intelops/compage/internal/utils"
@@ -14,6 +15,7 @@ type Copier struct {
1415
ProjectDirectoryName string
1516
GitRepositoryName string
1617
TemplatesRootPath string
18+
License *models.License
1719
Data map[string]interface{}
1820
}
1921

@@ -37,6 +39,7 @@ func NewCopier(project *core.Project) (*Copier, error) {
3739
ProjectDirectoryName: utils.GetProjectDirectoryName(project.Name),
3840
GitRepositoryName: project.GitRepositoryName,
3941
Data: data,
42+
License: project.License,
4043
}, nil
4144
}
4245

@@ -47,7 +50,22 @@ func (c Copier) CreateLicenseFiles() error {
4750
log.Errorf("error while creating directories [" + err.Error() + "]")
4851
return err
4952
}
50-
53+
// copy license file if it's been supplied
54+
if c.License != nil && len(c.License.URL) > 0 {
55+
// read file from url in c.License.URL. This is applicable for both config.yaml file and ui flow.
56+
return utils.DownloadFile(c.ProjectDirectoryName+"/LICENCE", c.License.URL)
57+
} else if c.License != nil && len(c.License.Path) > 0 {
58+
// local license file sent via config.yaml file.
59+
// get the absolute path of the license file
60+
_, err := utils.CopyFile(c.ProjectDirectoryName+"/LICENCE", c.License.Path)
61+
if err != nil {
62+
log.Errorf("error while copying file [" + err.Error() + "]")
63+
return err
64+
}
65+
// return from here as the license file has been copied
66+
return nil
67+
}
68+
// copy license file from templates (the default one)
5169
var filePaths []*string
5270
// copy deployment files to generated license config files
5371
targetLicenseFileName := c.ProjectDirectoryName + "/" + File

internal/utils/license-helper.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
log "github.com/sirupsen/logrus"
6+
"io"
7+
"net/http"
8+
"os"
9+
)
10+
11+
func DownloadFile(destination, src string) error {
12+
// Create blank file
13+
file, err := os.Create(destination)
14+
if err != nil {
15+
log.Errorf("error while creating file [" + err.Error() + "]")
16+
return err
17+
}
18+
client := http.Client{
19+
CheckRedirect: func(r *http.Request, via []*http.Request) error {
20+
r.URL.Opaque = r.URL.Path
21+
return nil
22+
},
23+
}
24+
// Put content on file
25+
resp, err := client.Get(src)
26+
if err != nil {
27+
log.Errorf("error while getting file [" + err.Error() + "]")
28+
return err
29+
}
30+
defer func(Body io.ReadCloser) {
31+
_ = Body.Close()
32+
}(resp.Body)
33+
size, err := io.Copy(file, resp.Body)
34+
defer func(file *os.File) {
35+
_ = file.Close()
36+
}(file)
37+
fmt.Printf("Downloaded a file %s with size %d", src, size)
38+
return nil
39+
}

0 commit comments

Comments
 (0)