refactor: allow setting a custom mode when writing a slice to a file
This commit is contained in:
@@ -3,6 +3,7 @@ package buildpacks
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -34,7 +35,11 @@ func rewriteBuildpacksFile(sourceWorkDir string) error {
|
||||
buildpacks[i] = buildpack
|
||||
}
|
||||
|
||||
return common.WriteSliceToFile(buildpacksPath, buildpacks)
|
||||
return common.WriteSliceToFile(common.WriteSliceToFileInput{
|
||||
Filename: buildpacksPath,
|
||||
Lines: buildpacks,
|
||||
Mode: os.FileMode(0600),
|
||||
})
|
||||
}
|
||||
|
||||
func validBuildpackURL(buildpack string) (string, error) {
|
||||
|
||||
@@ -217,33 +217,45 @@ func TouchFile(filename string) error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
file.Chmod(mode)
|
||||
SetPermissions(filename, mode)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteSliceToFile writes a slice of strings to a file
|
||||
func WriteSliceToFile(filename string, lines []string) error {
|
||||
mode := os.FileMode(0600)
|
||||
if strings.HasPrefix(filename, "/etc/sudoers.d/") {
|
||||
mode = os.FileMode(0440)
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
|
||||
if err != nil {
|
||||
if err := file.Chmod(mode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := SetPermissions(filename, mode); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type WriteSliceToFileInput struct {
|
||||
Filename string
|
||||
Lines []string
|
||||
Mode os.FileMode
|
||||
}
|
||||
|
||||
// WriteSliceToFile writes a slice of strings to a file
|
||||
func WriteSliceToFile(input WriteSliceToFileInput) error {
|
||||
file, err := os.OpenFile(input.Filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, input.Mode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
w := bufio.NewWriter(file)
|
||||
for _, line := range lines {
|
||||
for _, line := range input.Lines {
|
||||
fmt.Fprintln(w, line)
|
||||
}
|
||||
if err = w.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file.Chmod(mode)
|
||||
SetPermissions(filename, mode)
|
||||
if err := file.Chmod(input.Mode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := SetPermissions(input.Filename, input.Mode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -32,5 +32,9 @@ func touchPhaseFile(appName string, phase string) error {
|
||||
|
||||
func writeDockerOptionsForPhase(appName string, phase string, options []string) error {
|
||||
phaseFilePath := getPhaseFilePath(appName, phase)
|
||||
return common.WriteSliceToFile(phaseFilePath, options)
|
||||
return common.WriteSliceToFile(common.WriteSliceToFileInput{
|
||||
Filename: phaseFilePath,
|
||||
Lines: options,
|
||||
Mode: os.FileMode(0600),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -224,7 +225,11 @@ func writeVectorConfig() error {
|
||||
b = bytes.Replace(b, []byte("\\u002B"), []byte("+"), -1)
|
||||
|
||||
vectorConfig := filepath.Join(common.GetDataDirectory("logs"), "vector.json")
|
||||
if err := common.WriteSliceToFile(vectorConfig, []string{string(b)}); err != nil {
|
||||
if err := common.WriteSliceToFile(common.WriteSliceToFileInput{
|
||||
Filename: vectorConfig,
|
||||
Lines: []string{string(b)},
|
||||
Mode: os.FileMode(0600),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user