Skip to content

Commit

Permalink
fix(templates): now able to load templates from several directories
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Bétrancourt <[email protected]>
  • Loading branch information
rclsilver committed Oct 25, 2023
1 parent b116b6a commit cfbc8ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
7 changes: 2 additions & 5 deletions cmd/utask/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,8 @@ var rootCmd = &cobra.Command{
if err != nil {
return err
}
ds := strings.Split(utask.FTemplatesFolders, ":")
for _, dir := range ds {
if err := tasktemplate.LoadFromDir(dbp, dir); err != nil {
return err
}
if err := tasktemplate.LoadFromDir(dbp, strings.Split(utask.FTemplatesFolders, ":")...); err != nil {
return err
}
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
Expand Down
42 changes: 22 additions & 20 deletions models/tasktemplate/fromdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ var (

// LoadFromDir reads yaml-formatted task templates
// from a folder and upserts them in database
func LoadFromDir(dbp zesty.DBProvider, dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to open template directory %s: %s", dir, err)
}
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".yaml") {
continue
}
tmpl, err := os.ReadFile(path.Join(dir, file.Name()))
func LoadFromDir(dbp zesty.DBProvider, directories ...string) error {
for _, dir := range directories {
files, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to read template '%s': %s", file.Name(), err)
}
var tt TaskTemplate
if err := yaml.Unmarshal(tmpl, &tt); err != nil {
return fmt.Errorf("failed to unmarshal template '%s': '%s'", file.Name(), err)
return fmt.Errorf("failed to open template directory %s: %s", dir, err)
}
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".yaml") {
continue
}
tmpl, err := os.ReadFile(path.Join(dir, file.Name()))
if err != nil {
return fmt.Errorf("failed to read template '%s': %s", file.Name(), err)
}
var tt TaskTemplate
if err := yaml.Unmarshal(tmpl, &tt); err != nil {
return fmt.Errorf("failed to unmarshal template '%s': '%s'", file.Name(), err)
}

tt.Normalize()
tt.Normalize()

discoveredTemplates = append(discoveredTemplates, tt)
templateimport.AddTemplate(tt.Name)
discoveredTemplates = append(discoveredTemplates, tt)
templateimport.AddTemplate(tt.Name)
}
}

for _, tt := range discoveredTemplates {
Expand Down Expand Up @@ -89,12 +91,12 @@ func LoadFromDir(dbp zesty.DBProvider, dir string) error {
}
}
if !found {
if err = tt.Delete(dbp); err == nil {
if err := tt.Delete(dbp); err == nil {
logrus.Infof("Deleted task template %q", tt.Name)
continue
}
// unable to delete TaskTemplate, probably some old Tasks still in database, archiving it
tt, err = LoadFromID(dbp, tt.ID)
tt, err := LoadFromID(dbp, tt.ID)
if err != nil {
return fmt.Errorf("unable to load template %q for archiving: %s", tt.Name, err)
}
Expand Down

0 comments on commit cfbc8ee

Please sign in to comment.