run-sync-go/run-sync.go

57 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-04-16 11:12:56 +02:00
/*
* Copyright (c) 2024 - now, Alex <alex@milyutin.work>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Alex <alex@milyutin.work>
* @copyright Copyright (c) 2024 - now, Alex <alex@milyutin.work>
* @since 16.04.2024
*
*/
package run_sync
import (
"bytes"
"log"
"os/exec"
"time"
)
func RunProcessSync(command string, args ...string) (out *bytes.Buffer, err error) {
cmd := exec.Command(command, args...)
// Output buffer
out = &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = out
err = cmd.Start()
if err != nil {
return
}
err = cmd.Wait()
if err != nil {
return
}
time.Sleep(5 * time.Second)
output := []string{"[runProcessSync]", command}
output = append(output, args...)
output = append(output, "Output:", out.String())
log.Println(output)
return
}