util

package
v1.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2021 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NpmDir   = "node_modules"
	SetupDir = ".setup"
)

Variables

View Source
var (
	PyEnv     = filepath.Join(SetupDir, "venv")
	GoLinter  = filepath.Join(SetupDir, "golangci-lint")
	Terraform = filepath.Join(SetupDir, "terraform")

	PyTargets = []string{
		"internal/compliance/remediation_aws",
		"internal/compliance/policy_engine",
		"internal/log_analysis/rules_engine",
	}
)
View Source
var MaxWorkers = func() int {
	n := runtime.NumCPU()

	if IsRunningInCI() {
		return n
	}

	if n > 1 {
		return n - 1
	}
	return 1
}()

MaxWorkers limits CPU-intensive operations depending on the environment.

Functions

func AccountID added in v1.16.0

func AccountID(config aws.Config) string

Returns the 12-digit account ID associated with the current session.

The result will be cached for subsequent queries.

func ByteCountSI added in v1.16.0

func ByteCountSI(b int64) string

Returns human-readable byte size, e.g. "5.1 MB" or "1.2 kB" From https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format

func CommitSha added in v1.14.0

func CommitSha() string

Returns short commit string. For example, "64391f1e"

func EcrRepoURI added in v1.16.0

func EcrRepoURI(accountID, region, repoName string) string

Returns ECR image repo uri

func IsRunningInCI

func IsRunningInCI() bool

Returns true if the mage command is running inside the CI environment

func MustReadFile

func MustReadFile(path string) []byte

Wrapper around ioutil.ReadFile, logging errors as fatal.

func MustWalk

func MustWalk(root string, handler func(string, os.FileInfo) error)

Wrapper around filepath.Walk, logging errors as fatal.

func MustWriteFile

func MustWriteFile(path string, data []byte)

Wrapper around ioutil.WriteFile, creating the parent dirs if needed and logging errors as fatal.

func NodePath

func NodePath(binary string) string

Path to a node binary

func ParseTemplate

func ParseTemplate(path string, out interface{}) error

Parse a CloudFormation template and unmarshal into the out parameter. The out parameter must be a map or a pointer to a struct.

Short-form functions like "!If" and "!Sub" will be replaced with "Fn::" objects.

func PipPath

func PipPath(lib string) string

Path to a pip binary

func PublicAssetsBucket

func PublicAssetsBucket(region string) string

The name of the bucket containing published Panther releases

func RunTask

func RunTask(results chan<- TaskResult, name string, task func() error)

Ugly task queue hack to limit concurrent tasks

func RunWithCapturedOutput

func RunWithCapturedOutput(cmd string, args ...string) error

Run a command, capturing stdout and stderr unless the command errors or we're in verbose mode.

This is helpful for tools which print unwanted info to stderr even when successful or, conversely, tools which output failing tests to stdout that we want to show even in non-verbose mode.

Both outputs will be printed if the command returns an error.

Similar to sh.Run(), except sh.Run() only hides stdout in non-verbose mode.

func S3ObjectURL added in v1.16.0

func S3ObjectURL(region, bucket, key string) string

Returns S3 URL using virtual addressing ("BUCKET.s3.REGION.SUFFIX/KEY")

func S3URI added in v1.16.0

func S3URI(bucket, key string) string

Returns s3 URI ("s3://bucket/key")

func Semver added in v1.14.0

func Semver() string

Returns semantic version (from VERSION file). For example, "1.14.0-RC"

func URLSuffix added in v1.16.0

func URLSuffix(region string) string

Return the URL suffix for the partition associated with the given region.

func WaitForTasks

func WaitForTasks(log *zap.SugaredLogger, results chan TaskResult, start, end, total int) error

Wait for the given number of goroutines to finish, logging results as they come in.

This can be invoked multiple times to track progress over many parallel chunks of work:

"start" is the first message number to show in the output
"end" is the last message number to show in the output
"total" is the total number of tasks (across all invocations)

This will consume exactly (end - start) + 1 messages in the channel.

Returns a combined error message at the end if there were any failures.

Types

type TaskResult

type TaskResult struct {
	Summary string
	Err     error
}

Track results when executing similar tasks in parallel

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL