mirror of
https://github.com/TECHNOFAB11/powerproto.git
synced 2026-02-02 09:25:07 +01:00
feat(*): dryRun/debug mode/mode plugins/googleapis
Signed-off-by: storyicon <yuanchao@bilibili.com>
This commit is contained in:
parent
9aac714c32
commit
da77c8086d
26 changed files with 730 additions and 126 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -15,11 +15,15 @@
|
|||
package progressbar
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/vbauerster/mpb/v7"
|
||||
"github.com/vbauerster/mpb/v7/decor"
|
||||
|
||||
"github.com/storyicon/powerproto/pkg/consts"
|
||||
"github.com/storyicon/powerproto/pkg/util/logger"
|
||||
)
|
||||
|
||||
// ProgressBar implements a customizable progress bar
|
||||
|
|
@ -58,10 +62,12 @@ func newEmbedProgressBar(container *mpb.Progress, bar *mpb.Bar) *progressBar {
|
|||
}
|
||||
}
|
||||
|
||||
// Incr is used to increase progress
|
||||
func (s *progressBar) Incr() {
|
||||
s.bar.Increment()
|
||||
}
|
||||
|
||||
// Wait is used to wait for the rendering of the progress bar to complete
|
||||
func (s *progressBar) Wait() {
|
||||
s.container.Wait()
|
||||
}
|
||||
|
|
@ -79,8 +85,50 @@ func getSpinner() []string {
|
|||
}
|
||||
}
|
||||
|
||||
type fakeProgressbar struct {
|
||||
prefix string
|
||||
suffix string
|
||||
total int
|
||||
current int
|
||||
logger.Logger
|
||||
}
|
||||
|
||||
func (f *fakeProgressbar) Incr() {
|
||||
if f.current < f.total {
|
||||
f.current++
|
||||
}
|
||||
}
|
||||
|
||||
// Wait is used to wait for the rendering of the progress bar to complete
|
||||
func (f *fakeProgressbar) Wait() {}
|
||||
|
||||
// SetSuffix is used to set the prefix of progress bar
|
||||
func (f *fakeProgressbar) SetPrefix(format string, args ...interface{}) {
|
||||
f.prefix = fmt.Sprintf(format, args...)
|
||||
}
|
||||
|
||||
// SetSuffix is used to set the suffix of progress bar
|
||||
func (f *fakeProgressbar) SetSuffix(format string, args ...interface{}) {
|
||||
f.suffix = fmt.Sprintf(format, args...)
|
||||
f.LogInfo(map[string]interface{}{
|
||||
"progress": fmt.Sprintf("%3.f", float64(f.current)/float64(f.total)*100),
|
||||
"stage": f.prefix,
|
||||
}, f.suffix)
|
||||
}
|
||||
|
||||
func newFakeProgressbar(total int) ProgressBar {
|
||||
return &fakeProgressbar{
|
||||
total: total,
|
||||
Logger: logger.NewDefault("progress"),
|
||||
}
|
||||
}
|
||||
|
||||
// GetProgressBar is used to get progress bar
|
||||
func GetProgressBar(count int) ProgressBar {
|
||||
func GetProgressBar(ctx context.Context, count int) ProgressBar {
|
||||
if consts.IsDebugMode(ctx) {
|
||||
return newFakeProgressbar(count)
|
||||
}
|
||||
|
||||
var progressBar *progressBar
|
||||
container := mpb.New()
|
||||
bar := container.Add(int64(count),
|
||||
|
|
|
|||
|
|
@ -26,8 +26,13 @@ import (
|
|||
|
||||
// ContainsEmpty is used to check whether items contains empty string
|
||||
func ContainsEmpty(items ...string) bool {
|
||||
return Contains(items, "")
|
||||
}
|
||||
|
||||
// Contains is used to check whether the target is in items
|
||||
func Contains(items []string, target string) bool {
|
||||
for _, item := range items {
|
||||
if item == "" {
|
||||
if item == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
@ -103,4 +108,4 @@ func GetBinaryFileName(name string) string {
|
|||
return name
|
||||
}
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue