Skip to content

Commit

Permalink
-s only with stdin or arg
Browse files Browse the repository at this point in the history
  • Loading branch information
jdevoo committed Sep 2, 2024
1 parent bc4e0ad commit 2ac5718
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
27 changes: 11 additions & 16 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,21 @@ func emitGen(in io.Reader, out io.Writer) int {
// Handle invalid argument and option combinations
if (*topPVal < 0 || *topPVal > 1) || // temp out of range
// no prompt as stdin, argument or file
(!stdinFlag && len(flag.Args()) == 0 && !anyMatches(filePaths, pExt, siExt)) ||
(!stdinFlag && len(flag.Args()) == 0 && !oneMatches(filePaths, pExt) && !oneMatches(filePaths, siExt)) ||
// lack of /dev/tty on Windows prevents this flag combination
(runtime.GOOS == "windows" && stdinFlag && *chatModeFlag) ||
// stdin set but neither used as file nor as argument
(stdinFlag && !(len(flag.Args()) == 1 && flag.Args()[0] == "-") && !oneMatches(filePaths, "-")) ||
(!*chatModeFlag && *systemInstructionFlag &&
// no chat mode, stdin as system instruction, no prompt argument
((stdinFlag && oneMatches(filePaths, "-") && len(flag.Args()) == 0) ||
// no chat mode, no file as prompt, argument as system instruction
// no chat mode, one of file or argument as system instruction - look for a prompt
(*systemInstructionFlag &&
// no stdin, no argument
((!stdinFlag && len(flag.Args()) == 0) ||
// no stdin, argument as system instruction, no prompt as file
(!stdinFlag && len(flag.Args()) > 0 && !anyMatches(filePaths, pExt)) ||
// no chat mode, file as system instruction, no prompt
(!stdinFlag && len(flag.Args()) == 0 && allMatch(filePaths, siExt)))) ||
(*chatModeFlag && *systemInstructionFlag && len(filePaths) > 0 &&
!anyMatches(filePaths, pExt, siExt) &&
// chat mode, no file as prompt, no stdin, argument as system instruction
((!stdinFlag && len(flag.Args()) > 0) ||
// chat mode, no file as prompt, stdin as system instruction file, no prompt argument
(stdinFlag && len(flag.Args()) == 0 && oneMatches(filePaths, "-")) ||
// chat mode, no file as prompt, no file as system instruction, argument as system instruction
(stdinFlag && !oneMatches(filePaths, "-") && len(flag.Args()) == 1 && flag.Args()[0] == "-"))) {
// stdin as file, no prompt as file or argument
(stdinFlag && oneMatches(filePaths, "-") && len(flag.Args()) == 0 && !oneMatches(filePaths, pExt)) ||
// stdin as argument, no prompt as file
(stdinFlag && len(flag.Args()) == 1 && flag.Args()[0] == "-" && !oneMatches(filePaths, pExt)))) {
emitUsage(out)
return 1
}
Expand Down Expand Up @@ -230,7 +225,7 @@ func emitGen(in io.Reader, out io.Writer) int {
if stdinFlag && text == "-" {
text = string(stdinData)
}
if *chatModeFlag && *systemInstructionFlag {
if *systemInstructionFlag && !(stdinFlag && oneMatches(filePaths, "-")) {
instructions = append(instructions, genai.Text(text))
} else {
prompts = append(prompts, genai.Text(text))
Expand Down
14 changes: 7 additions & 7 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ func TestFlags(t *testing.T) {
{
"gen",
"",
[]string{"-tool", "-m", "gemini-1.5-pro", "list known models"},
[]string{"-tool", "list known models"},
0,
[]string{"gemini-1.5-flash"},
},
{
"gen",
"",
[]string{"-tool", "-m", "gemini-1.5-pro", "-p", "DSN=postgres://steampipe:[email protected]:9193/steampipe", "retrieve AWS account IDs"},
[]string{"-tool", "-p", "DSN=postgres://steampipe:[email protected]:9193/steampipe", "retrieve AWS account IDs"},
0,
[]string{"["},
[]string{"connection refused"},
},
}

Expand All @@ -98,14 +98,14 @@ func TestFlags(t *testing.T) {
var out bytes.Buffer
actualExit := emitGen(strings.NewReader(test.stdin), &out)
if test.wantExit != actualExit {
t.Errorf("Wrong exit code for args: %v %v, expected: %v, got: %v",
test.stdin, test.args, test.wantExit, actualExit)
t.Errorf("Wrong exit code for args: %v, expected: %v, got: %v",
test.args, test.wantExit, actualExit)
continue
}
actualOutput := out.String()
if !anyMatches([]string{actualOutput}, test.wantOutput...) {
t.Errorf("Wrong output for args: %v %v, expected: %v, got: %v",
test.stdin, test.args, strings.Join(test.wantOutput, ","), actualOutput)
t.Errorf("Wrong output for args: %v, expected one of: %v, got: %v",
test.args, strings.Join(test.wantOutput, ","), actualOutput)
}
}
}
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func invokeTool(fc genai.FunctionCall) string {
}
vals := f.Call(args)
if err := vals[1].Interface(); err != nil {
log.Fatal(err)
return fmt.Sprint(err)
}
return vals[0].String()
}
Expand Down

0 comments on commit 2ac5718

Please sign in to comment.