utils

package
v0.0.0-...-4aaa17f Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package utils provides utility functions.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ForbiddenError is a ChangelogError object indicating the user does not have
	// permission to access a resource
	ForbiddenError = &UtilChangelogError{
		httpCode: "403",
		header:   "No Access",
		err:      "This account does not have access to internal repositories. Please retry with an authorized account, or select the external button to query from publically accessible sources.",
	}

	// InternalServerError is a ChangelogError object indicating an internal error
	InternalServerError = &UtilChangelogError{
		httpCode: "500",
		header:   "Internal Server Error",
		err:      "An unexpected error occurred while retrieving the requested information.",
	}
)

Functions

func CheckClose

func CheckClose(closer io.Closer, errMsgOnClose string, err *error)

CheckClose closes an io.Closer and checks its error. Useful for checking the errors on deferred Close() behaviors.

func CheckFileExists

func CheckFileExists(path string) (bool, error)

func Commits

func Commits(client gitilesProto.GitilesClient, repo string, committish string, ancestor string, querySize int) ([]*git.Commit, bool, error)

Commits retrieves querySize commits that occur between a committish and an ancestor for a given repository. Returns a list of commits and a bool that is set to true if there are more than querySize commits between the two provided committishs.

func ConvertImageToRaw

func ConvertImageToRaw(inputFile, destFile string) error

ConvertImageToRaw converts the image at inputFile to raw format and saves it at destFile

func ConvertImageToVMDK

func ConvertImageToVMDK(inputFile, destFile string) error

ConvertImageToVMDK converts the image at inputFile to vmdk format and saves it at destFile

func CopyFile

func CopyFile(src, dest string) error

CopyFile copies a file from src to dest.

func CreateGerritURL

func CreateGerritURL(gitilesHostURL string) (string, error)

CreateGerritURL creates a Gerrit URL from a given Gitiles Host URL. For example: If the given Gitiles Host URL is: https://cos.googlesource.com, then it will return https://cos-review.googlesource.com. In case the Gitiles Host URL is in incorrect format, error is returned.

func CreateTarFile

func CreateTarFile(tarFilename string, files map[string][]byte) error

CreateTarFile creates a tar archive file given a map of {filename: content}.

func Cut

func Cut(s, sep string) (before, after string, found bool)

Cut slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, "", false.

func DownloadContentFromURL

func DownloadContentFromURL(url, outputPath, infoStr string) error

DownloadContentFromURL downloads file from a given URL.

func DownloadFromGCS

func DownloadFromGCS(destDir, gcsBucket, gcsPath string) error

DownloadFromGCS downloads an object from the given GCS path.

func DownloadManifest

func DownloadManifest(client gitilesProto.GitilesClient, manifestRepo, buildNum string) (*gitilesProto.DownloadFileResponse, error)

DownloadManifest retrieves a manifest file from Git on Borg for a specific build number

func Flock

func Flock() *os.File

Flock exclusively locks a special file on the host to make sure only one calling process is running at any time.

func GerritErrCode

func GerritErrCode(err error) string

GerritErrCode parse a Gerrit error and returns an HTTP error code associated with the error. Returns 500 if no error code is found.

func GetDefaultVMToken

func GetDefaultVMToken() (string, error)

GetDefaultVMToken returns the default GCE service account of the COS VM the program is running on.

func GetGCEMetadata

func GetGCEMetadata(metadataPath string) (string, error)

GetGCEMetadata queries GCE metadata server to get the value of a given metadata key.

func GitilesErrCode

func GitilesErrCode(err error) string

GitilesErrCode parses a Gitiles error message and returns an HTTP error code associated with the error. Returns 500 if no error code is found.

func IsDirEmpty

func IsDirEmpty(dirName string) (bool, error)

IsDirEmpty returns whether a given directory is empty.

func ListGCSBucket

func ListGCSBucket(bucket, prefix string) ([]string, error)

ListGCSBucket lists the objects whose names begin with the given prefix in the given GCS bucket.

func LoadEnvFromFile

func LoadEnvFromFile(prefix, filePath string) (map[string]string, error)

