pkg/config/apply/files.go

97 lines
2.1 KiB
Go
Raw Normal View History

2024-01-20 13:00:01 +00:00
package apply
import (
"fmt"
"os"
"path/filepath"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2024-01-20 13:16:45 +00:00
"novit.tech/direktil/pkg/config"
2024-01-20 13:00:01 +00:00
)
// Files writes the files from the given config
func Files(cfgFiles []config.FileDef, prefix string, filters ...string) error {
2024-01-20 13:00:01 +00:00
accept := func(n string) bool { return true }
if len(filters) > 0 {
accept = func(n string) bool {
for _, filter := range filters {
if matched, err := filepath.Match(filter, n); err != nil {
log.Error().Err(err).Str("filter", filter).Msg("bad filter, ignored")
} else if matched {
return true
}
}
return false
}
}
failed := 0
for _, file := range cfgFiles {
2024-01-20 13:00:01 +00:00
if !accept(file.Path) {
continue
}
filePath := filepath.Join(prefix, file.Path)
mode := file.Mode
if mode == 0 {
mode = 0644
}
log := log.With().Str("file", filePath).Str("mode", fmt.Sprintf("%04o", mode)).Logger()
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
log.Error().Err(err).Msg("failed to create parent dir")
2024-01-20 13:00:01 +00:00
failed++
continue
}
switch {
case file.Symlink != "":
log := log.With().Str("target", file.Symlink).Logger()
if err := os.Symlink(file.Symlink, filePath); err == nil {
log.Info().Msg("created symlink")
} else {
log.Error().Err(err).Msg("failed to create symlink")
failed++
}
case file.Dir:
if err := os.Mkdir(filePath, mode); err == nil {
log.Info().Msg("created dir")
} else if os.IsExist(err) {
log.Warn().Msg("dir exist, ignoring error")
} else {
log.Error().Err(err).Msg("failed to create dir")
failed++
}
default:
content := []byte(file.Content)
if err := os.WriteFile(filePath, content, mode); err == nil {
log.Info().Msgf("wrote %d bytes", len(content))
} else {
log.Error().Err(err).Msg("write failed")
failed++
}
}
2024-01-20 13:00:01 +00:00
}
if failed != 0 {
err := fmt.Errorf("%d writes failed", failed)
log.Error().Err(err).Send()
return err
}
return nil
}
func writeFile(log zerolog.Logger, path string, content []byte, fileMode, dirMode os.FileMode) (err error) {
2024-01-20 13:00:01 +00:00
return
}