diff --git a/internal/edgeapps/edgeapps.go b/internal/edgeapps/edgeapps.go index ecace61..b5d99a7 100644 --- a/internal/edgeapps/edgeapps.go +++ b/internal/edgeapps/edgeapps.go @@ -239,14 +239,14 @@ func GetEdgeAppStatus(ID string) EdgeAppStatus { func GetEdgeAppServices(ID string) []EdgeAppService { cmdArgs := []string{"-r", ".services | keys[]", utils.GetPath("edgeAppsPath") + ID + configFilename} - servicesString := utils.Exec("yq", cmdArgs) + servicesString := utils.Exec(utils.GetPath("wsPath"), "yq", cmdArgs) serviceSlices := strings.Split(servicesString, "\n") serviceSlices = utils.DeleteEmptySlices(serviceSlices) var edgeAppServices []EdgeAppService for _, serviceID := range serviceSlices { cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "exec", "-T", serviceID, "echo", "'Service Check'"} - cmdResult := utils.Exec("docker-compose", cmdArgs) + cmdResult := utils.Exec(utils.GetPath("wsPath"), "docker-compose", cmdArgs) isRunning := false if cmdResult != "" { isRunning = true @@ -268,7 +268,7 @@ func RunEdgeApp(ID string) EdgeAppStatus { for _, service := range services { cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "start", service.ID} - utils.Exec("docker-compose", cmdArgs) + utils.Exec(utils.GetPath("wsPath"), "docker-compose", cmdArgs) } @@ -289,7 +289,7 @@ func StopEdgeApp(ID string) EdgeAppStatus { for _, service := range services { cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "stop", service.ID} - utils.Exec("docker-compose", cmdArgs) + utils.Exec(utils.GetPath("wsPath"), "docker-compose", cmdArgs) } @@ -326,7 +326,7 @@ func DisableOnline(ID string) MaybeEdgeApp { log.Println("myedge.app environment file for " + ID + " not found. No need to delete.") } else { cmdArgs := []string{envFilePath} - utils.Exec("rm", cmdArgs) + utils.Exec(utils.GetPath("wsPath"), "rm", cmdArgs) } buildFrameworkContainers() @@ -338,7 +338,7 @@ func DisableOnline(ID string) MaybeEdgeApp { func buildFrameworkContainers() { cmdArgs := []string{utils.GetPath("wsPath") + "ws", "--build"} - utils.ExecAndStream("sh", cmdArgs) + utils.ExecAndStream(utils.GetPath("wsPath"), "sh", cmdArgs) time.Sleep(defaultContainerOperationSleepTime) diff --git a/internal/storage/storage.go b/internal/storage/storage.go index e750084..58628db 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -74,7 +74,7 @@ func GetDevices() []Device { var devices []Device cmdArgs := []string{"--raw", "--bytes", "--noheadings"} - scanner := utils.ExecAndGetLines("lsblk", cmdArgs) + scanner := utils.ExecAndGetLines("/", "lsblk", cmdArgs) var currentDevice Device var currentPartitions []Partition diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index add1596..f8e8978 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -284,13 +284,13 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string { fmt.Println("Executing taskSetupTunnel") cmdargs := []string{"gen", "--name", args.NodeName, "--token", args.BootnodeToken, args.BootnodeAddress + ":8655", "--prefix", args.AssignedAddress} - utils.Exec("tinc-boot", cmdargs) + utils.Exec(utils.GetPath("wsPath"), "tinc-boot", cmdargs) cmdargs = []string{"start", "tinc@dnet"} - utils.Exec("systemctl", cmdargs) + utils.Exec(utils.GetPath("wsPath"), "systemctl", cmdargs) cmdargs = []string{"enable", "tinc@dnet"} - utils.Exec("systemctl", cmdargs) + utils.Exec(utils.GetPath("wsPath"), "systemctl", cmdargs) output := "OK" // Better check / logging of command execution result. return output diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 776c0f3..ba1b6d0 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -15,14 +15,14 @@ import ( ) // ExecAndStream : Runs a terminal command, but streams progress instead of outputting. Ideal for long lived process that need to be logged. -func ExecAndStream(command string, args []string) { +func ExecAndStream(path string, command string, args []string) { cmd := exec.Command(command, args...) var stdoutBuf, stderrBuf bytes.Buffer cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf) cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf) - cmd.Dir = GetPath("wsPath") + cmd.Dir = path err := cmd.Run() @@ -36,13 +36,13 @@ func ExecAndStream(command string, args []string) { } // Exec : Runs a terminal Command, catches and logs errors, returns the result. -func Exec(command string, args []string) string { +func Exec(path string, command string, args []string) string { cmd := exec.Command(command, args...) var out bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &out cmd.Stderr = &stderr - cmd.Dir = GetPath("wsPath") + cmd.Dir = path err := cmd.Run() if err != nil { // TODO: Deal with possibility of error in command, allow explicit error handling and return proper formatted stderr @@ -56,8 +56,8 @@ func Exec(command string, args []string) string { } // Exec : Runs a terminal Command, returns the result as a *bufio.Scanner type, split in lines and ready to parse. -func ExecAndGetLines(command string, args []string) *bufio.Scanner { - cmdOutput := Exec(command, args) +func ExecAndGetLines(path string, command string, args []string) *bufio.Scanner { + cmdOutput := Exec(path, command, args) cmdOutputReader := strings.NewReader(cmdOutput) scanner := bufio.NewScanner(cmdOutputReader) scanner.Split(bufio.ScanLines) @@ -105,10 +105,10 @@ func GetPath(pathKey string) string { // Read whole of .env file to map. var env map[string]string env, err := godotenv.Read() - targetPath := "" + var targetPath string if err != nil { - fmt.Println("Project .env file not found withing project root. Using only hardcoded path variables.") + targetPath = "" } switch pathKey {