LoadEnvFromFile reads an env file from fs into memory as a map.

func MoveFile

func MoveFile(src, dest string) error

MoveFile moves a file from src to dest. Avoid to use os.Rename as the src and dst may on different filesystems, e.g. (container temp fs -> host mounted volume).

func QuoteForShell

func QuoteForShell(str string) string

QuoteForShell quotes a string for use in a bash shell.

func RemoveDir

func RemoveDir(inputPath string, errMsgOnRemove string, err *error)

RemoveDir removes the directory at inputPath and checks its error. Useful for checking the errors on deferred remove().

func RunCommand

func RunCommand(args []string, dir string, env []string) error

RunCommand runs a command using exec.Command. The command runs in the working directory "dir" with environment "env" and outputs to stdout and stderr.

func RunCommandAndLogOutput

func RunCommandAndLogOutput(cmd *exec.Cmd, expectError bool) error

RunCommandAndLogOutput runs the given command and logs the stdout and stderr in parallel.

func RunCommandWithExitCode

func RunCommandWithExitCode(args []string, dir string, env []string) (int, error)

RunCommandWithExitCode runs a command using exec.Command. The command runs in the working directory "dir" with environment "env" and outputs to stdout and stderr. Further it returns the exit code of the executed command.

func StringSliceContains

func StringSliceContains(arr []string, elem string) bool

StringSliceContains returns "true" if elem is in arr, "false" otherwise.

Types

type ChangelogError

type ChangelogError interface {
	error
	HTTPCode() string
	Header() string
	HTMLError() string
	Retryable() bool
}

ChangelogError is the error type used by the changelog and findbuild package

type UtilChangelogError

type UtilChangelogError struct {
	// contains filtered or unexported fields
}

UtilChangelogError implements the ChangelogError interface

func BothBuildsNotFound

func BothBuildsNotFound(croslandURL, source, target, sourceBuildNum, targetBuildNum string) *UtilChangelogError

BothBuildsNotFound indicates that neither build was not found

func BuildNotFound

func BuildNotFound(buildNumber string) *UtilChangelogError

BuildNotFound returns a ChangelogError object for changelog indicating the desired build could not be found

func CLInvalidRelease

func CLInvalidRelease(clID, release, instanceURL string) *UtilChangelogError

CLInvalidRelease returns a ChangelogError object for findbuild indicating that the branch a CL was submitted in was not recognized as a release branch

func CLLandingNotFound

func CLLandingNotFound(clID, instanceURL string) *UtilChangelogError

CLLandingNotFound returns a ChangelogError object for findbuild indicating no build was found containing a CL

func CLNotFound

func CLNotFound(clID string) *UtilChangelogError

CLNotFound returns a ChangelogError object for findbuild indicating the provided CL could not be found

func CLNotSubmitted

func CLNotSubmitted(clID, instanceURL string) *UtilChangelogError

CLNotSubmitted returns a ChangelogError object for findbuild indicating that the provided CL has not been submitted

func CLNotUsed

func CLNotUsed(clID, repo, branch, instanceURL string) *UtilChangelogError

CLNotUsed returns a ChangelogError object for findbuild indicating that the repository and branch associated with a CL was not found in any manifest files

func CLTooRecent

func CLTooRecent(clID, instanceURL string) *UtilChangelogError

CLTooRecent returns a ChangelogError object for findbuild indicating the provided CL could not be found

func (*UtilChangelogError) Error

func (e *UtilChangelogError) Error() string

Error returns the error string

func (*UtilChangelogError) HTMLError

func (e *UtilChangelogError) HTMLError() string

HTMLError returns an HTML version of the error string

func (*UtilChangelogError) HTTPCode

func (e *UtilChangelogError) HTTPCode() string

HTTPCode retrieves the HTTP error code associated with the error ex. 400

func (*UtilChangelogError) Header

func (e *UtilChangelogError) Header() string

Header retrieves the full HTTP status associated with the error ex. 400 Bad Request

func (*UtilChangelogError) Retryable

func (e *UtilChangelogError) Retryable() bool

Retryable indicates whether increasing the search range could resolve this error

Jump to

Keyboard shortcuts

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