Skip to content

Commit

Permalink
Add flag support, update README docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bcspragu committed Apr 29, 2024
1 parent dfab1ff commit 5354ec5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/kagi
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@



`kagi` is a quick-and-dirty CLI for querying the [Kagi search engine](https://kagi.com/) with their [FastGPT API](https://help.kagi.com/kagi/api/fastgpt.html)
`kagi` is a simple CLI for querying the [Kagi search engine](https://kagi.com/) with their [FastGPT API](https://help.kagi.com/kagi/api/fastgpt.html)

## Installation

Expand All @@ -21,9 +21,20 @@ Then, create an API key + add API credits, following [the official Kagi instruct
export KAGI_API_KEY=...
```

And you should be good to go! Try running `kagi <query>` to test it out.
If you don't want to expose sensitive credentials to all applications running in your shell, you can wrap the `kagi` CLI in a shell script, e.g.:

```bash
#!/bin/bash

# Maybe this lives in ~/.local/bin, which has a higher precedence than your $GOPATH/bin dir

KAGI_API_KEY=$(some CLI password manager or just hardcode it) $GOPATH/bin/kagi "$@"

As an aside, I'm **really** not a fan of storing sensitive credentials in accessible-to-everything-all-the-time environment variables, and if anyone has a good + ergonomic alternative (e.g. involving `pass` or credential helpers), I'm all ears.
# or, if you prefer flags:
# $GOPATH/bin/kagi --kagi_api_key=... "$@"
```

And you should be good to go! Try running `kagi <query>` to test it out.

## Example Output

Expand Down
33 changes: 21 additions & 12 deletions cmd/kagi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
Expand All @@ -16,22 +17,30 @@ func main() {
}
}

var errUsage = errors.New("usage: kagi <query>")

func run(args []string) error {
var query string
switch len(args) {
case 0, 1:
return errors.New("usage: kagi <query>")
case 2:
query = args[1]
default:
query = strings.Join(args[1:], " ")
if len(args) == 0 {
return errUsage
}
fs := flag.NewFlagSet(args[0], flag.ContinueOnError)
var (
kagiAPIKey = fs.String("kagi_api_key", os.Getenv("KAGI_API_KEY"), "API key to use with the Kagi FastGPT API")
)
if err := fs.Parse(args[1:]); err != nil {
return fmt.Errorf("failed to parse flags: %w", err)
}
apiKey := os.Getenv("KAGI_API_KEY")
if apiKey == "" {
return errors.New("no KAGI_API_KEY was set")
if *kagiAPIKey == "" {
return errors.New("no KAGI_API_KEY env var or --kagi_api_key flag was set")
}

fArgs := fs.Args()
if len(fArgs) == 0 {
return errUsage
}
query := strings.Join(fArgs, " ")

client := api.NewClient(apiKey)
client := api.NewClient(*kagiAPIKey)

resp, err := client.QueryFastGPT(query)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/bcspragu/kagi

go 1.20
go 1.22

0 comments on commit 5354ec5

Please sign in to comment.