feat(*): dryRun/debug mode/mode plugins/googleapis

Signed-off-by: storyicon <yuanchao@bilibili.com>
This commit is contained in:
storyicon 2021-07-21 20:25:38 +08:00
parent 9aac714c32
commit da77c8086d
No known key found for this signature in database
GPG key ID: 245915D985F966CF
26 changed files with 730 additions and 126 deletions

View file

@ -21,44 +21,11 @@ import (
"os"
"os/exec"
"github.com/storyicon/powerproto/pkg/consts"
"github.com/storyicon/powerproto/pkg/util"
"github.com/storyicon/powerproto/pkg/util/logger"
)
type dryRun struct{}
type ignoreDryRun struct{}
type disableAction struct{}
// WithIgnoreDryRun is used to inject ignore dryRun flag into context
func WithIgnoreDryRun(ctx context.Context) context.Context {
return context.WithValue(ctx, ignoreDryRun{}, "true")
}
// WithDisableAction is used to disable post action/shell
func WithDisableAction(ctx context.Context) context.Context {
return context.WithValue(ctx, disableAction{}, "true")
}
// WithDryRun is used to inject dryRun flag into context
func WithDryRun(ctx context.Context) context.Context {
return context.WithValue(ctx, dryRun{}, "true")
}
// IsDisableAction is used to decide whether to disable PostAction and PostShell
func IsDisableAction(ctx context.Context) bool {
return ctx.Value(disableAction{}) != nil
}
// IsIgnoreDryRun is used to determine whether it is currently in ignore DryRun mode
func IsIgnoreDryRun(ctx context.Context) bool {
return ctx.Value(ignoreDryRun{}) != nil
}
// IsDryRun is used to determine whether it is currently in DryRun mode
func IsDryRun(ctx context.Context) bool {
return ctx.Value(dryRun{}) != nil
}
// Execute is used to execute commands, return stdout and execute errors
func Execute(ctx context.Context,
log logger.Logger,
@ -67,17 +34,21 @@ func Execute(ctx context.Context,
cmd.Env = append(os.Environ(), env...)
cmd.Dir = dir
if IsDryRun(ctx) && !IsIgnoreDryRun(ctx) {
if consts.IsDryRun(ctx) && !consts.IsIgnoreDryRun(ctx) {
log.LogInfo(map[string]interface{}{
"command": cmd.String(),
"dir": cmd.Dir,
}, "DryRun")
}, consts.TextDryRun)
return nil, nil
}
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
log.LogDebug(map[string]interface{}{
"command": cmd.String(),
"dir": cmd.Dir,
}, consts.TextExecuteCommand)
if err := cmd.Run(); err != nil {
return nil, &ErrCommandExec{
Err: err,