Skip to content

Commit

Permalink
commands 'root' prop works properly now
Browse files Browse the repository at this point in the history
  • Loading branch information
nxy7 committed Jun 21, 2023
1 parent f81a96d commit 0430321
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
17 changes: 14 additions & 3 deletions config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
},
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
33 changes: 11 additions & 22 deletions config/service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package config

import (
"os"
"os/exec"

"github.com/spf13/cobra"
)

Expand All @@ -27,27 +24,29 @@ func ParseService(name string, a any) Service {
// fmt.Println("Parsing service")
s := Service{
Name: name,
Root: name,
}

amap, o := a.(map[string]any)
if !o {
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
}

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions optimus.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
global:
shell_cmd: nu -c

include:
- ./config

# script that initializes your dev environment
start:
description: |
Start the application
Expand All @@ -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"
lscmd: |
ls

0 comments on commit 0430321

Please sign in to comment.