Skip to content

Commit

Permalink
pipe raw stdout and stderror from commands to the console's stdout & …
Browse files Browse the repository at this point in the history
…stderror directly to avoid color artifacts in command output
  • Loading branch information
flawiddsouza committed Aug 13, 2022
1 parent dcedeb6 commit ccb2349
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ goreleaser release --rm-dist

## Installing on Linux (tested on Ubuntu)
```bash
wget https://github.com/flawiddsouza/shortcommand/releases/download/v0.0.1/shortcommand_0.0.1_Linux_x86_64.tar.gz
tar -xf shortcommand_0.0.1_Linux_x86_64.tar.gz
wget https://github.com/flawiddsouza/shortcommand/releases/download/v0.0.2/shortcommand_0.0.2_Linux_x86_64.tar.gz
tar -xf shortcommand_0.0.2_Linux_x86_64.tar.gz
mv shortcommand ~/.local/bin
rm shortcommand_0.0.1_Linux_x86_64.tar.gz
rm shortcommand_0.0.2_Linux_x86_64.tar.gz
```
You should then be able to use the `shortcommand` command anywhere you are.
35 changes: 12 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -40,11 +40,12 @@ func fileExists(path string) bool {
return false
}

func executeCommand(commandToRun string, currentWorkingDirectory string) error {
colorReset := "\033[0m"
colorRed := "\033[31m"
colorGreen := "\033[32m"
var ConsoleColors = map[string]string{
"Reset": "\033[0m",
"Red": "\033[31m",
}

func executeCommand(commandToRun string, currentWorkingDirectory string) error {
cmd := exec.Command("sh", "-c", commandToRun)
if currentWorkingDirectory != "" {
currentWorkingDirectoryResolved := os.ExpandEnv(currentWorkingDirectory)
Expand All @@ -53,28 +54,16 @@ func executeCommand(commandToRun string, currentWorkingDirectory string) error {
currentWorkingDirectoryResolved = home + currentWorkingDirectoryResolved[1:]
}
if !fileExists(currentWorkingDirectoryResolved) {
fmt.Printf("%sUnable to find given working directory: %s%s\n", colorRed, currentWorkingDirectory, colorReset)
fmt.Printf("%sUnable to find given working directory: %s%s\n", ConsoleColors["Red"], currentWorkingDirectory, ConsoleColors["Reset"])
}
cmd.Dir = currentWorkingDirectoryResolved
}

cmdReader, _ := cmd.StdoutPipe()
stdout := io.Writer(os.Stdout)
stderr := io.Writer(os.Stderr)

scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("%s--> %s\n%s", colorGreen, scanner.Text(), colorReset)
}
}()

cmdReader2, _ := cmd.StderrPipe()

scanner2 := bufio.NewScanner(cmdReader2)
go func() {
for scanner2.Scan() {
fmt.Printf("%s--> %s\n%s", colorRed, scanner2.Text(), colorReset)
}
}()
cmd.Stderr = stderr
cmd.Stdout = stdout

cmd.Start()

Expand Down Expand Up @@ -119,7 +108,7 @@ func main() {
err = executeCommand(commandToRun, command.CurrentWorkingDirectory)
if err != nil {
if i < len(command.Do)-1 {
fmt.Println("Not continuing with the next command in do sequence as previous command errored out")
fmt.Printf("%sNot continuing with the next command in do sequence as previous command errored out%s\n", ConsoleColors["Red"], ConsoleColors["Reset"])
return
}
}
Expand Down

0 comments on commit ccb2349

Please sign in to comment.