@@ -16,6 +16,16 @@ var configKeys = []string{
1616 "team" ,
1717}
1818
19+ // SetConfigHome sets the configuration home directory - useful for testing
20+ func SetConfigHome (configDir string ) error {
21+ return setXDGEnv ("XDG_CONFIG_HOME" , configDir )
22+ }
23+
24+ // UnsetConfigHome unsets the configuration home directory returning to the default value
25+ func UnsetConfigHome () error {
26+ return unsetXDGEnv ("XDG_CONFIG_HOME" )
27+ }
28+
1929// GetValue reads the value of a config key from the config file
2030func GetValue (key string ) (string , error ) {
2131 if ! slices .Contains (configKeys , key ) {
@@ -88,3 +98,77 @@ func setValue(key string, val *string) error {
8898 }
8999 return nil
90100}
101+
102+ // SetDataHome sets the data home directory - useful for testing
103+ func SetDataHome (dataHome string ) error {
104+ return setXDGEnv ("XDG_DATA_HOME" , dataHome )
105+ }
106+
107+ // UnsetDataHome unsets the data home directory returning to the default value
108+ func UnsetDataHome () error {
109+ return unsetXDGEnv ("XDG_DATA_HOME" )
110+ }
111+
112+ // SaveDataString saves a string to a file in the data home directory
113+ func SaveDataString (relPath string , data string ) error {
114+ filePath , err := xdg .DataFile (relPath )
115+ if err != nil {
116+ return fmt .Errorf ("failed to get file path: %w" , err )
117+ }
118+ file , err := os .OpenFile (filePath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0644 )
119+ if err != nil {
120+ return fmt .Errorf ("failed to open file %q for writing: %w" , filePath , err )
121+ }
122+ defer func () {
123+ if closeErr := file .Close (); closeErr != nil {
124+ fmt .Printf ("error closing file: %v" , closeErr )
125+ }
126+ }()
127+ if _ , err = file .WriteString (data ); err != nil {
128+ return fmt .Errorf ("failed to write data to %q: %w" , filePath , err )
129+ }
130+ return nil
131+ }
132+
133+ // ReadDataString reads a string from a file in the data home directory
134+ func ReadDataString (relPath string ) (string , error ) {
135+ filePath , err := xdg .DataFile (relPath )
136+ if err != nil {
137+ return "" , fmt .Errorf ("failed to get file path: %w" , err )
138+ }
139+ b , err := os .ReadFile (filePath )
140+ if err != nil {
141+ return "" , fmt .Errorf ("failed to read file: %w" , err )
142+ }
143+ return strings .TrimSpace (string (b )), nil
144+ }
145+
146+ // DeleteDataString deletes a file in the data home directory
147+ func DeleteDataString (relPath string ) error {
148+ filePath , err := xdg .DataFile (relPath )
149+ if err != nil {
150+ return fmt .Errorf ("failed to get file path: %w" , err )
151+ }
152+ if err := os .RemoveAll (filePath ); err != nil {
153+ return fmt .Errorf ("failed to remove file %q: %w" , filePath , err )
154+ }
155+ return nil
156+ }
157+
158+ func setXDGEnv (key , value string ) error {
159+ err := os .Setenv (key , value )
160+ if err != nil {
161+ return fmt .Errorf ("failed to set %s: %w" , key , err )
162+ }
163+ xdg .Reload ()
164+ return nil
165+ }
166+
167+ func unsetXDGEnv (key string ) error {
168+ err := os .Unsetenv (key )
169+ if err != nil {
170+ return fmt .Errorf ("failed to unset %s: %w" , key , err )
171+ }
172+ xdg .Reload ()
173+ return nil
174+ }
0 commit comments