Skip to content

gRPCのサーバを接続 / 設定情報をenumで定義 #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 10, 2023
Merged
Prev Previous commit
Next Next commit
feat: 設定情報に関するメソッドをentity層に移植
  • Loading branch information
mochi-yu committed Sep 10, 2023
commit 7ce15a06da3007f1b4621184b8f66f1c65bdaf4d
6 changes: 3 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/cocoide/commitify/util"
"github.com/cocoide/commitify/internal/entity"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -182,7 +182,7 @@ func init() {
}

func saveConfig(cm configModel) {
currentConfig, err := util.ReadConfig()
currentConfig, err := entity.ReadConfig()
if err != nil {
fmt.Println(err)
}
Expand All @@ -196,7 +196,7 @@ func saveConfig(cm configModel) {
currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex]
}

err = util.WriteConfig(currentConfig)
err = entity.WriteConfig(currentConfig)
if err != nil {
fmt.Println(err)
}
Expand Down
44 changes: 44 additions & 0 deletions internal/entity/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
package entity

import (
"encoding/json"
"fmt"

"github.com/spf13/viper"
)

type Config struct {
ChatGptApiKey string `json:"chatGptApiKey"`
UseLanguage string `json:"UseLanguage"`
CommitFormat string `json:"CommitFormat"`
}

func ReadConfig() (*Config, error) {
var result Config

viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
return &result, fmt.Errorf("error reading config file, %s", err.Error())
}
if err := viper.Unmarshal(&result); err != nil {
return &result, fmt.Errorf("unable to decode into struct, %v", err.Error())
}
return &result, nil
}

func WriteConfig(config *Config) error {
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
configMap := make(map[string]interface{})
configBytes, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("error marshalling config: %s", err.Error())
}
err = json.Unmarshal(configBytes, &configMap)
if err != nil {
return fmt.Errorf("error unmarshalling config: %s", err.Error())
}
if err := viper.MergeConfigMap(configMap); err != nil {
return err
}
if err := viper.WriteConfig(); err != nil {
return fmt.Errorf("error saving config file, %s", err.Error())
}
return nil
}
2 changes: 1 addition & 1 deletion internal/gateway/grpc_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewGrpcServeGateway() *grpcServeGateway {

func (gsg grpcServeGateway) FetchCommitMessages() ([]string, error) {
// 設定情報からOpenAIへのアクセス方法の変更
// if conf, err := util.ReadConfig(); err != nil {
// if conf, err := entity.ReadConfig(); err != nil {
// fmt.Printf("設定ファイルが開けません: %v", err)
// }

Expand Down
4 changes: 2 additions & 2 deletions internal/gateway/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"log"

"github.com/cocoide/commitify/util"
"github.com/cocoide/commitify/internal/entity"
"github.com/sashabaranov/go-openai"
)

Expand All @@ -20,7 +20,7 @@ type openAIGateway struct {
}

func NewOpenAIGateway(ctx context.Context) OpenAIGateway {
config, err := util.ReadConfig()
config, err := entity.ReadConfig()
if err != nil {
log.Fatalf("Failed to read config: %v", err)
}
Expand Down
46 changes: 0 additions & 46 deletions util/config.go

This file was deleted.