diff --git a/command/command.go b/command/command.go index a2e66b3..8bc4a93 100644 --- a/command/command.go +++ b/command/command.go @@ -5,6 +5,9 @@ import ( "os" "os/exec" "strings" + "log" + "bytes" + "regexp" "pikman/alpine" "pikman/arch" @@ -77,16 +80,21 @@ func (c *Command) processCommand() error { var err error if c.OsType != types.Ubuntu && c.OsType != types.Flatpak && c.Command != "init" && c.ContainerName == "" { - cmd := exec.Command("/bin/bash", "-c", "apx subsystems list | grep ^" + c.ContainerSubsystem() + "$") - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - if err := cmd.Wait(); err != nil { + cmd := exec.Command("/bin/bash", "-c", "apx subsystems list") + var outb bytes.Buffer + cmd.Stdout = &outb + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + log.Fatal(err) + } + re := regexp.MustCompile("^" + c.ContainerSubsystem() + "$") + if re.MatchString(outb.String()) { fmt.Println("Warning: Subsystem hasn't been pre-initialized, initializing...") - cmd_exec := exec.Command("/bin/bash", "-c", "apx subsystems new -n " + c.ContainerSubsystem() + " -s " + c.ApxSubsystem()) + cmd_exec := exec.Command("/bin/bash", "-c", "apx subsystems new -n " + c.ContainerSubsystem() + " -s " + c.ApxSubsystem()) + cmd_exec.Stdout = os.Stdout cmd_exec.Stdin = os.Stdin cmd_exec.Stderr = os.Stderr - cmd_exec.Stdout = os.Stdout if err := cmd_exec.Run(); err != nil { fmt.Println("Apx Error: ", err) return err @@ -95,11 +103,16 @@ func (c *Command) processCommand() error { } if c.OsType != types.Ubuntu && c.OsType != types.Flatpak && c.Command != "init" && c.ContainerName != "" { - cmd := exec.Command("/bin/bash", "-c", "apx subsystems list | grep ^" + c.ContainerName + "$") - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - if err := cmd.Wait(); err != nil { + cmd := exec.Command("/bin/bash", "-c", "apx subsystems list") + var outb bytes.Buffer + cmd.Stdout = &outb + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + log.Fatal(err) + } + re := regexp.MustCompile("^" + c.ContainerName + "$") + if re.MatchString(outb.String()) { fmt.Println("Warning: Subsystem hasn't been pre-initialized, initializing...") cmd_exec := exec.Command("/bin/bash", "-c", "apx subsystems new -n " + c.ContainerName + " -s " + c.ApxSubsystem()) cmd_exec.Stdin = os.Stdin