Skip to content

Commit

Permalink
feat #3: add upsert mode
Browse files Browse the repository at this point in the history
  • Loading branch information
graytonio committed Apr 13, 2024
1 parent f176fd7 commit 37bb0d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var rootCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
if verbose {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetReportCaller(true)
}

conf, err := config.LoadConfig(configPath)
Expand Down
3 changes: 3 additions & 0 deletions lib/config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type Destination struct {

// Root path of output. For git type relative to repo root
Path string `mapstructure:"path"`

// Do not delete any existing files in destination only template and update specified files
UpsertMode bool `mapstructure:"upsert"`
}

type Config struct {
Expand Down
46 changes: 41 additions & 5 deletions lib/templ/templ.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"text/template"
Expand All @@ -27,8 +28,8 @@ type TemplateEngine struct {
FlagProvider *openfeature.Client
RootPath string

files []string
dest config.Destination
files []string
dest config.Destination
funcMap map[string]any

gitRepo *git.Repository
Expand Down Expand Up @@ -106,10 +107,11 @@ func (te *TemplateEngine) finalizeGitRepo() error {

stat, err := w.Status()
if err != nil {
return err
return err
}

if stat.IsClean() {
logrus.Debug("noting to commit")
return nil
}

Expand All @@ -119,7 +121,7 @@ func (te *TemplateEngine) finalizeGitRepo() error {
if err != nil {
return err
}

_, err = w.Commit("flagops: Built templates", &git.CommitOptions{
AllowEmptyCommits: false,
Author: &object.Signature{
Expand Down Expand Up @@ -163,8 +165,42 @@ func (te *TemplateEngine) getFileOutputDestination(originalPath string) (string,
return path.Join(te.dest.Path, relPath), nil
}

func (te *TemplateEngine) cleanOutputDestination(path string) error {
stat, err := te.DestinationFilesystem.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}

if !stat.IsDir() {
return os.Remove(path)
}

names, err := te.DestinationFilesystem.ReadDir(path)
if err != nil {
return err
}

for _, name := range names {
err = util.RemoveAll(te.DestinationFilesystem, filepath.Join(path, name.Name()))
if err != nil {
return err
}
}
return nil
}

// Iterates each file in the engine and writes it to the destination
func (te *TemplateEngine) Execute() error {
if !te.dest.UpsertMode {
logrus.WithField("upsert_mode", te.dest.UpsertMode).Debug("cleaning output directory")
err := te.cleanOutputDestination(te.dest.Path)
if err != nil {
return err
}
}
for _, file := range te.files {
err := te.executeFileTemplate(file)
if err != nil {
Expand Down Expand Up @@ -209,4 +245,4 @@ func (te *TemplateEngine) executeFileTemplate(path string) error {
}

return nil
}
}

0 comments on commit 37bb0d2

Please sign in to comment.