2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2025-06-23 22:02:25 +00:00

improved commands output: add --time format option and color output

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2022-09-09 13:22:04 +02:00
parent 77eac66d01
commit 9893c8a95a
7 changed files with 106 additions and 23 deletions

View File

@ -18,9 +18,10 @@ import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/sirupsen/logrus"
)
var (
@ -31,17 +32,18 @@ var (
func SetDebug(debug bool) {
if debug {
Run = RunStdout
Run = RunDebug
logrus.SetLevel(logrus.DebugLevel)
} else {
Run = RunNoOut
}
}
func RunStdout(ctx context.Context, c string, args ...string) error {
fmt.Printf("\n$ %s %s\n", c, strings.Join(args, " "))
func RunDebug(ctx context.Context, c string, args ...string) error {
logrus.Debugf("$ %s %s", c, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, c, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdout = logrus.StandardLogger().WriterLevel(logrus.DebugLevel)
cmd.Stderr = logrus.StandardLogger().WriterLevel(logrus.DebugLevel)
return cmd.Run()
}