From 3d369eac1b804266e61404121393f533d0d50cbf Mon Sep 17 00:00:00 2001 From: "takahiro.tominaga" Date: Mon, 30 Jan 2023 23:47:29 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20main=20=E9=96=A2=E6=95=B0=E3=81=AB?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E6=83=85=E5=A0=B1=E3=82=92=E8=A8=98=E8=BC=89?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=84=E3=81=9F=E3=81=AE=E3=82=92=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=8B=E3=82=89?= =?UTF-8?q?=E8=AA=AD=E3=81=BF=E5=8F=96=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/members.go | 2 +- main.go | 9 +++++---- util/config.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) 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 +}