Skip to content

Commit

Permalink
feat: --version flag (based on git tag)
Browse files Browse the repository at this point in the history
  • Loading branch information
laszukdawid committed Oct 17, 2024
1 parent 69596d6 commit d101dd5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
30 changes: 29 additions & 1 deletion cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"runtime/debug"

"github.com/laszukdawid/terminal-agent/internal/commands"
"github.com/laszukdawid/terminal-agent/internal/config"
Expand All @@ -12,7 +13,9 @@ import (
"go.uber.org/zap"
)

var loglevel string
var (
loglevel string
)

type exitCode int

Expand All @@ -21,6 +24,22 @@ const (
exitNotOk
)

// printVersion prints the version of the CLI
// The version is based on the golang module version which is based on git tag
func printVersion() {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("Unable to determine version information.")
return
}

if buildInfo.Main.Version != "" {
fmt.Printf("Version: %s\n", buildInfo.Main.Version)
} else {
fmt.Println("Version: unknown")
}
}

// NewCommand creates a new cobra command
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -39,6 +58,13 @@ func NewCommand() *cobra.Command {
},
Run: func(cmd *cobra.Command, args []string) {
u.Logger.Debug("Running command", zap.Strings("args", args))

// Check if version flag is set
if versionFlag, _ := cmd.Flags().GetBool("version"); versionFlag {
printVersion()
return
}

if len(args) == 0 {
cmd.Help()
// return exitNotOk
Expand All @@ -49,6 +75,7 @@ func NewCommand() *cobra.Command {
},
}
cmd.PersistentFlags().StringVar(&loglevel, "loglevel", "info", "set the log level (debug, info, warn, error, dpanic, panic, fatal)")
cmd.Flags().BoolP("version", "v", false, "Print the version of the CLI")

return cmd
}
Expand All @@ -60,6 +87,7 @@ func main() {

func mainRun() exitCode {

// Load configuration for each execution
c, err := config.LoadConfig()
if err != nil {
c = config.NewDefaultConfig()
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/ask_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewQuestionCommand(config config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "ask",
SilenceUsage: true,
Args: cobra.MinimumNArgs(1),
Short: "Sends a question to the underlying LLM model",
Long: `Sends a question to the underlying LLM model
Expand Down Expand Up @@ -83,7 +84,6 @@ func NewQuestionCommand(config config.Config) *cobra.Command {

return nil
},
Args: cobra.MinimumNArgs(1),
}

cmd.MarkFlagRequired("question")
Expand Down
14 changes: 10 additions & 4 deletions internal/commands/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/spf13/cobra"
)

const (
cmdProvider = "provider"
cmdModel = "model"
)

func ConfigCommand(config config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Expand Down Expand Up @@ -36,12 +41,13 @@ func ConfigGetCommand(config config.Config) *cobra.Command {

key := args[0]
switch key {
case "provider":
case cmdProvider:
fmt.Println(config.GetDefaultProvider())
case "model":
case cmdModel:
fmt.Println(config.GetDefaultModelId())
default:
fmt.Println("Unknown key:", key)
cmd.Help()
}
},
}
Expand All @@ -68,10 +74,10 @@ For example: terminal-agent config set provider bedrock`,
value := args[1]

switch key {
case "provider":
case cmdProvider:
config.SetDefaultProvider(value)
fmt.Println("Default provider set to:", value)
case "model":
case cmdModel:
config.SetDefaultModelId(value)
fmt.Println("Default model ID set to:", value)
default:
Expand Down

0 comments on commit d101dd5

Please sign in to comment.