Skip to content

Commit

Permalink
enhancement: added new way of templates inclusion using pullTemplates…
Browse files Browse the repository at this point in the history
… command
  • Loading branch information
mahendraintelops committed Nov 30, 2023
1 parent 40fa292 commit 706dabf
Show file tree
Hide file tree
Showing 40 changed files with 446 additions and 263 deletions.
6 changes: 6 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func GenerateCode() {
}

// pull all required templates
// pull the common templates
err = CloneOrPullRepository("common")
if err != nil {
log.Errorf("error while pulling the common templates [" + err.Error() + "]")
return
}
for _, node := range coreProject.CompageJSON.Nodes {
// make sure that the latest template is pulled
err = CloneOrPullRepository(node.Language)
Expand Down
39 changes: 23 additions & 16 deletions cmd/git-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (
)

func CloneOrPullRepository(language string) error {
repoURL := getRepositoryURLByLanguage(language)
if repoURL == "" {
log.Errorf("language %s not supported", language)
return errors.New("language not supported")
}
userHomeDir, err := os.UserHomeDir()
repoURL, repositoryPath, err := getRepositoryURLByLanguage(language)
if err != nil {
log.Errorf("error:%v", err)
return err
}
repositoryPath := userHomeDir + "/.compage/templates/compage-template-" + language
if repoURL == "" || repositoryPath == "" {
log.Errorf("language %s not supported", language)
return errors.New("language not supported")
}

exists, err := utils.DirectoryExists(repositoryPath)
if err != nil {
log.Errorf("error checking directory existence: %v", err)
Expand All @@ -36,23 +35,31 @@ func CloneOrPullRepository(language string) error {
return nil
}

func getRepositoryURLByLanguage(language string) string {
func getRepositoryURLByLanguage(language string) (string, string, error) {
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", "", err
}
repositoryPath := userHomeDir + "/.compage/templates/compage-template-" + language
if language == "go" {
return "https://github.com/intelops/compage-template-go.git"
return "https://github.com/intelops/compage-template-go.git", repositoryPath, nil
} else if language == "python" {
return "https://github.com/intelops/compage-template-python.git"
return "https://github.com/intelops/compage-template-python.git", repositoryPath, nil
} else if language == "java" {
return "https://github.com/intelops/compage-template-java.git"
return "https://github.com/intelops/compage-template-java.git", repositoryPath, nil
} else if language == "javascript" {
return "https://github.com/intelops/compage-template-javascript.git"
return "https://github.com/intelops/compage-template-javascript.git", repositoryPath, nil
} else if language == "ruby" {
return "https://github.com/intelops/compage-template-ruby.git"
return "https://github.com/intelops/compage-template-ruby.git", repositoryPath, nil
} else if language == "rust" {
return "https://github.com/intelops/compage-template-rust.git"
return "https://github.com/intelops/compage-template-rust.git", repositoryPath, nil
} else if language == "typescript" {
return "https://github.com/intelops/compage-template-typescript.git"
return "https://github.com/intelops/compage-template-typescript.git", repositoryPath, nil
} else if language == "common" {
repositoryPath = userHomeDir + "/.compage/templates/common-templates"
return "https://github.com/intelops/common-templates.git", repositoryPath, nil
}
return ""
return "", "", nil
}

func cloneNewRepository(repoURL string, cloneDir string) {
Expand Down
3 changes: 3 additions & 0 deletions cmd/pullTemplates.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var pullTemplatesCmd = &cobra.Command{
Short: "Pulls the compage supported templates in the ~/.compage/templates directory",
Long: `Compage supports multiple templates for different languages. You can pull just the required template by lana or all the templates.`,
Run: func(cmd *cobra.Command, args []string) {
// common template is required for all the languages
err := CloneOrPullRepository("common")
cobra.CheckErr(err)
if all {
err := CloneOrPullRepository("go")
cobra.CheckErr(err)
Expand Down
11 changes: 8 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"embed"
"fmt"
log "github.com/sirupsen/logrus"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -54,11 +55,11 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
userHomeDir, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".compage" (without extension).
viper.AddConfigPath(home + "/.compage")
viper.AddConfigPath(userHomeDir + "/.compage")
viper.SetConfigType("yaml")
viper.SetConfigName("config")
}
Expand All @@ -67,6 +68,10 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
_, err = fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
if err != nil {
log.Errorf("error while printing config file path [%s]", err.Error())
return
}
}
}
45 changes: 36 additions & 9 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/intelops/compage/config"
project "github.com/intelops/compage/gen/api/v1"
server "github.com/intelops/compage/grpc"
"github.com/intelops/compage/internal/utils"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -48,6 +49,23 @@ This command will start thr gRPC server and allow the gRPC clients to get connec
}
}()

err := CloneOrPullRepository("common")
cobra.CheckErr(err)
err = CloneOrPullRepository("go")
cobra.CheckErr(err)
err = CloneOrPullRepository("python")
cobra.CheckErr(err)
err = CloneOrPullRepository("java")
cobra.CheckErr(err)
err = CloneOrPullRepository("javascript")
cobra.CheckErr(err)
err = CloneOrPullRepository("ruby")
cobra.CheckErr(err)
err = CloneOrPullRepository("rust")
cobra.CheckErr(err)
err = CloneOrPullRepository("typescript")
cobra.CheckErr(err)

// check if the git submodules have been pulled (mainly need to check this on developer's machine)
if checkIfGitSubmodulesExist() {
startGrpcServer()
Expand All @@ -72,23 +90,30 @@ func init() {
}

func checkIfGitSubmodulesExist() bool {
templatesPath := "templates"
// currently available templates
templates := []string{"compage-templates", "compage-template-go", "compage-template-java", "compage-template-python", "compage-template-javascript", "compage-template-ruby", "compage-template-rust", "compage-template-typescript"}
templates := []string{"common-templates", "compage-template-go", "compage-template-java", "compage-template-python", "compage-template-javascript", "compage-template-ruby", "compage-template-rust", "compage-template-typescript"}

// read the templates directory and list down files in it.
files, err := os.ReadDir(templatesPath)
baseTemplateRootPath, err := utils.GetBaseTemplateRootPath()
if err != nil {
log.Fatal(err)
log.Error("error while getting base template root path [" + err.Error() + "]")
return false
}
files, err := os.ReadDir(baseTemplateRootPath)
if err != nil {
log.Error("error while reading templates directory [" + err.Error() + "]")
return false
}
// iterate over files in templates directory
for _, file := range files {
// check if the item in templates directory is directory,
// and it's a valid template (by looking into available templates)
if file.IsDir() && slices.Contains(templates, file.Name()) {
// check in specific template directory.
filesInDir, err0 := os.ReadDir(templatesPath + "/" + file.Name())
filesInDir, err0 := os.ReadDir(baseTemplateRootPath + "/" + file.Name())
if err0 != nil {
log.Fatal(err0)
log.Error("error while reading templates directory [" + err0.Error() + "]")
return false
}
// if there are no directories or files in specific template, this means that the `git submodules update --remote` command wasn't fired before.
if len(filesInDir) <= 0 {
Expand All @@ -103,7 +128,8 @@ func checkIfGitSubmodulesExist() bool {
func startGrpcServer() {
listener, err := net.Listen("tcp", "0.0.0.0:50051")
if err != nil {
log.Fatalf("failed to serve: %v", err)
log.Errorf("failed to listen: %v", err)
return
}
log.Println("started gRPC server on '0.0.0.0:50051'")
grpcServer := grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler()))
Expand All @@ -115,8 +141,9 @@ func startGrpcServer() {
reflection.Register(grpcServer)

go func() {
if err := grpcServer.Serve(listener); err != nil {
log.Printf("grpcServer.Serve: %v", err)
if err = grpcServer.Serve(listener); err != nil {
log.Errorf("failed to serve: %v", err)
return
}
}()

Expand Down
2 changes: 1 addition & 1 deletion config/grpc-opentel-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func InitGrpcTracer(serviceName, collectorURL, insecure string) *sdktrace.Tracer
)

if err != nil {
log.Debugf("error while configuring opentel, %v", err)
log.Errorf("error while configuring opentel, %v", err)
os.Exit(1)
}

Expand Down
12 changes: 6 additions & 6 deletions grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,36 @@ func (s *server) GenerateCode(projectRequest *project.GenerateCodeRequest, serve
// converts to core project
coreProject, err := grpc.GetProject(projectRequest)
if err != nil {
log.Debugf("err : %s", err)
log.Errorf("err : %s", err)
return status.Errorf(codes.InvalidArgument,
"error while converting request to project ["+err.Error()+"]")
}

// triggers project generation, process the request
if err0 := handlers.Handle(coreProject); err0 != nil {
log.Debugf("err : %s", err0)
log.Errorf("err : %s", err0)
return status.Errorf(codes.InvalidArgument,
"error while generating the project ["+err0.Error()+"]")
}

// CreateTarFile creates tar file for the project generated
if err1 := taroperations.CreateTarFile(coreProject.Name, utils.GetProjectDirectoryName(projectRequest.GetProjectName())); err1 != nil {
log.Debugf("err : %s", err1)
log.Errorf("err : %s", err1)
return status.Errorf(codes.Internal,
"error while converting request to project ["+err1.Error()+"]")
}

// delete tmp/project-name directory
defer func(name string) {
if err2 := os.RemoveAll(name); err2 != nil {
log.Debugf("err : %s", err2)
log.Errorf("err : %s", err2)
}
}(utils.GetProjectDirectoryName(projectRequest.GetProjectName()))

// delete just file
defer func(name string) {
if err3 := os.Remove(name); err3 != nil {
log.Debugf("err : %s", err3)
log.Errorf("err : %s", err3)
}
}(taroperations.GetProjectTarFilePath(projectRequest.GetProjectName()))

Expand All @@ -76,7 +76,7 @@ func (s *server) RegenerateCode(generateCodeRequest *project.GenerateCodeRequest
// GenerateCode
err := taroperations.CreateTarFile(generateCodeRequest.ProjectName, utils.GetProjectDirectoryName(generateCodeRequest.GetProjectName()))
if err != nil {
log.Debugf("err : %s", err)
log.Errorf("err : %s", err)
return status.Errorf(codes.InvalidArgument,
"error while creating a tar file ["+err.Error()+"]")
}
Expand Down
Loading

0 comments on commit 706dabf

Please sign in to comment.