Skip to content

Commit c82e053

Browse files
Merge pull request #192 from intelops/add-new-command
Add new command : pullTemplates
2 parents 8bcf7e7 + 3a43c0e commit c82e053

File tree

823 files changed

+127335
-381
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

823 files changed

+127335
-381
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ jobs:
4848
export GOPATH=$HOME/go
4949
export GOBIN=$GOPATH/bin
5050
export PATH=$PATH:$GOPATH:$GOBIN:$HOME/ogc
51+
go run main.go pullTemplates --all
5152
go test -v ./... -race -coverprofile=coverage.out -coverpkg=./... -covermode=atomic
5253
- name: Upload coverage to Codecov
5354
uses: codecov/codecov-action@v3

.gitmodules

Lines changed: 0 additions & 31 deletions
This file was deleted.

.goreleaser.yaml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
version: 1
1010
project_name: compage
1111

12-
before:
13-
hooks:
14-
- go mod tidy
15-
- go mod download
16-
- git submodule update --init --recursive
17-
1812
builds:
1913
- env:
2014
- CGO_ENABLED=0
@@ -87,15 +81,4 @@ nfpms:
8781
Compage installer package. CLI application to generate code for rest and grpc.
8882
formats:
8983
- rpm
90-
- deb
91-
92-
#blobs:
93-
# - extra_files:
94-
# # Include specific files or directories from submodules
95-
# - templates/compage-template-go/**
96-
# - templates/compage-template-java/**
97-
# - templates/compage-template-javascript/**
98-
# - templates/compage-template-python/**
99-
# - templates/compage-template-typescript/**
100-
# - templates/compage-template-rust/**
101-
# - templates/compage-template-ruby/**
84+
- deb

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
6060
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
6161

6262
COPY --from=builder /app /
63-
COPY templates /app/templates
6463

6564
# test if the below command avoids loading the files later.
6665
RUN go version

cmd/generate.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var generateCmd = &cobra.Command{
1818
1919
Change the file as per your needs and then run the compage generate command to generate the code.`,
2020
Run: func(cmd *cobra.Command, args []string) {
21-
GenerateCode()
21+
err := GenerateCode()
22+
cobra.CheckErr(err)
2223
},
2324
}
2425

@@ -29,10 +30,10 @@ func init() {
2930
log.Errorf("error while getting the current directory [" + err.Error() + "]")
3031
return
3132
}
32-
// set the project folder environment variable, if this is set, then the project will be generated in this folder
33-
err = os.Setenv("COMPAGE_GENERATED_PROJECT_FOLDER", wD)
33+
// set the project directory environment variable, if this is set, then the project will be generated in this folder
34+
err = os.Setenv("COMPAGE_GENERATED_PROJECT_DIRECTORY", wD)
3435
if err != nil {
35-
log.Errorf("error while setting the project folder [" + err.Error() + "]")
36+
log.Errorf("error while setting the project directory [" + err.Error() + "]")
3637
return
3738
}
3839
// Here you will define your flags and configuration settings.
@@ -46,7 +47,7 @@ func init() {
4647
// generateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4748
}
4849

49-
func GenerateCode() {
50+
func GenerateCode() error {
5051
// Read the file from the current directory and convert it to project
5152
project, err := models.ReadConfigYAMLFile("config.yaml")
5253
cobra.CheckErr(err)
@@ -55,14 +56,32 @@ func GenerateCode() {
5556
coreProject, err := cmd.GetProject(project)
5657
if err != nil {
5758
log.Errorf("error while converting request to project [" + err.Error() + "]")
58-
return
59+
return err
60+
}
61+
62+
// pull all required templates
63+
// pull the common templates
64+
err = CloneOrPullRepository("common")
65+
if err != nil {
66+
log.Errorf("error while pulling the common templates [" + err.Error() + "]")
67+
return err
68+
}
69+
for _, node := range coreProject.CompageJSON.Nodes {
70+
// make sure that the latest template is pulled
71+
err = CloneOrPullRepository(node.Language)
72+
if err != nil {
73+
log.Errorf("error while pulling the template [" + err.Error() + "]")
74+
return err
75+
}
76+
log.Debugf("template pulled successfully for language %s", node.Language)
5977
}
6078

6179
// triggers project generation, process the request
6280
err0 := handlers.Handle(coreProject)
6381
if err0 != nil {
6482
log.Errorf("error while generating the project [" + err0.Error() + "]")
65-
return
83+
return err
6684
}
6785
log.Infof("project generated successfully at %s", utils.GetProjectDirectoryName(project.Name))
86+
return nil
6887
}

cmd/git-utils.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"github.com/go-git/go-git/v5"
6+
"github.com/intelops/compage/internal/utils"
7+
log "github.com/sirupsen/logrus"
8+
"os"
9+
)
10+
11+
func CloneOrPullRepository(language string) error {
12+
repoURL, repositoryPath, err := getRepositoryURLByLanguage(language)
13+
if err != nil {
14+
log.Errorf("error:%v", err)
15+
return err
16+
}
17+
if repoURL == "" || repositoryPath == "" {
18+
log.Errorf("language %s not supported", language)
19+
return errors.New("language not supported")
20+
}
21+
22+
exists, err := utils.DirectoryExists(repositoryPath)
23+
if err != nil {
24+
log.Errorf("error checking directory existence: %v", err)
25+
return err
26+
}
27+
if exists {
28+
log.Debugf("directory %s exists.\n", repositoryPath)
29+
return pullExistingRepository(repositoryPath)
30+
}
31+
log.Debugf("directory %s does not exist.\n", repositoryPath)
32+
log.Infof("cloning repository %s into %s\n", repoURL, repositoryPath)
33+
return cloneNewRepository(repoURL, repositoryPath)
34+
}
35+
36+
func getRepositoryURLByLanguage(language string) (string, string, error) {
37+
userHomeDir, err := os.UserHomeDir()
38+
if err != nil {
39+
return "", "", err
40+
}
41+
repositoryPath := userHomeDir + "/.compage/templates/compage-template-" + language
42+
repositoryURL := "https://github.com/intelops/compage-template-" + language + ".git"
43+
if language == "common" {
44+
repositoryPath = userHomeDir + "/.compage/templates/common-templates"
45+
return "https://github.com/intelops/common-templates.git", repositoryPath, nil
46+
}
47+
return repositoryURL, repositoryPath, nil
48+
}
49+
50+
func cloneNewRepository(repoURL string, cloneDir string) error {
51+
_, err := git.PlainClone(cloneDir, false, &git.CloneOptions{
52+
URL: repoURL,
53+
Progress: os.Stdout,
54+
})
55+
if err != nil {
56+
log.Errorf("error:%v", err)
57+
return err
58+
}
59+
log.Infof("repository[%s] cloned successfully.", repoURL)
60+
return nil
61+
}
62+
63+
func pullExistingRepository(existingRepositoryPath string) error {
64+
r, err := git.PlainOpen(existingRepositoryPath)
65+
if err != nil {
66+
log.Errorf("error:%v", err)
67+
return err
68+
}
69+
// Get the working directory for the repository
70+
w, err := r.Worktree()
71+
if err != nil {
72+
log.Errorf("error:%v", err)
73+
return err
74+
}
75+
// Pull the latest changes from the origin remote and merge into the current branch
76+
log.Info("git pull origin")
77+
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
78+
if err != nil {
79+
if errors.Is(err, git.NoErrAlreadyUpToDate) {
80+
// This is a special error that means we don't have any new changes
81+
log.Info(err)
82+
return nil
83+
}
84+
log.Errorf("error:%v", err)
85+
return err
86+
}
87+
// Print the latest commit that was just pulled
88+
ref, err := r.Head()
89+
if err != nil {
90+
log.Errorf("error:%v", err)
91+
return err
92+
}
93+
commit, err := r.CommitObject(ref.Hash())
94+
if err != nil {
95+
log.Errorf("error:%v", err)
96+
return err
97+
}
98+
log.Info(commit)
99+
return nil
100+
}

cmd/init.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,45 @@ var initCmd = &cobra.Command{
2525
2626
You can change the file as per your needs and then run the compage generate command to generate the code.`,
2727
Run: func(cmd *cobra.Command, args []string) {
28-
createOrUpdateDefaultConfigFile()
28+
err := createOrUpdateDefaultConfigFile()
29+
cobra.CheckErr(err)
2930
},
3031
}
3132

