Example #1
0
// fileContains checks whether a file matches a given regex
func fileContains(parameters []string) (exitCode int, exitMessage string) {
	path := parameters[0]
	regex := wrkutils.ParseUserRegex(parameters[1])
	if regex.Match(wrkutils.FileToBytes(path)) {
		return 0, ""
	}
	msg := "File does not match regexp:\n\tFile: "
	return 1, msg + path + "\n\tRegexp" + regex.String()
}
Example #2
0
// checksum checks the hash of a given file using the given algorithm
func checksum(parameters []string) (exitCode int, exitMessage string) {
	// getChecksum returns the checksum of some data, using a specified
	// algorithm
	getChecksum := func(algorithm string, data []byte) (checksum string) {
		algorithm = strings.ToUpper(algorithm)
		// default
		hasher := md5.New()
		switch algorithm {
		case "SHA1":
			hasher = sha1.New()
		case "SHA224":
			hasher = sha256.New224()
		case "SHA256":
			hasher = sha256.New()
		case "SHA384":
			hasher = sha512.New384()
		case "SHA512":
			hasher = sha512.New()
		}
		hasher.Write(data)
		str := hex.EncodeToString(hasher.Sum(nil))
		return str

	}
	// getFileChecksum is self-explanatory
	getFileChecksum := func(algorithm string, path string) (checksum string) {
		return getChecksum(algorithm, wrkutils.FileToBytes(path))
	}

	algorithm := parameters[0]
	checkAgainst := parameters[1]
	path := parameters[2]
	chksum := getFileChecksum(algorithm, path)
	// TODO warn on unequal lengths
	if chksum == checkAgainst {
		return 0, ""
	}
	msg := "Checksums do not match for file: " + path
	return wrkutils.GenericError(msg, checkAgainst, []string{chksum})
}
Example #3
0
// checklistFromFile reads the file at the path and parses its utf8 encoded json
// data, turning it into a checklist struct.
func checklistFromFile(path string) (chklst Checklist) {
	return checklistFromBytes(wrkutils.FileToBytes(path))
}