|
1 | 1 | package common |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "gopkg.in/yaml.v3" |
5 | 6 | "os" |
6 | 7 | "path" |
@@ -28,15 +29,44 @@ func getConfigPath(isServer bool) string { |
28 | 29 | } else { |
29 | 30 | configPath = path.Join(*ConfigPath, "go-public-client.yaml") |
30 | 31 | } |
| 32 | + // If config file is not found in the specified path, try to find it in the default path |
| 33 | + if _, err := os.Stat(*ConfigPath); os.IsNotExist(err) { |
| 34 | + basePath := path.Join(GetHomeDir(), ".config") |
| 35 | + var configPath2 string |
| 36 | + if isServer { |
| 37 | + configPath2 = path.Join(basePath, "go-public-server.yaml") |
| 38 | + } else { |
| 39 | + configPath2 = path.Join(basePath, "go-public-client.yaml") |
| 40 | + } |
| 41 | + if _, err := os.Stat(configPath2); err == nil { |
| 42 | + configPath = configPath2 |
| 43 | + } |
| 44 | + } |
31 | 45 | return configPath |
32 | 46 | } |
33 | 47 |
|
34 | 48 | func InitConfigFile(isServer bool) { |
35 | 49 | configPath := getConfigPath(isServer) |
36 | 50 | if _, err := os.Stat(configPath); err == nil { |
37 | | - println("Config file already exists.") |
| 51 | + fmt.Println("Config file already exists at: " + configPath) |
38 | 52 | os.Exit(1) |
39 | 53 | } |
| 54 | + if !isServer { |
| 55 | + fmt.Print("Where do you want to save the config file? (default: ~/.config) ") |
| 56 | + var input string |
| 57 | + _, _ = fmt.Scanln(&input) |
| 58 | + if input == "" { |
| 59 | + input = path.Join(GetHomeDir(), ".config") |
| 60 | + } |
| 61 | + if _, err := os.Stat(input); os.IsNotExist(err) { |
| 62 | + err := os.Mkdir(input, 0700) |
| 63 | + if err != nil { |
| 64 | + fmt.Println("Failed to create directory: " + input) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + } |
| 68 | + configPath = path.Join(input, "go-public-client.yaml") |
| 69 | + } |
40 | 70 | defaultServerConfig := serverConfig{ |
41 | 71 | Port: 6871, |
42 | 72 | Token: GenerateToken(), |
|
0 commit comments