// Execute runs the defined command
Execute runs the defined command
// Execute runs the defined command
func (command Command) Execute() (result model.Result) {
	fmt.Println(command.Description)
	log.Printf("Executing: %s in %s", command.Command, command.Path)
	cmd := exec.Command(command.Command, command.Args...)
	cmd.Dir = command.Path
	var writer io.Writer
	var stdBuffer bytes.Buffer
	if command.Silent {
		writer = io.MultiWriter(&stdBuffer)
	} else {
		writer = io.MultiWriter(os.Stdout, &stdBuffer)
	}
	cmd.Stdout = writer
	cmd.Stderr = writer
	if err := cmd.Run(); err != nil {
		log.Print(err)
		return model.Result{
			Message:       err.Error(),
			WasSuccessful: false,
		}
	}
	log.Println("Command output:")
	log.Println("-----------------------------------")
	scanner := bufio.NewScanner(&stdBuffer)
	for scanner.Scan() {
		log.Println(scanner.Text())
	}
	log.Println("-----------------------------------")
	result.WasSuccessful = true
	result.Message = "The command \"" + command.Command + "\"" + " was executed successfully."
	return result
}
func ( command Command ) Execute ( ) ( result model . Result ) { fmt . Println ( command . Description ) log . Printf ( " " , command . Command , command . Path ) cmd := exec . Command ( command . Command , command . Args ... ) cmd . Dir = command . Path var writer io . Writer var stdBuffer bytes . Buffer if command . Silent { writer = io . MultiWriter ( & stdBuffer ) } else { writer = io . MultiWriter ( os . Stdout , & stdBuffer ) } cmd . Stdout = writer cmd . Stderr = writer if err := cmd . Run ( ) ; err != nil { log . Print ( err ) return model . Result { Message : err . Error ( ) , WasSuccessful : false , } } log . Println ( " " ) log . Println ( " " ) scanner := bufio . NewScanner ( & stdBuffer ) for scanner . Scan ( ) { log . Println ( scanner . Text ( ) ) } log . Println ( " " ) result . WasSuccessful = true result . Message = " \" " + command . Command + " \" " + " " return result }
Execute runs the defined command
Execute runs the defined command
// Create the command // If it's silent we only write to the buffer // Execute the command // Read the output of the executed command // For now we just log it. But it could be later used to determine the success of running the command.