Skip to content

Commit

Permalink
refactor: extract saving config to separate config service
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Oct 22, 2023
1 parent c4e78d9 commit 5643424
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 98 deletions.
54 changes: 2 additions & 52 deletions pkg/jira_changelog/jira/auth/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package auth

import (
"encoding/json"
"fmt"
"os"
"path"

"github.com/mitchellh/go-homedir"
"github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/jira/config"
)

const ResourcesFile = "resources.json"
Expand All @@ -19,26 +16,7 @@ type Resource struct {
}

func (r Resource) Save() error {

confdir, err := getOrCreateConfDir()
if err != nil {
return fmt.Errorf("failed to get config dir %w", err)
}

filepath := path.Join(confdir, ResourcesFile)
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(r)
if err != nil {
return fmt.Errorf("failed to encode resources to json. %w", err)
}

return nil
return config.Save(r, ResourcesFile)
}

func parseResources(raw []byte) ([]Resource, error) {
Expand All @@ -50,31 +28,3 @@ func parseResources(raw []byte) ([]Resource, error) {

return result, 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
}
23 changes: 0 additions & 23 deletions pkg/jira_changelog/jira/auth/resource_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
package auth

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)
}

func TestParseResources(t *testing.T) {
raw := `[
{
Expand Down
26 changes: 3 additions & 23 deletions pkg/jira_changelog/jira/auth/token.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package auth

import (
"encoding/json"
"fmt"
"os"
"path"
"time"

"github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/jira/config"
)

type Token struct {
Expand All @@ -17,23 +15,5 @@ type Token struct {
const TokenFile = "token.json"

func (t *Token) Save() error {
confdir, err := getOrCreateConfDir()
if err != nil {
return fmt.Errorf("failed to get config dir for saving token. %w", err)
}

filepath := path.Join(confdir, TokenFile)
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(t)
if err != nil {
return fmt.Errorf("failed to encode resources to json. %w", err)
}

return nil
return config.Save(t, TokenFile)
}
60 changes: 60 additions & 0 deletions pkg/jira_changelog/jira/config/config_service.go
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
}
30 changes: 30 additions & 0 deletions pkg/jira_changelog/jira/config/config_service_test.go
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)
}

0 comments on commit 5643424

Please sign in to comment.