@@ -24,21 +24,13 @@ type ConfigProvider interface {
2424}
2525
2626type YamlConfigProvider struct {
27- // For testing purposes
28- homeDir string
27+ dirProvider DirectoryProvider
2928}
3029
31- func NewYamlConfigProvider () ConfigProvider {
32- return & YamlConfigProvider {}
33- }
34-
35- // GetUserHomeDir returns the user's home directory
36- // This is extracted to a method to make it testable
37- func (cp * YamlConfigProvider ) GetUserHomeDir () (string , error ) {
38- if cp .homeDir != "" {
39- return cp .homeDir , nil
30+ func NewYamlConfigProvider (dirProvider DirectoryProvider ) ConfigProvider {
31+ return & YamlConfigProvider {
32+ dirProvider : dirProvider ,
4033 }
41- return os .UserHomeDir ()
4234}
4335
4436func (cp * YamlConfigProvider ) StoreInstallationToken (token string ) error {
@@ -61,13 +53,11 @@ func (cp *YamlConfigProvider) GetInstallationToken() (string, error) {
6153}
6254
6355func (cp * YamlConfigProvider ) DeleteConfig () error {
64- home , err := cp .GetUserHomeDir ()
56+ configPath , err := cp .dirProvider . GetConfigFile ()
6557 if err != nil {
66- return fmt .Errorf ("failed to get user home directory : %w" , err )
58+ return fmt .Errorf ("failed to get config file path : %w" , err )
6759 }
6860
69- configPath := filepath .Join (home , ".config" , "mbvpn" , "config.yml" )
70-
7161 err = os .Remove (configPath )
7262 if err != nil {
7363 return fmt .Errorf ("failed to delete config file: %w" , err )
@@ -77,12 +67,11 @@ func (cp *YamlConfigProvider) DeleteConfig() error {
7767}
7868
7969func (cp * YamlConfigProvider ) update (cfg Config ) error {
80- home , err := cp .GetUserHomeDir ()
70+ configPath , err := cp .dirProvider . GetConfigFile ()
8171 if err != nil {
82- return fmt .Errorf ("failed to get user home directory : %w" , err )
72+ return fmt .Errorf ("failed to get config file path : %w" , err )
8373 }
8474
85- configPath := filepath .Join (home , ".config" , "mbvpn" , "config.yml" )
8675 f , err := os .OpenFile (configPath , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , 0600 )
8776 if err != nil {
8877 return fmt .Errorf ("failed to open config file for writing: %w" , err )
@@ -94,7 +83,7 @@ func (cp *YamlConfigProvider) update(cfg Config) error {
9483 if err != nil {
9584 return fmt .Errorf ("failed to encode config data: %w" , err )
9685 }
97-
86+
9887 return nil
9988}
10089
@@ -117,44 +106,42 @@ func (cp *YamlConfigProvider) StoreData(publicKey string, privateKey string) err
117106
118107
119108func (cp * YamlConfigProvider ) Get () (Config , error ) {
120- home , err := cp .GetUserHomeDir ()
109+ configPath , err := cp .dirProvider . GetConfigFile ()
121110 if err != nil {
122- return Config {}, fmt .Errorf ("failed to get user home directory : %w" , err )
111+ return Config {}, fmt .Errorf ("failed to get config file path : %w" , err )
123112 }
124113
125- configPath := filepath .Join (home , ".config" , "mbvpn" , "config.yml" )
126-
127114 // Try to open the file
128115 f , err := os .Open (configPath )
129-
116+
130117 // If file doesn't exist, create it with default config
131118 if os .IsNotExist (err ) {
132119 // Ensure directory exists
133120 configDir := filepath .Dir (configPath )
134121 if err := os .MkdirAll (configDir , 0755 ); err != nil {
135122 return Config {}, fmt .Errorf ("failed to create config directory: %w" , err )
136123 }
137-
124+
138125 // Create default config
139126 defaultConfig := Config {
140127 // Set your default values here
141128 // For example:
142129 // ServerURL: "default.server.com",
143130 // Port: 51820,
144131 }
145-
132+
146133 // Create and write to the file
147134 f , err := os .Create (configPath )
148135 if err != nil {
149136 return Config {}, fmt .Errorf ("failed to create config file: %w" , err )
150137 }
151138 defer f .Close ()
152-
139+
153140 encoder := yaml .NewEncoder (f )
154141 if err := encoder .Encode (defaultConfig ); err != nil {
155142 return Config {}, fmt .Errorf ("failed to write default config: %w" , err )
156143 }
157-
144+
158145 return defaultConfig , nil
159146 } else if err != nil {
160147 // Handle other errors
0 commit comments