Skip to content

Commit

Permalink
More verbal on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevhen Zavhorodnii committed Feb 7, 2024
1 parent b9fc492 commit 4e92af1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ To run the code with VS Code add launch.json with this content and simply use ``
"type": "go",
"request": "launch",
"mode": "debug",
"console": "integratedTerminal",
"program": "${workspaceFolder}/cmd/threagile",
"args": [
"help",
Expand Down
7 changes: 4 additions & 3 deletions internal/threagile/quit.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package threagile

import (
"os"

"github.com/spf13/cobra"
"github.com/threagile/threagile/pkg/common"
"os"
)

func (what *Threagile) initQuit() *Threagile {
analyze := &cobra.Command{
quit := &cobra.Command{
Use: common.QuitCommand,
Short: "quit client",
Aliases: []string{"exit", "bye", "x", "q"},
Expand All @@ -19,7 +20,7 @@ func (what *Threagile) initQuit() *Threagile {
},
}

what.rootCmd.AddCommand(analyze)
what.rootCmd.AddCommand(quit)

return what
}
23 changes: 14 additions & 9 deletions internal/threagile/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,20 @@ func (what *Threagile) initRoot() *Threagile {
return what
}

func (what *Threagile) run(*cobra.Command, []string) {
func (what *Threagile) run(cmd *cobra.Command, _ []string) {
if !what.flags.interactiveFlag {
cmd.Println("Please add the --interactive flag to run in interactive mode.")
return
}

what.rootCmd.Use = "\b"
completer := readline.NewPrefixCompleter()
for _, child := range what.rootCmd.Commands() {
what.cobraToReadline(completer, child)
}

dir, homeError := os.UserHomeDir()
if homeError != nil {
cmd.Println("Error, please report bug at https://github.com/Threagile/threagile. Unable to find home directory: " + homeError.Error())
return
}

Expand All @@ -114,14 +115,19 @@ func (what *Threagile) run(*cobra.Command, []string) {
})

if readlineError != nil {
cmd.Println("Error, please report bug at https://github.com/Threagile/threagile. Unable to initialize readline: " + readlineError.Error())
return
}

defer func() { _ = shell.Close() }()

for {
line, readError := shell.Readline()
if readError == readline.ErrInterrupt {
return
}
if readError != nil {
cmd.Println("Error, please report bug at https://github.com/Threagile/threagile. Unable to read line: " + readError.Error())
return
}

Expand All @@ -131,24 +137,24 @@ func (what *Threagile) run(*cobra.Command, []string) {

params, parseError := shellwords.Parse(line)
if parseError != nil {
fmt.Printf("failed to parse command line: %s", parseError.Error())
cmd.Printf("failed to parse command line: %s", parseError.Error())
continue
}

cmd, args, findError := what.rootCmd.Find(params)
if findError != nil {
fmt.Printf("failed to find command: %s", findError.Error())
cmd.Printf("failed to find command: %s", findError.Error())
continue
}

if cmd == nil || cmd == what.rootCmd {
fmt.Printf("failed to find command")
cmd.Println("failed to find command")
continue
}

flagsError := cmd.ParseFlags(args)
if flagsError != nil {
fmt.Printf("invalid flags: %s", flagsError.Error())
cmd.Printf("invalid flags: %s", flagsError.Error())
continue
}

Expand All @@ -170,13 +176,12 @@ func (what *Threagile) run(*cobra.Command, []string) {
if cmd.RunE != nil {
runError := cmd.RunE(cmd, args)
if runError != nil {
fmt.Printf("error: %v \n", runError)
cmd.Printf("error: %v \n", runError)
}
continue
}

_ = cmd.Help()
continue
}
}

Expand Down Expand Up @@ -219,7 +224,7 @@ func (what *Threagile) readConfig(cmd *cobra.Command, buildTimestamp string) *co
cfg := new(common.Config).Defaults(buildTimestamp)
configError := cfg.Load(what.flags.configFlag)
if configError != nil {
fmt.Printf("WARNING: failed to load config file %q: %v\n", what.flags.configFlag, configError)
cmd.Printf("WARNING: failed to load config file %q: %v\n", what.flags.configFlag, configError)
}

flags := cmd.Flags()
Expand Down
4 changes: 3 additions & 1 deletion internal/threagile/threagile.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package threagile

import (
"github.com/spf13/cobra"
"os"

"github.com/spf13/cobra"
)

type Threagile struct {
Expand All @@ -14,6 +15,7 @@ type Threagile struct {
func (what *Threagile) Execute() {
err := what.rootCmd.Execute()
if err != nil {
what.rootCmd.Println(err)
os.Exit(1)
}
}
Expand Down

0 comments on commit 4e92af1

Please sign in to comment.