Skip to content

Commit

Permalink
Abstracts actions into its own file plus adds a lot of testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jan 10, 2025
1 parent 4de2c86 commit d3a2813
Show file tree
Hide file tree
Showing 5 changed files with 990 additions and 55 deletions.
72 changes: 61 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"log"
"log/slog"
"os"

"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/types"
"github.com/urfave/cli/v2"
Expand All @@ -16,25 +17,20 @@ type KhedraApp struct {
}

func NewKhedraApp() *KhedraApp {
os.Args = cleanArgs(os.Args)
k := &KhedraApp{}
k.Cli = initializeCli(k)

cfg, err := LoadConfig()
if err != nil {
log.Fatalf("failed to load config: %v", err)
}

fileLogger, progLogger := types.NewLoggers(cfg.Logging)
cli := initializeCli()

k := &KhedraApp{
config: &cfg,
fileLogger: fileLogger,
progLogger: progLogger,
Cli: cli,
}
k.config = &cfg
k.fileLogger, k.progLogger = types.NewLoggers(cfg.Logging)

return k
}

// Run runs the Khedra cli
func (k *KhedraApp) Run(args []string) error {
return k.Cli.Run(args)
}
Expand Down Expand Up @@ -156,3 +152,57 @@ func (a *App) Fatal(err error) {
os.Exit(1)
}
*/

func parseArgsInternal(args []string) (hasHelp bool, hasVersion bool, commands []string, nonFlagCount int) {
commands = []string{}
if len(args) == 0 {
hasHelp = true
return
}

helpForms := map[string]bool{
"--help": true, "-help": true, "help": true,
"--h": true, "-h": true,
}

versionForms := map[string]bool{
"--version": true, "-version": true, "version": true,
"--v": true, "-v": true,
}

for i, arg := range args {
if helpForms[arg] {
hasHelp = true
continue
}
if versionForms[arg] {
hasVersion = true
continue
}
commands = append(commands, arg)
if i != 0 && len(arg) == 0 || arg[0] != '-' {
nonFlagCount++
}
}

return
}

func cleanArgs(args []string) []string {
programName := args[:1] // program name

hasHelp, hasVersion, commands, _ := parseArgsInternal(args[1:])
if hasHelp {
result := append(programName, "help")
if len(commands) > 0 {
return append(result, commands[0])
}
return result
}

if hasVersion {
return append(programName, "version")
}

return append(programName, commands...)
}
143 changes: 143 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package app

import (
"reflect"
"testing"
)

func TestParseArgsInternal(t *testing.T) {
tests := []struct {
name string
args []string
expHelp bool
expVersion bool
expCmds []string
}{
{
name: "No args",
args: []string{},
expHelp: true,
expVersion: false,
expCmds: []string{},
},
{
name: "Only help",
args: []string{"--help"},
expHelp: true,
expVersion: false,
expCmds: []string{},
},
{
name: "Only version",
args: []string{"--version"},
expHelp: false,
expVersion: true,
expCmds: []string{},
},
{
name: "Help and version",
args: []string{"--help", "--version"},
expHelp: true,
expVersion: true,
expCmds: []string{},
},
{
name: "Commands only",
args: []string{"init", "config", "edit"},
expHelp: false,
expVersion: false,
expCmds: []string{"init", "config", "edit"},
},
{
name: "Commands with help",
args: []string{"init", "config", "--help"},
expHelp: true,
expVersion: false,
expCmds: []string{"init", "config"},
},
{
name: "Commands with version",
args: []string{"init", "config", "--version"},
expHelp: false,
expVersion: true,
expCmds: []string{"init", "config"},
},
{
name: "Commands with help and version",
args: []string{"init", "--help", "config", "--version"},
expHelp: true,
expVersion: true,
expCmds: []string{"init", "config"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hasHelp, hasVersion, commands, _ := parseArgsInternal(tt.args)
if hasHelp != tt.expHelp {
t.Errorf("expected hasHelp=%v, got %v", tt.expHelp, hasHelp)
}
if hasVersion != tt.expVersion {
t.Errorf("expected hasVersion=%v, got %v", tt.expVersion, hasVersion)
}
if !reflect.DeepEqual(commands, tt.expCmds) {
t.Errorf("expected commands=%v, got %v", tt.expCmds, commands)
}
})
}
}

func TestCleanArgs(t *testing.T) {
tests := []struct {
name string
args []string
expectErr bool
expected []string
}{
{
name: "No args",
args: []string{"./program"},
expectErr: false,
expected: []string{"./program", "help"},
},
{
name: "Help flag",
args: []string{"./program", "--help"},
expectErr: false,
expected: []string{"./program", "help"},
},
{
name: "Version flag",
args: []string{"./program", "--version"},
expectErr: false,
expected: []string{"./program", "version"},
},
{
name: "Help and command",
args: []string{"./program", "--help", "init"},
expectErr: false,
expected: []string{"./program", "help", "init"},
},
{
name: "Commands only",
args: []string{"./program", "init", "config"},
expectErr: true,
expected: []string{"./program", "init", "config"},
},
{
name: "Help and version",
args: []string{"./program", "--help", "--version"},
expectErr: false,
expected: []string{"./program", "help"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := cleanArgs(tt.args)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
Loading

0 comments on commit d3a2813

Please sign in to comment.