Skip to content

Commit 6385fd2

Browse files
committed
fix: config.yamlの挙動を修正
1 parent a833572 commit 6385fd2

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

internal/entity/config.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package entity
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67

78
pb "github.com/cocoide/commitify/pkg/grpc"
89
"github.com/spf13/viper"
@@ -61,8 +62,12 @@ func (c *Config) Config2PbVars() (pb.CodeFormatType, pb.LanguageType) {
6162

6263
func ReadConfig() (Config, error) {
6364
var result Config
65+
homePath, err := os.UserHomeDir()
66+
if err != nil {
67+
return result, err
68+
}
6469

65-
viper.AddConfigPath("$HOME/.commitify")
70+
viper.AddConfigPath(homePath + "/.commitify")
6671
viper.SetConfigName("config")
6772
viper.SetConfigType("yaml")
6873
if err := viper.ReadInConfig(); err != nil {
@@ -75,7 +80,12 @@ func ReadConfig() (Config, error) {
7580
}
7681

7782
func WriteConfig(config Config) error {
78-
viper.AddConfigPath("$HOME/.commitify")
83+
homePath, err := os.UserHomeDir()
84+
if err != nil {
85+
return err
86+
}
87+
88+
viper.AddConfigPath(homePath + "/.commitify")
7989
viper.SetConfigName("config")
8090
viper.SetConfigType("yaml")
8191
configMap := make(map[string]interface{})

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import (
99

1010
func main() {
1111
// configファイルがあるかどうかを確認
12-
homePath := os.Getenv("HOME")
12+
homePath, err := os.UserHomeDir()
13+
if err != nil {
14+
fmt.Printf("error of find user home dir, %v", err)
15+
return
16+
}
1317

14-
_, err := os.Stat(homePath + "/.commitify/config.yaml")
18+
_, err = os.Stat(homePath + "/.commitify/config.yaml")
1519
if os.IsNotExist(err) {
1620
if err := os.MkdirAll(homePath+"/.commitify", 0755); err != nil {
1721
fmt.Printf("error of make directory, %v", err)

0 commit comments

Comments
 (0)