Skip to content

Commit

Permalink
cmd/ask: accept files
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed May 15, 2024
1 parent 4aad051 commit a7ba132
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func (c *client) Runbooks(ctx context.Context) ([]RunbookInfo, error) {
type QuestionInfo struct {
Question string `json:"question"`
Tags map[string]string `json:"tags,omitempty"`
FileData []byte `json:"file_data,omitempty"`
FileName string `json:"file_name,omitempty"`
}

func (c *client) Ask(ctx context.Context, question QuestionInfo) (*Runbook, error) {
Expand Down
43 changes: 43 additions & 0 deletions cmd/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"errors"
"fmt"
"io"
"os"
"path"
"runtime"
"strings"

Expand All @@ -26,9 +28,12 @@ var askCmd = &cobra.Command{
savvy ask "how do I parse a x509 cert"
savvy ask "how do I find the process id listening on a port?"
savvy ask "how do I quit vim?"
savvy ask "extract filenames from the name key in each line of li_ids.txt" --file /path/to/li_ids.txt
`,
Long: `
Ask Savvy a question and it will generate a command for you.
If a file path is provided, Savvy will use the contents of the file to generate a command.
`,
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
Expand All @@ -52,11 +57,19 @@ var askCmd = &cobra.Command{
goos = "macos, darwin, osx"
}

fileData, err := fileData(filePath)
if err != nil {
display.Error(err)
os.Exit(1)
}

qi := client.QuestionInfo{
Question: question,
Tags: map[string]string{
"os": goos,
},
FileData: fileData,
FileName: path.Base(filePath),
}

var runbook *client.Runbook
Expand Down Expand Up @@ -144,6 +157,36 @@ func (dc *askCommands) View() string {
return dc.l.View()
}

func fileData(filePath string) ([]byte, error) {
if filePath == "" {
return nil, nil
}

file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

stat, err := file.Stat()
if err != nil {
return nil, err
}

if stat.Size() > 20*1024 {
return nil, errors.New("file must be less than 20KB")
}

data, err := io.ReadAll(file)
if err != nil {
return nil, err
}
return data, nil
}

var filePath string

func init() {
rootCmd.AddCommand(askCmd)
askCmd.Flags().StringVarP(&filePath, "file", "f", "", "File path for ask to read and use while generating an answer")
}

0 comments on commit a7ba132

Please sign in to comment.