-
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.
fix: main 関数に設定情報を記載していたのを設定ファイルから読み取るように修正
- Loading branch information
1 parent
8b6bf4f
commit 3d369ea
Showing
3 changed files
with
50 additions
and
5 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
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 | ||
} |