Skip to content

Commit

Permalink
Merge branch 'feat/account_setting_file'
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoichi206 committed Jan 30, 2023
2 parents 8b6bf4f + 3d369ea commit 824aff9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type ListMembersResponse struct {
func (a *Api) ListMembers(gn string) ([]Member, error) {

var (
URL = fmt.Sprintf("%s/members?gn=%s&key=%s", a.config.BaseURL, gn, a.config.APIKey)
URL = fmt.Sprintf("%s/members?gn=%s&key=%s", a.config.Account.BaseURL, gn, a.config.Account.APIKey)
)

resp, err := a.httpCall(URL)
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"

"github.com/android-project-46group/sgi-cli/api"
"github.com/android-project-46group/sgi-cli/cmd"
Expand All @@ -15,10 +16,10 @@ var (
)

func main() {
config := util.Config{
Version: fmt.Sprintf("%s - %s", version, revision),
BaseURL: "https://kokoichi0206.mydns.jp/api/v1",
APIKey: "my_api_key",
ver := fmt.Sprintf("%s - %s", version, revision)
config, err := util.NewConfig(ver)
if err != nil {
log.Fatal(err)
}
api := api.New(config)

Expand Down
44 changes: 44 additions & 0 deletions util/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
package util

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)

type Config struct {
Version string
Account Account
}

type Account struct {
BaseURL string
APIKey string
}

var (
// Path to an account setting file from HOME directory
accountPath = ".sgi/account.json"
)

func NewConfig(version string) (Config, error) {

var cfg Config
cfg.Version = version

// Read account.json from a specific file.
home, err := os.UserHomeDir()
if err != nil {
return cfg, err
}
joined := filepath.Join(home, accountPath)

f, err := os.Open(joined)
if err != nil {
return cfg, errors.New(fmt.Sprintf("Expected to read account information from: %s\n", joined))
}
defer f.Close()

var ac Account
if err := json.NewDecoder(f).Decode(&ac); err != nil {
return cfg, err
}
cfg.Account = ac

return cfg, nil
}

0 comments on commit 824aff9

Please sign in to comment.