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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -34,7 +35,11 @@ func rewriteBuildpacksFile(sourceWorkDir string) error {
|
|||||||
buildpacks[i] = buildpack
|
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) {
|
func validBuildpackURL(buildpack string) (string, error) {
|
||||||
|
|||||||
@@ -217,33 +217,45 @@ func TouchFile(filename string) error {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
file.Chmod(mode)
|
if err := file.Chmod(mode); err != nil {
|
||||||
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 {
|
|
||||||
return err
|
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)
|
w := bufio.NewWriter(file)
|
||||||
for _, line := range lines {
|
for _, line := range input.Lines {
|
||||||
fmt.Fprintln(w, line)
|
fmt.Fprintln(w, line)
|
||||||
}
|
}
|
||||||
if err = w.Flush(); err != nil {
|
if err = w.Flush(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
file.Chmod(mode)
|
if err := file.Chmod(input.Mode); err != nil {
|
||||||
SetPermissions(filename, mode)
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := SetPermissions(input.Filename, input.Mode); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,5 +32,9 @@ func touchPhaseFile(appName string, phase string) error {
|
|||||||
|
|
||||||
func writeDockerOptionsForPhase(appName string, phase string, options []string) error {
|
func writeDockerOptionsForPhase(appName string, phase string, options []string) error {
|
||||||
phaseFilePath := getPhaseFilePath(appName, phase)
|
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"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -224,7 +225,11 @@ func writeVectorConfig() error {
|
|||||||
b = bytes.Replace(b, []byte("\\u002B"), []byte("+"), -1)
|
b = bytes.Replace(b, []byte("\\u002B"), []byte("+"), -1)
|
||||||
|
|
||||||
vectorConfig := filepath.Join(common.GetDataDirectory("logs"), "vector.json")
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user