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

@ -18,20 +18,35 @@ import (
"os"
"path/filepath"
"github.com/fatih/color"
"github.com/storyicon/powerproto/pkg/util/logger"
)
// defines a set of const value
const (
// ConfigFileName defines the config file name
ConfigFileName = "powerproto.yaml"
// The default include can be referenced by this key in import paths
KeyPowerProtoInclude = "$POWERPROTO_INCLUDE"
// The googleapis can be referenced by this key in import paths
KeyPowerProtoGoogleAPIs = "$POWERPROTO_GOOGLEAPIS"
// KeySourceRelative can be specified in import paths to refer to
// the folder where the current proto file is located
KeySourceRelative = "$SOURCE_RELATIVE"
// Defines the program directory of PowerProto, including various binary and include files
EnvHomeDir = "POWERPROTO_HOME"
// ProtobufRepository defines the protobuf repository
ProtobufRepository = "https://github.com/protocolbuffers/protobuf"
// GoogleAPIsRepository defines the google apis repository
GoogleAPIsRepository = "https://github.com/googleapis/googleapis"
)
// defines a set of text style
var (
TextExecuteAction = color.HiGreenString("EXECUTE ACTION")
TextExecuteCommand = color.HiGreenString("EXECUTE COMMAND")
TextDryRun = color.HiGreenString("DRY RUN")
)
var homeDir string

65
pkg/consts/context.go Normal file
View file

@ -0,0 +1,65 @@
// Copyright 2021 storyicon@foxmail.com
//
// 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.
package consts
import (
"context"
)
type debugMode struct{}
type dryRun struct{}
type ignoreDryRun struct{}
type disableAction struct{}
// WithDebugMode is used to set debug mode
func WithDebugMode(ctx context.Context) context.Context {
return context.WithValue(ctx, debugMode{}, "true")
}
// 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 {
ctx = WithDebugMode(ctx)
return context.WithValue(ctx, dryRun{}, "true")
}
// IsDebugMode is used to decide whether to disable PostAction and PostShell
func IsDebugMode(ctx context.Context) bool {
return ctx.Value(debugMode{}) != nil
}
// 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
}