Skip to content

Commit

Permalink
refactor: use consistent error handling and improve variable naming
Browse files Browse the repository at this point in the history
  • Loading branch information
kardolus committed Oct 17, 2024
1 parent 5121c35 commit 1286e9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
30 changes: 15 additions & 15 deletions cmd/chatgpt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ func main() {

var err error
if cfg, err = initConfig(rootCmd); err != nil {
fmt.Fprintf(os.Stderr, "Config initialization failed: %v\n", err)
_, _ = fmt.Fprintf(os.Stderr, "Config initialization failed: %v\n", err)
os.Exit(1)
}

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Expand Down Expand Up @@ -213,10 +213,10 @@ func run(cmd *cobra.Command, args []string) error {
}

hs, _ := history.New() // do not error out
client := client.New(http.RealCallerFactory, hs, cfg, interactiveMode)
c := client.New(http.RealCallerFactory, hs, cfg, interactiveMode)

if ServiceURL != "" {
client = client.WithServiceURL(ServiceURL)
c = c.WithServiceURL(ServiceURL)
}

if hs != nil && newThread {
Expand All @@ -234,7 +234,7 @@ func run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
client.ProvideContext(prompt)
c.ProvideContext(prompt)
}

// Check if there is input from the pipe (stdin)
Expand All @@ -250,11 +250,11 @@ func run(cmd *cobra.Command, args []string) error {
if strings.Trim(context, "\n ") != "" {
hasPipe = true
}
client.ProvideContext(context)
c.ProvideContext(context)
}

if listModels {
models, err := client.ListModels()
models, err := c.ListModels()
if err != nil {
return err
}
Expand All @@ -278,7 +278,7 @@ func run(cmd *cobra.Command, args []string) error {
defer rl.Close()

commandPrompt := func(counter, usage int) string {
return utils.FormatPrompt(client.Config.CommandPrompt, counter, usage, time.Now())
return utils.FormatPrompt(c.Config.CommandPrompt, counter, usage, time.Now())
}

qNum, usage := 1, 0
Expand All @@ -291,10 +291,10 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

fmtOutputPrompt := utils.FormatPrompt(client.Config.OutputPrompt, qNum, usage, time.Now())
fmtOutputPrompt := utils.FormatPrompt(c.Config.OutputPrompt, qNum, usage, time.Now())

if queryMode {
result, qUsage, err := client.Query(input)
result, qUsage, err := c.Query(input)
if err != nil {
fmt.Println("Error:", err)
} else {
Expand All @@ -304,8 +304,8 @@ func run(cmd *cobra.Command, args []string) error {
}
} else {
fmt.Print(fmtOutputPrompt)
if err := client.Stream(input); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
if err := c.Stream(input); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Error:", err)
} else {
fmt.Println()
qNum++
Expand All @@ -317,17 +317,17 @@ func run(cmd *cobra.Command, args []string) error {
return errors.New("you must specify your query or provide input via a pipe")
}
if queryMode {
result, usage, err := client.Query(strings.Join(args, " "))
result, usage, err := c.Query(strings.Join(args, " "))
if err != nil {
return err
}
fmt.Println(result)

if client.Config.TrackTokenUsage {
if c.Config.TrackTokenUsage {
fmt.Printf("\n[Token Usage: %d]\n", usage)
}
} else {
if err := client.Stream(strings.Join(args, " ")); err != nil {
if err := c.Stream(strings.Join(args, " ")); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func testUtils(t *testing.T, when spec.G, it spec.S) {

it("Overwrites the default when OPENAI_CONFIG_HOME is set", func() {
customConfigHome := "/custom/config/path"
os.Setenv("OPENAI_CONFIG_HOME", customConfigHome)
Expect(os.Setenv("OPENAI_CONFIG_HOME", customConfigHome)).To(Succeed())

configHome, err := utils.GetConfigHome()

Expand All @@ -118,7 +118,7 @@ func testUtils(t *testing.T, when spec.G, it spec.S) {

it("Overwrites the default when OPENAI_DATA_HOME is set", func() {
customDataHome := "/custom/data/path"
os.Setenv("OPENAI_DATA_HOME", customDataHome)
Expect(os.Setenv("OPENAI_DATA_HOME", customDataHome)).To(Succeed())

dataHome, err := utils.GetDataHome()

Expand Down

0 comments on commit 1286e9e

Please sign in to comment.