Skip to content

Commit

Permalink
cmd/explain: start interactive mode if no args provided
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed May 21, 2024
1 parent 9a6e0ed commit a3653b0
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions cmd/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"os"
"path"
"runtime"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/getsavvyinc/savvy-cli/client"
"github.com/getsavvyinc/savvy-cli/cmd/component/viewport"
"github.com/getsavvyinc/savvy-cli/display"
Expand All @@ -17,8 +17,8 @@ import (
var explainCmd = &cobra.Command{
Use: "explain",
Short: "Explain explains shell commands and functions",
Args: cobra.MinimumNArgs(1),
Example: `
savvy explain # interactive mode
savvy explain 'openssl x509 -text -in ./ca.crt -noout | grep --color=auto -C 2 "Valid"'
savvy explain cat "file.txt | sort | uniq -c | sort -nr | head -n 10"
`,
Expand All @@ -40,28 +40,33 @@ var explainCmd = &cobra.Command{
cl = client.NewGuest()
}

var code string
if len(args) == 0 {
// interactive mode
text := huh.NewText().Title("Enter the shell command savvy should explain").Value(&code)
form := huh.NewForm(huh.NewGroup(text))
if err := form.Run(); err != nil {
display.ErrorWithSupportCTA(err)
os.Exit(1)
}
}

// be defensive: users can pass questions as one string or multiple strings
code := strings.Join(args[:], " ")
if len(args) > 0 && len(code) == 0 {
code = strings.Join(args[:], " ")
}

// get info about the os from os pkg: mac/darwin, linux, windows
goos := runtime.GOOS
if goos == "darwin" {
goos = "macos, darwin, osx"
}

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

ci := client.CodeInfo{
Code: code,
Tags: map[string]string{
"os": goos,
},
FileData: fileData,
FileName: path.Base(filePath),
}

explainCh, err := cl.Explain(ctx, ci)
Expand Down

0 comments on commit a3653b0

Please sign in to comment.