package apply import ( "fmt" "os" "path/filepath" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "novit.tech/direktil/pkg/config" ) // Files writes the files from the given config func Files(cfgFiles []config.FileDef, prefix string, filters ...string) error { 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 { 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") 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++ } } } 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) { return }