feat(scaffolder): Dynamically load the files from disk

This commit is contained in:
blam
2020-02-05 16:45:45 +01:00
parent e1ddbe60e9
commit 600a001d6f
2 changed files with 44 additions and 13 deletions
+17 -10
View File
@@ -14,17 +14,24 @@ type Server struct {
// GetAllTemplates returns the local templatess
func (s *Server) GetAllTemplates(ctx context.Context, req *pb.Empty) (*pb.GetAllTemplatesReply, error) {
_, err := s.Repository.Load()
template := &pb.Template{
Id: "react-ssr-template",
Name: "React SSR Template",
User: &identity.User{
Id: "spotify",
Name: "Spotify",
},
}
definitions, err := s.Repository.Load()
var templates []*pb.Template
templates := []*pb.Template{template}
for _, definition := range definitions {
template := &pb.Template{
Id: definition.ID,
Name: definition.Name,
Description: definition.Description,
// need to actually call the idenity service here to get the
// actual user and propgate back when needed.
User: &identity.User{
Id: "spotify",
Name: "Spotify",
},
}
templates = append(templates, template)
}
return &pb.GetAllTemplatesReply{
Templates: templates,
+27 -3
View File
@@ -1,7 +1,9 @@
package repository
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
)
@@ -18,8 +20,30 @@ type TemplateDefinition struct {
// Load will return all the Repository templates
func (s *Repository) Load() ([]*TemplateDefinition, error) {
matches, err := filepath.Glob("templates/**/template-info.json")
fmt.Println(matches)
templateInfoFilePaths, err := filepath.Glob("templates/**/template-info.json")
var templateDefinitions []*TemplateDefinition
return []*TemplateDefinition{}, err
if err != nil {
fmt.Errorf("failed to load template-info files")
return nil, err
}
for _, templatePath := range templateInfoFilePaths {
content, err := ioutil.ReadFile(templatePath)
if err != nil {
fmt.Errorf("failed to load path for %s", templatePath)
continue
}
definition := TemplateDefinition{}
err = json.Unmarshal([]byte(content), &definition)
if err != nil {
fmt.Errorf("failed to unmarshal %s", templatePath)
continue
}
templateDefinitions = append(templateDefinitions, &definition)
}
return templateDefinitions, err
}