This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566package processor
import ( "fmt" "io" "os" "path/filepath"
"github.com/aottr/nox/internal/config" "github.com/aottr/nox/internal/constants")
func WriteToFile(data []byte, file config.FileConfig) error { path := file.Output if path == "" { // Default output filename if none specified, e.g. replace .age with .env path = filepath.Base(file.Path) fmt.Println(path) if filepath.Ext(path) == ".age" { path = path[:len(path)-4] + ".env" } } if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { return fmt.Errorf("failed to create directories for %s: %w", path, err) }
if err := os.WriteFile(path, data, 0600); err != nil { return fmt.Errorf("failed to write decrypted file to %s: %w", path, err) } return nil}
// IOWrapper is a generic wrapper for reading/writing files/STDIN/STDOUTfunc IOWrapper[T any](input, output string, additional T, process func([]byte, T) ([]byte, error)) error { var inputBytes []byte var err error
if input == constants.StandardInput { inputBytes, err = io.ReadAll(os.Stdin) if err != nil { return err } } else { inputBytes, err = os.ReadFile(input) if err != nil { return err } }
outbutBytes, err := process(inputBytes, additional) if err != nil { return err }
if output == constants.StandardOutput { if _, err = os.Stdout.Write(outbutBytes); err != nil { return err } } else { if err = os.WriteFile(output, outbutBytes, 0600); err != nil { return err } } return nil}