Documentation ¶
Overview ¶
Package utils provides utility functions.
Index ¶
- Variables
- func ArrayElements(array string) map[string]string
- func CheckClose(closer io.Closer, errMsgOnClose string, err *error)
- func CheckFileExists(path string) (bool, error)
- func Commits(client gitilesProto.GitilesClient, repo string, committish string, ...) ([]*git.Commit, bool, error)
- func ConvertImageToRaw(inputFile, destFile string) error
- func ConvertImageToVMDK(inputFile, destFile string) error
- func CopyFile(src, dest string) error
- func CreateGerritURL(gitilesHostURL string) (string, error)
- func CreateTarFile(tarFilename string, files map[string][]byte) error
- func Cut(s, sep string) (before, after string, found bool)
- func DownloadContentFromURL(url, outputPath, infoStr string) error
- func DownloadFromGCS(destDir, gcsBucket, gcsPath string) error
- func DownloadManifest(client gitilesProto.GitilesClient, manifestRepo, buildNum string) (*gitilesProto.DownloadFileResponse, error)
- func Flock() *os.File
- func GerritErrCode(err error) string
- func GetDefaultVMToken() (string, error)
- func GetGCEMetadata(metadataPath string) (string, error)
- func GitilesErrCode(err error) string
- func IsDirEmpty(dirName string) (bool, error)
- func ListGCSBucket(bucket, prefix string) ([]string, error)
- func LoadEnvFromFile(prefix, filePath string) (map[string]string, error)
- func MoveFile(src, dest string) error
- func QuoteForShell(str string) string
- func RemoveDir(inputPath string, errMsgOnRemove string, err *error)
- func RunCommand(args []string, dir string, env []string) error
- func RunCommandAndLogOutput(cmd *exec.Cmd, expectError bool) error
- func RunCommandString(dir string, command string) error
- func RunCommandWithExitCode(args []string, dir string, env []string) (int, error)
- func SourceFile(file string) (map[string]string, error)
- func SourceString(contents string) (map[string]string, error)
- func StringSliceContains(arr []string, elem string) bool
- type ChangelogError
- type UtilChangelogError
- func BothBuildsNotFound(croslandURL, source, target, sourceBuildNum, targetBuildNum string) *UtilChangelogError
- func BuildNotFound(buildNumber string) *UtilChangelogError
- func CLInvalidRelease(clID, release, instanceURL string) *UtilChangelogError
- func CLLandingNotFound(clID, instanceURL string) *UtilChangelogError
- func CLNotFound(clID string) *UtilChangelogError
- func CLNotSubmitted(clID, instanceURL string) *UtilChangelogError
- func CLNotUsed(clID, repo, branch, instanceURL string) *UtilChangelogError
- func CLTooRecent(clID, instanceURL string) *UtilChangelogError
Constants ¶
This section is empty.
Variables ¶
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 ArrayElements ¶
ArrayElements splits a bash array output by `declare -p` into its indexed elements and returns the resulting map from keys to values. For example, the input string `X=([0]="abc" [1]="def")` should return map[string]string { "0": "abc", "1": "def" }.
func CheckClose ¶
CheckClose closes an io.Closer and checks its error. Useful for checking the errors on deferred Close() behaviors.
func CheckFileExists ¶
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 ¶
ConvertImageToRaw converts the image at inputFile to raw format and saves it at destFile
func ConvertImageToVMDK ¶
ConvertImageToVMDK converts the image at inputFile to vmdk format and saves it at destFile
func CreateGerritURL ¶
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 ¶
CreateTarFile creates a tar archive file given a map of {filename: content}.
func Cut ¶
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 ¶
DownloadContentFromURL downloads file from a given URL.
func DownloadFromGCS ¶
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 ¶
Flock exclusively locks a special file on the host to make sure only one calling process is running at any time.
func GerritErrCode ¶
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 ¶
GetDefaultVMToken returns the default GCE service account of the COS VM the program is running on.
func GetGCEMetadata ¶
GetGCEMetadata queries GCE metadata server to get the value of a given metadata key.
func GitilesErrCode ¶
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 ¶
IsDirEmpty returns whether a given directory is empty.
func ListGCSBucket ¶
ListGCSBucket lists the objects whose names begin with the given prefix in the given GCS bucket.
func LoadEnvFromFile ¶
LoadEnvFromFile reads an env file from fs into memory as a map.
func MoveFile ¶
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 ¶
QuoteForShell quotes a string for use in a bash shell.
func RemoveDir ¶
RemoveDir removes the directory at inputPath and checks its error. Useful for checking the errors on deferred remove().
func RunCommand ¶
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 ¶
RunCommandAndLogOutput runs the given command and logs the stdout and stderr in parallel.
func RunCommandString ¶
RunCommandString runs a command string from a provided directory and logs the combined stdout and stderr. If there is no error, the output will be logged at Info level. If there is an error, the output will be logged at Error level.
func RunCommandWithExitCode ¶
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 SourceFile ¶
SourceFile sources a bash file and returns the shell variables as a map.
func SourceString ¶
SourceString sources a string as a bash file and returns the shell variables as a map.
func StringSliceContains ¶
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