Skip to content

Commit 8d1eef1

Browse files
committed
Fix bug around obtaining default values
1 parent 0c6e592 commit 8d1eef1

File tree

14 files changed

+62
-92
lines changed

14 files changed

+62
-92
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ chatgpt --list-models
150150
## Configuration
151151

152152
The chatGPT CLI uses a two-level configuration system. The default configuration is read from the
153-
file `resources/config.yaml` located within the package. These default values are:
153+
file `utils/constants.go` located within the package. These default values are:
154154

155155
```yaml
156156
model: gpt-3.5-turbo

client/client.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,12 @@ type Client struct {
3030
historyStore history.HistoryStore
3131
}
3232

33-
func New(caller http.Caller, cs config.ConfigStore, hs history.HistoryStore) (*Client, error) {
34-
cm, err := configmanager.New(cs)
35-
if err != nil {
36-
return nil, err
37-
}
38-
39-
result := &Client{
40-
Config: cm.Config,
33+
func New(caller http.Caller, cs config.ConfigStore, hs history.HistoryStore) *Client {
34+
return &Client{
35+
Config: configmanager.New(cs).Config,
4136
caller: caller,
4237
historyStore: hs,
4338
}
44-
45-
return result, nil
4639
}
4740

4841
func (c *Client) WithCapacity(capacity int) *Client {

client/client_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ func newClientFactory(mc *MockCaller, mcs *MockConfigStore, mhs *MockHistoryStor
466466
URL: defaultURL,
467467
CompletionsPath: defaultCompletionsPath,
468468
ModelsPath: defaultModelsPath,
469-
}, nil).Times(1)
469+
}).Times(1)
470470

471471
return &clientFactory{
472472
mockCaller: mc,
@@ -478,18 +478,16 @@ func newClientFactory(mc *MockCaller, mcs *MockConfigStore, mhs *MockHistoryStor
478478
func (f *clientFactory) buildClientWithoutConfig() *client.Client {
479479
f.mockConfigStore.EXPECT().Read().Return(types.Config{}, nil).Times(1)
480480

481-
c, err := client.New(f.mockCaller, f.mockConfigStore, f.mockHistoryStore)
482-
Expect(err).NotTo(HaveOccurred())
481+
c := client.New(f.mockCaller, f.mockConfigStore, f.mockHistoryStore)
483482

484483
return c.WithCapacity(50)
485484
}
486485

487486
func (f *clientFactory) buildClientWithConfig(config types.Config) *client.Client {
488487
f.mockConfigStore.EXPECT().Read().Return(config, nil).Times(1)
489488

490-
c, err := client.New(f.mockCaller, f.mockConfigStore, f.mockHistoryStore)
491-
Expect(err).NotTo(HaveOccurred())
492-
489+
c := client.New(f.mockCaller, f.mockConfigStore, f.mockHistoryStore)
490+
493491
return c.WithCapacity(50)
494492
}
495493

client/configmocks_test.go

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/chatgpt/main.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ func run(cmd *cobra.Command, args []string) error {
6363
}
6464

6565
if cmd.Flag("set-model").Changed {
66-
cm, err := configmanager.New(config.New())
67-
if err != nil {
68-
return err
69-
}
66+
cm := configmanager.New(config.New())
7067

7168
if err := cm.WriteModel(modelName); err != nil {
7269
return err
@@ -85,15 +82,13 @@ func run(cmd *cobra.Command, args []string) error {
8582
return nil
8683
}
8784

88-
secret := viper.GetString(utils.OpenAPIKeyEnv)
85+
secret := viper.GetString(utils.OpenAIKeyEnv)
8986
if secret == "" {
90-
return errors.New("missing environment variable: " + utils.OpenAPIKeyEnv)
87+
return errors.New("missing environment variable: " + utils.OpenAIKeyEnv)
9188
}
92-
client, err := client.New(http.New().WithSecret(secret), config.New(), history.New())
93-
if err != nil {
94-
return err
95-
}
96-
89+
90+
client := client.New(http.New().WithSecret(secret), config.New(), history.New())
91+
9792
if modelName != "" {
9893
client = client.WithModel(modelName)
9994
}

config/store.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"gopkg.in/yaml.v3"
77
"os"
88
"path/filepath"
9-
"runtime"
109
)
1110

1211
type ConfigStore interface {
1312
Read() (types.Config, error)
14-
ReadDefaults() (types.Config, error)
13+
ReadDefaults() types.Config
1514
Write(types.Config) error
1615
}
1716

@@ -38,12 +37,14 @@ func (f *FileIO) Read() (types.Config, error) {
3837
return parseFile(f.configFilePath)
3938
}
4039

41-
func (f *FileIO) ReadDefaults() (types.Config, error) {
42-
_, thisFile, _, _ := runtime.Caller(0)
43-
44-
configPath := filepath.Join(thisFile, "..", "..", "resources", "config.yaml")
45-
46-
return parseFile(configPath)
40+
func (f *FileIO) ReadDefaults() types.Config {
41+
return types.Config{
42+
Model: utils.OpenAIModel,
43+
MaxTokens: utils.OpenAIModelMaxTokens,
44+
URL: utils.OpenAIURL,
45+
CompletionsPath: utils.OpenAICompletionsPath,
46+
ModelsPath: utils.OpenAIModelsPath,
47+
}
4748
}
4849

4950
func (f *FileIO) Write(config types.Config) error {

configmanager/configmanager.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package configmanager
22

33
import (
4-
"fmt"
54
"github.com/kardolus/chatgpt-cli/config"
65
"github.com/kardolus/chatgpt-cli/types"
76
)
@@ -11,11 +10,8 @@ type ConfigManager struct {
1110
Config types.Config
1211
}
1312

14-
func New(cs config.ConfigStore) (*ConfigManager, error) {
15-
c, err := cs.ReadDefaults()
16-
if err != nil {
17-
return nil, fmt.Errorf("error reading default values: %w", err)
18-
}
13+
func New(cs config.ConfigStore) *ConfigManager {
14+
c := cs.ReadDefaults()
1915

2016
configured, err := cs.Read()
2117
if err == nil {
@@ -36,7 +32,7 @@ func New(cs config.ConfigStore) (*ConfigManager, error) {
3632
}
3733
}
3834

39-
return &ConfigManager{configStore: cs, Config: c}, nil
35+
return &ConfigManager{configStore: cs, Config: c}
4036
}
4137

4238
func (c *ConfigManager) WriteModel(model string) error {

configmanager/configmanager_test.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
5252

5353
when("Constructing a new ConfigManager", func() {
5454
it("applies the default configuration when user config is missing", func() {
55-
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig, nil).Times(1)
55+
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig).Times(1)
5656
mockConfigStore.EXPECT().Read().Return(types.Config{}, errors.New("no such file")).Times(1)
5757

58-
subject, err := configmanager.New(mockConfigStore)
59-
Expect(err).NotTo(HaveOccurred())
58+
subject := configmanager.New(mockConfigStore)
6059

6160
Expect(subject.Config.Model).To(Equal(defaultModel))
6261
Expect(subject.Config.MaxTokens).To(Equal(defaultMaxTokens))
@@ -67,11 +66,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
6766
it("gives precedence to the user provided model", func() {
6867
userModel := "the-model"
6968

70-
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig, nil).Times(1)
69+
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig).Times(1)
7170
mockConfigStore.EXPECT().Read().Return(types.Config{Model: userModel}, nil).Times(1)
7271

73-
subject, err := configmanager.New(mockConfigStore)
74-
Expect(err).NotTo(HaveOccurred())
72+
subject := configmanager.New(mockConfigStore)
7573

7674
Expect(subject.Config.Model).To(Equal(userModel))
7775
Expect(subject.Config.MaxTokens).To(Equal(defaultMaxTokens))
@@ -82,11 +80,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
8280
it("gives precedence to the user provided max-tokens", func() {
8381
userMaxTokens := 42
8482

85-
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig, nil).Times(1)
83+
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig).Times(1)
8684
mockConfigStore.EXPECT().Read().Return(types.Config{MaxTokens: userMaxTokens}, nil).Times(1)
8785

88-
subject, err := configmanager.New(mockConfigStore)
89-
Expect(err).NotTo(HaveOccurred())
86+
subject := configmanager.New(mockConfigStore)
9087

9188
Expect(subject.Config.Model).To(Equal(defaultModel))
9289
Expect(subject.Config.MaxTokens).To(Equal(userMaxTokens))
@@ -97,11 +94,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
9794
it("gives precedence to the user provided URL", func() {
9895
userURL := "the-user-url"
9996

100-
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig, nil).Times(1)
97+
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig).Times(1)
10198
mockConfigStore.EXPECT().Read().Return(types.Config{URL: userURL}, nil).Times(1)
10299

103-
subject, err := configmanager.New(mockConfigStore)
104-
Expect(err).NotTo(HaveOccurred())
100+
subject := configmanager.New(mockConfigStore)
105101

106102
Expect(subject.Config.Model).To(Equal(defaultModel))
107103
Expect(subject.Config.MaxTokens).To(Equal(defaultMaxTokens))
@@ -112,11 +108,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
112108
it("gives precedence to the user provided completions-path", func() {
113109
completionsPath := "the-completions-path"
114110

115-
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig, nil).Times(1)
111+
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig).Times(1)
116112
mockConfigStore.EXPECT().Read().Return(types.Config{CompletionsPath: completionsPath}, nil).Times(1)
117113

118-
subject, err := configmanager.New(mockConfigStore)
119-
Expect(err).NotTo(HaveOccurred())
114+
subject := configmanager.New(mockConfigStore)
120115

121116
Expect(subject.Config.Model).To(Equal(defaultModel))
122117
Expect(subject.Config.MaxTokens).To(Equal(defaultMaxTokens))
@@ -127,11 +122,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
127122
it("gives precedence to the user provided models-path", func() {
128123
modelsPath := "the-models-path"
129124

130-
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig, nil).Times(1)
125+
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig).Times(1)
131126
mockConfigStore.EXPECT().Read().Return(types.Config{ModelsPath: modelsPath}, nil).Times(1)
132127

133-
subject, err := configmanager.New(mockConfigStore)
134-
Expect(err).NotTo(HaveOccurred())
128+
subject := configmanager.New(mockConfigStore)
135129

136130
Expect(subject.Config.Model).To(Equal(defaultModel))
137131
Expect(subject.Config.MaxTokens).To(Equal(defaultMaxTokens))
@@ -143,11 +137,10 @@ func testConfig(t *testing.T, when spec.G, it spec.S) {
143137

144138
when("WriteModel()", func() {
145139
it("writes the expected config file", func() {
146-
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig, nil).Times(1)
140+
mockConfigStore.EXPECT().ReadDefaults().Return(defaultConfig).Times(1)
147141
mockConfigStore.EXPECT().Read().Return(defaultConfig, nil).Times(1)
148142

149-
subject, err := configmanager.New(mockConfigStore)
150-
Expect(err).NotTo(HaveOccurred())
143+
subject := configmanager.New(mockConfigStore)
151144

152145
modelName := "the-model"
153146
subject.Config.Model = modelName

configmanager/configmocks_test.go

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/contract_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@ func testContract(t *testing.T, when spec.G, it spec.S) {
2727
it.Before(func() {
2828
RegisterTestingT(t)
2929

30-
apiKey := os.Getenv(utils.OpenAPIKeyEnv)
30+
apiKey := os.Getenv(utils.OpenAIKeyEnv)
3131
Expect(apiKey).NotTo(BeEmpty())
3232

3333
restCaller = http.New().WithSecret(apiKey)
34-
35-
var err error
36-
defaults, err = config.New().ReadDefaults()
37-
Expect(err).NotTo(HaveOccurred())
34+
defaults = config.New().ReadDefaults()
3835
})
3936

4037
when("accessing the completion endpoint", func() {

0 commit comments

Comments
 (0)