- 
      
- 
        Save farhany/92adc4ad0067ca8b6b67ea78b5a441d5 to your computer and use it in GitHub Desktop. 
    Go exec.Command example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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