diff --git a/config/command.go b/config/command.go index 3e94758..9656639 100644 --- a/config/command.go +++ b/config/command.go @@ -2,24 +2,30 @@ package config import ( "log" + "optimus/utils" + "os" "os/exec" + "strings" "github.com/spf13/cobra" ) type Cmd struct { Run string + Path string Name string Description string File string Shell string } -func ParseCmd(name string, a any) Cmd { +func ParseCmd(name string, root string, a any) Cmd { command := Cmd{ - Run: "", - Name: name, + Run: "", + Name: name, + Path: root, + // RootPath string, Description: "", File: "", Shell: "bash -c", @@ -66,9 +72,14 @@ func (c *Cmd) ToCobraCommand() cobra.Command { Use: c.Name, Short: c.Description, Run: func(cmd *cobra.Command, args []string) { + cmdPath := c.Path + cmdPath = strings.Replace(cmdPath, "./", "/", 1) + e := exec.Command("bash", "-c", c.Run) + e.Dir = utils.ProjectRoot() + cmdPath e.Stdout = os.Stdout e.Stderr = os.Stderr + e.Stdin = os.Stdin e.Run() }, diff --git a/config/config.go b/config/config.go index 655577f..8ad91b3 100644 --- a/config/config.go +++ b/config/config.go @@ -71,7 +71,7 @@ func LoadConfigFromPath(p string) Config { g := ParseGlobal(v2) conf.Global = &g } else if k == "e2e_tests" { - c := ParseCmd(k, v2) + c := ParseCmd(k, "", v2) conf.E2eTests = &c } else if k == "services" { servicesAny, o := v2.(map[string]any) @@ -83,7 +83,7 @@ func LoadConfigFromPath(p string) Config { conf.Services[svcName] = &s } } else { - cmd := ParseCmd(k, v2) + cmd := ParseCmd(k, "", v2) conf.AdditionalCommands[k] = &cmd } diff --git a/config/service.go b/config/service.go index 974190d..26a546b 100644 --- a/config/service.go +++ b/config/service.go @@ -1,9 +1,6 @@ package config import ( - "os" - "os/exec" - "github.com/spf13/cobra" ) @@ -27,6 +24,7 @@ func ParseService(name string, a any) Service { // fmt.Println("Parsing service") s := Service{ Name: name, + Root: name, } amap, o := a.(map[string]any) @@ -34,20 +32,21 @@ func ParseService(name string, a any) Service { panic("Invalid service format") } + r, o := amap["root"] + if o { + s.Root = r.(string) + } + for k, v := range amap { if k == "dev" { - c := ParseCmd(k, v) + c := ParseCmd(k, s.Root, v) s.Dev = &c } else if k == "build" { - c := ParseCmd(k, v) + c := ParseCmd(k, s.Root, v) s.Build = &c - } else if k == "root" { - str := v.(string) - s.Root = str } } - // fmt.Println(s) return s } @@ -70,19 +69,9 @@ func (s *Service) ToCobraCommand() cobra.Command { allCmds["test"] = &s.Test.Cmd } - for k, c := range allCmds { - svcCmd.AddCommand(&cobra.Command{ - Use: k, - Short: k + " command", - Run: func(cmd *cobra.Command, args []string) { - e := exec.Command("bash", "-c", c.Run) - e.Stdout = os.Stdout - e.Stderr = os.Stderr - - e.Run() - - }, - }) + for _, c := range allCmds { + cobraCmd := c.ToCobraCommand() + svcCmd.AddCommand(&cobraCmd) } return svcCmd diff --git a/optimus.yaml b/optimus.yaml index eb08140..c5f29e4 100644 --- a/optimus.yaml +++ b/optimus.yaml @@ -1,10 +1,8 @@ global: shell_cmd: nu -c - include: - ./config -# script that initializes your dev environment start: description: | Start the application @@ -23,11 +21,13 @@ e2e_tests: services: frontend: - root: ./frontend + root: ./config dev: shell: nu -c run: | - pnpm i; pnpm dev; + ls + build: | + ls -someCmd: | - echo "some command" \ No newline at end of file +lscmd: | + ls \ No newline at end of file