refactor: use type-specific functions for writing contents to a file
This commit is contained in:
@@ -213,6 +213,7 @@ func TouchFile(filename string) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteSliceToFile writes a slice of strings to a file
|
||||||
type WriteSliceToFileInput struct {
|
type WriteSliceToFileInput struct {
|
||||||
Filename string
|
Filename string
|
||||||
GroupName string
|
GroupName string
|
||||||
@@ -223,17 +224,53 @@ type WriteSliceToFileInput struct {
|
|||||||
|
|
||||||
// WriteSliceToFile writes a slice of strings to a file
|
// WriteSliceToFile writes a slice of strings to a file
|
||||||
func WriteSliceToFile(input WriteSliceToFileInput) error {
|
func WriteSliceToFile(input WriteSliceToFileInput) error {
|
||||||
|
return WriteBytesToFile(WriteBytesToFileInput{
|
||||||
|
Bytes: []byte(strings.TrimSuffix(strings.Join(input.Lines, "\n"), "\n") + "\n"),
|
||||||
|
Filename: input.Filename,
|
||||||
|
GroupName: input.GroupName,
|
||||||
|
Mode: input.Mode,
|
||||||
|
Username: input.Username,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteStringToFile writes a string to a file
|
||||||
|
type WriteStringToFileInput struct {
|
||||||
|
Content string
|
||||||
|
Filename string
|
||||||
|
GroupName string
|
||||||
|
Mode os.FileMode
|
||||||
|
Username string
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteStringToFile writes a string to a file
|
||||||
|
func WriteStringToFile(input WriteStringToFileInput) error {
|
||||||
|
return WriteBytesToFile(WriteBytesToFileInput{
|
||||||
|
Bytes: []byte(input.Content),
|
||||||
|
Filename: input.Filename,
|
||||||
|
GroupName: input.GroupName,
|
||||||
|
Mode: input.Mode,
|
||||||
|
Username: input.Username,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteBytesToFileInput writes a byte array to a file
|
||||||
|
type WriteBytesToFileInput struct {
|
||||||
|
Bytes []byte
|
||||||
|
Filename string
|
||||||
|
GroupName string
|
||||||
|
Mode os.FileMode
|
||||||
|
Username string
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteBytesToFile writes a byte array to a file
|
||||||
|
func WriteBytesToFile(input WriteBytesToFileInput) error {
|
||||||
file, err := os.OpenFile(input.Filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, input.Mode)
|
file, err := os.OpenFile(input.Filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, input.Mode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
w := bufio.NewWriter(file)
|
if _, err := file.Write(input.Bytes); err != nil {
|
||||||
for _, line := range input.Lines {
|
|
||||||
fmt.Fprintln(w, line)
|
|
||||||
}
|
|
||||||
if err = w.Flush(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -295,9 +295,9 @@ 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(common.WriteSliceToFileInput{
|
if err := common.WriteBytesToFile(common.WriteBytesToFileInput{
|
||||||
|
Bytes: b,
|
||||||
Filename: vectorConfig,
|
Filename: vectorConfig,
|
||||||
Lines: []string{string(b)},
|
|
||||||
Mode: os.FileMode(0600),
|
Mode: os.FileMode(0600),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -755,10 +755,10 @@ func installHelperCommands(ctx context.Context) error {
|
|||||||
return fmt.Errorf("Unable to close %s file: %w", binaryName, err)
|
return fmt.Errorf("Unable to close %s file: %w", binaryName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = common.WriteSliceToFile(common.WriteSliceToFileInput{
|
err = common.WriteStringToFile(common.WriteStringToFileInput{
|
||||||
|
Content: resp.String(),
|
||||||
Filename: f.Name(),
|
Filename: f.Name(),
|
||||||
GroupName: "root",
|
GroupName: "root",
|
||||||
Lines: strings.Split(resp.String(), "\n"),
|
|
||||||
Mode: os.FileMode(0755),
|
Mode: os.FileMode(0755),
|
||||||
Username: "root",
|
Username: "root",
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -107,9 +107,9 @@ func CommandInitialize(serverIP string, taintScheduling bool) error {
|
|||||||
return fmt.Errorf("Unable to close k3s installer file: %w", err)
|
return fmt.Errorf("Unable to close k3s installer file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = common.WriteSliceToFile(common.WriteSliceToFileInput{
|
err = common.WriteStringToFile(common.WriteStringToFileInput{
|
||||||
|
Content: resp.String(),
|
||||||
Filename: f.Name(),
|
Filename: f.Name(),
|
||||||
Lines: strings.Split(resp.String(), "\n"),
|
|
||||||
Mode: os.FileMode(0755),
|
Mode: os.FileMode(0755),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user