diff --git a/api/members.go b/api/members.go index 1b6ebf7..652971e 100644 --- a/api/members.go +++ b/api/members.go @@ -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) diff --git a/main.go b/main.go index 9a13606..c867d4e 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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) diff --git a/util/config.go b/util/config.go index 5e7f787..5f9439e 100644 --- a/util/config.go +++ b/util/config.go @@ -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 +}