32-
func createOrUpdateDefaultConfigFile() {
33+
func createOrUpdateDefaultConfigFile() error {
3334
// create default config file
3435
configFilePath := "config.yaml"
3536
_, err := os.Stat(configFilePath)
3637
if err == nil {
3738
log.Warnf("config file already exists at %s", configFilePath)
3839
if !overwriteConfigFile {
3940
log.Infof("skipping config file creation")
40-
return
41+
return err
4142
}
4243
log.Infof("overwriting the config file")
4344
err = os.Remove(configFilePath)
4445
if err != nil {
45-
log.Warnf("error while removing the config file %s", err)
46+
log.Errorf("error while removing the config file %s", err)
4647
cobra.CheckErr(err)
4748
}
4849
}
4950

5051
_, err = os.Create(configFilePath)
51-
cobra.CheckErr(err)
52+
if err != nil {
53+
log.Errorf("error while creating the config file %s", err)
54+
return err
55+
}
5256
contentData, err := Content.ReadFile("config.yaml.tmpl")
53-
cobra.CheckErr(err)
57+
if err != nil {
58+
log.Errorf("error while reading the config file %s", err)
59+
return err
60+
}
5461
// copy the default config file and use go template to replace the values
5562
err = os.WriteFile(configFilePath, contentData, 0644)
56-
cobra.CheckErr(err)
57-
63+
if err != nil {
64+
log.Errorf("error while creating the config file %s", err)
65+
return err
66+
}
5867
var filePaths []*string
5968
filePaths = append(filePaths, &configFilePath)
6069
data := make(map[string]interface{})
@@ -78,8 +87,12 @@ func createOrUpdateDefaultConfigFile() {
7887
data["IsRestServer"] = true
7988
}
8089
err = executor.Execute(filePaths, data)
81-
cobra.CheckErr(err)
90+
if err != nil {
91+
log.Errorf("error while creating the config file %s", err)
92+
return err
93+
}
8294
log.Infof("config file created at %s", configFilePath)
95+
return nil
8396
}
8497

8598
func init() {

cmd/models/config.go

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

33
import (
4+
log "github.com/sirupsen/logrus"
45
"gopkg.in/yaml.v3"
56
"os"
67
)
@@ -31,12 +32,14 @@ type Project struct {
3132
func ReadConfigYAMLFile(configFile string) (*Project, error) {
3233
data, err := os.ReadFile(configFile)
3334
if err != nil {
35+
log.Errorf("error reading config file: %v", err)
3436
return nil, err
3537
}
3638

3739
var project Project
3840
// Unmarshal YAML data into the provided struct
3941
if err := yaml.Unmarshal(data, &project); err != nil {
42+
log.Errorf("error unmarshalling YAML data: %v", err)
4043
return nil, err
4144
}
4245

0 commit comments

Comments
 (0)