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 (
44 "bufio"
55 "fmt"
66 "os"
7 "path/filepath"
78 "reflect"
89 "strconv"
910 "strings"
@@ -88,12 +89,19 @@ func defaultConfigMap() map[string]string {
8889}
8990
9091// Create a config file with default values in key=value format
92// Ensure the directory exists before creating the file
9193func createDefaultConfig(path string) error {
9294 defaultConfig := defaultConfigMap()
9395
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
94102 file, err := os.Create(path)
95103 if err != nil {
96 return err
104 return fmt.Errorf("error creating file: %v", err)
97105 }
98106 defer file.Close()
99107
@@ -101,10 +109,13 @@ func createDefaultConfig(path string) error {
101109 for key, value := range defaultConfig {
102110 line := fmt.Sprintf("%s=%s\n", key, value)
103111 if _, err := writer.WriteString(line); err != nil {
104 return err
112 return fmt.Errorf("error writing to file: %v", err)
105113 }
106114 }
107 return writer.Flush()
115 if err := writer.Flush(); err != nil {
116 return fmt.Errorf("error flushing writer: %v", err)
117 }
118 return nil
108119}
109120
110121// Load config file from disk into a map (key=value format)