-
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.
feat(oauth): save resources info in config dir
- Loading branch information
1 parent
015d49f
commit 71b8957
Showing
7 changed files
with
191 additions
and
8 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
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,72 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
const ResourcesFile = "resources.json" | ||
|
||
type Resource struct { | ||
CloudId string `json:"id"` | ||
Name string `json:"name"` | ||
BaseURL string `json:"url"` | ||
Scopes []string `json:"scopes"` | ||
} | ||
|
||
func (r Resource) Save() 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 | ||
} | ||
} | ||
|
||
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 | ||
} | ||
|
||
func parseResources(raw []byte) ([]Resource, error) { | ||
result := make([]Resource, 0) | ||
|
||
if err := json.Unmarshal(raw, &result); err != nil { | ||
return []Resource{}, nil | ||
} | ||
|
||
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 | ||
} |
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,64 @@ | ||
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 := `[ | ||
{ | ||
"id": "1324a887-45db-1bf4-1e99-ef0ff456d421", | ||
"name": "Site name", | ||
"url": "https://your-domain.atlassian.net", | ||
"scopes": [ | ||
"write:jira-work", | ||
"read:jira-user", | ||
"manage:jira-configuration" | ||
], | ||
"avatarUrl": "https:\/\/site-admin-avatar-cdn.prod.public.atl-paas.net\/avatars\/240\/flag.png" | ||
} | ||
]` | ||
|
||
got, err := parseResources([]byte(raw)) | ||
|
||
want := []Resource{ | ||
{ | ||
CloudId: "1324a887-45db-1bf4-1e99-ef0ff456d421", | ||
Name: "Site name", | ||
BaseURL: "https://your-domain.atlassian.net", | ||
Scopes: []string{ | ||
"write:jira-work", | ||
"read:jira-user", | ||
"manage:jira-configuration", | ||
}, | ||
}, | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, want, got) | ||
} |
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