Skip to content

Instantly share code, notes, and snippets.

@AnthoniG
Forked from danesparza/exec.go
Created June 28, 2023 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnthoniG/8135e36e635b86ec5826bbfe9f6217cd to your computer and use it in GitHub Desktop.
Save AnthoniG/8135e36e635b86ec5826bbfe9f6217cd to your computer and use it in GitHub Desktop.
Go exec.Command example
package main
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
func main() {
// Split the entire command up using ' -' as the delimeter
parts := strings.Split(`pushnotify.exe --to=gqukgkJyLtchaLE41WUEJ2qFM7Q3tb --title=New Plex content --message={showname} season {showseasonnumber} episode {showepisodenumber} just landed on Plex --sound=magic`, " -")
// The first part is the command, the rest are the args:
head := parts[0]
args := parts[1:len(parts)]
// Format the command
cmd := exec.Command(head, args...)
/*
// Sanity check -- just print out the detected args:
for _, arg := range cmd.Args {
log.Println(arg)
}
*/
// Sanity check -- capture stdout and stderr:
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
// Run the command
cmd.Run()
// Output our results
fmt.Printf("Result: %v / %v", out.String(), stderr.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment