Commit b5a697505e

b5a697505e256734e18d7b807baf93332be8d15e

parent: 697ad94275

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-22 17:02 UTC

Create config works

Layout: unified · split

internal/config.go +14 −3
@@ -4,6 +4,7 @@ import (
4 "bufio" 4 "bufio"
5 "fmt" 5 "fmt"
6 "os" 6 "os"
7 "path/filepath"
7 "reflect" 8 "reflect"
8 "strconv" 9 "strconv"
9 "strings" 10 "strings"
@@ -88,12 +89,19 @@ func defaultConfigMap() map[string]string {
88} 89}
89 90
90// Create a config file with default values in key=value format 91// Create a config file with default values in key=value format
92// Ensure the directory exists before creating the file
91func createDefaultConfig(path string) error { 93func createDefaultConfig(path string) error {
92 defaultConfig := defaultConfigMap() 94 defaultConfig := defaultConfigMap()
93 95
96 // Ensure the directory exists
97 dir := filepath.Dir(path)
98 if err := os.MkdirAll(dir, 0755); err != nil {
99 return fmt.Errorf("error creating directory: %v", err)
100 }
101
94 file, err := os.Create(path) 102 file, err := os.Create(path)
95 if err != nil { 103 if err != nil {
96 return err 104 return fmt.Errorf("error creating file: %v", err)
97 } 105 }
98 defer file.Close() 106 defer file.Close()
99 107
@@ -101,10 +109,13 @@ func createDefaultConfig(path string) error {
101 for key, value := range defaultConfig { 109 for key, value := range defaultConfig {
102 line := fmt.Sprintf("%s=%s\n", key, value) 110 line := fmt.Sprintf("%s=%s\n", key, value)
103 if _, err := writer.WriteString(line); err != nil { 111 if _, err := writer.WriteString(line); err != nil {
104 return err 112 return fmt.Errorf("error writing to file: %v", err)
105 } 113 }
106 } 114 }
107 return writer.Flush() 115 if err := writer.Flush(); err != nil {
116 return fmt.Errorf("error flushing writer: %v", err)
117 }
118 return nil
108} 119}
109 120
110// Load config file from disk into a map (key=value format) 121// Load config file from disk into a map (key=value format)