-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract saving config to separate config service
- Loading branch information
1 parent
c4e78d9
commit 5643424
Showing
5 changed files
with
95 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
func Save(content any, filepath string) error { | ||
confdir, err := getOrCreateConfDir() | ||
if err != nil { | ||
return fmt.Errorf("failed to get config dir for saving token. %w", err) | ||
} | ||
|
||
filepath = path.Join(confdir, filepath) | ||
f, err := os.OpenFile(filepath, os.O_CREATE|os.O_WRONLY, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
enc := json.NewEncoder(f) | ||
err = enc.Encode(content) | ||
if err != nil { | ||
return fmt.Errorf("failed to encode resources to json. %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func defaultConfDir() (res string, err error) { | ||
filepath := path.Join("gh-jira-changelog") | ||
res = os.Getenv("XDG_CONFIG_HOME") | ||
if res == "" { | ||
res, err = homedir.Dir() | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
return path.Join(res, filepath), nil | ||
} | ||
|
||
func getOrCreateConfDir() (string, error) { | ||
confdir, err := defaultConfDir() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to generate config file %w", err) | ||
} | ||
|
||
if _, err := os.Stat(confdir); os.IsNotExist(err) { | ||
err = os.Mkdir(confdir, os.ModeDir|0755) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return confdir, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDefaultDir_When_XDG_CONFIG_HOME_isSet(t *testing.T) { | ||
os.Setenv("XDG_CONFIG_HOME", "~/foobar") | ||
got, err := defaultConfDir() | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "~/foobar/gh-jira-changelog", got) | ||
} | ||
|
||
func TestDefaultDir_When_XDG_CONFIG_HOME_isNotSet(t *testing.T) { | ||
os.Unsetenv("XDG_CONFIG_HOME") | ||
|
||
homeDir, _ := homedir.Dir() | ||
expected := path.Join(homeDir, "gh-jira-changelog") | ||
|
||
got, err := defaultConfDir() | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expected, got) | ||
} |