feat(scaffolder): Dynamically load the files from disk
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user