this repo has no description
at main 65 lines 1.5 kB view raw
1package processor 2 3import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 9 "github.com/aottr/nox/internal/config" 10 "github.com/aottr/nox/internal/constants" 11) 12 13func WriteToFile(data []byte, file config.FileConfig) error { 14 path := file.Output 15 if path == "" { 16 // Default output filename if none specified, e.g. replace .age with .env 17 path = filepath.Base(file.Path) 18 fmt.Println(path) 19 if filepath.Ext(path) == ".age" { 20 path = path[:len(path)-4] + ".env" 21 } 22 } 23 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { 24 return fmt.Errorf("failed to create directories for %s: %w", path, err) 25 } 26 27 if err := os.WriteFile(path, data, 0600); err != nil { 28 return fmt.Errorf("failed to write decrypted file to %s: %w", path, err) 29 } 30 return nil 31} 32 33// IOWrapper is a generic wrapper for reading/writing files/STDIN/STDOUT 34func IOWrapper[T any](input, output string, additional T, process func([]byte, T) ([]byte, error)) error { 35 var inputBytes []byte 36 var err error 37 38 if input == constants.StandardInput { 39 inputBytes, err = io.ReadAll(os.Stdin) 40 if err != nil { 41 return err 42 } 43 } else { 44 inputBytes, err = os.ReadFile(input) 45 if err != nil { 46 return err 47 } 48 } 49 50 outbutBytes, err := process(inputBytes, additional) 51 if err != nil { 52 return err 53 } 54 55 if output == constants.StandardOutput { 56 if _, err = os.Stdout.Write(outbutBytes); err != nil { 57 return err 58 } 59 } else { 60 if err = os.WriteFile(output, outbutBytes, 0600); err != nil { 61 return err 62 } 63 } 64 return nil 65}