コード例 #1
0
ファイル: gather-facts.go プロジェクト: csm/vapor-modules
func (self GatherFacts) Exec(_ types.Value) (types.Value, error) {
	var result types.Map = make(types.Map)
	result[types.Keyword("os")] = types.String(runtime.GOOS)
	result[types.Keyword("arch")] = types.String(runtime.GOARCH)
	var interfaces, err = net.Interfaces()
	if err != nil {
		return nil, err
	}
	var netifs = make(types.Vector, len(interfaces))
	for i, iface := range interfaces {
		var facemap = make(types.Map)
		facemap[types.Keyword("name")] = types.String(iface.Name)
		facemap[types.Keyword("mac-address")] = types.String(iface.HardwareAddr.String())
		facemap[types.Keyword("loopback?")] = types.Bool(iface.Flags&net.FlagLoopback != 0)
		var addrs, err = iface.Addrs()
		if err != nil {
			return nil, err
		}
		var v4list = (*types.List)(list.New())
		var v6list = (*types.List)(list.New())
		for _, addr := range addrs {
			var addrmap = make(types.Map)
			switch ip := addr.(type) {
			case *net.IPAddr:
				var v4 = ip.IP.To4()
				addrmap[types.Keyword("address")] = types.String(ip.IP.String())
				if v4 != nil {
					v4list.Insert(addrmap)
				} else {
					v6list.Insert(addrmap)
				}
				break

			case *net.IPNet:
				var v4 = ip.IP.To4()
				addrmap[types.Keyword("address")] = types.String(ip.IP.String())
				size, bits := ip.Mask.Size()
				addrmap[types.Keyword("prefix-length")] = types.Int(size)
				addrmap[types.Keyword("bit-length")] = types.Int(bits)
				if v4 != nil {
					v4list.Insert(addrmap)
				} else {
					v6list.Insert(addrmap)
				}
				break

			}
		}
		facemap[types.Keyword("ipv4-addresses")] = v4list
		facemap[types.Keyword("ipv6-addresses")] = v6list
		netifs[i] = facemap
	}
	result[types.Keyword("network-interfaces")] = netifs
	return result, nil
}
コード例 #2
0
ファイル: hash-file.go プロジェクト: csm/vapor-modules
func (this HashFile) Exec(input types.Value) (out types.Value, err error) {
	var inputMap types.Map
	if i, ok := input.(types.Map); ok {
		inputMap = i
	} else {
		err = errors.New(fmt.Sprintf("expected a map as input, got a %T", input))
		return
	}

	var hashName string
	var hashNameElement = inputMap[types.Keyword("hash")]
	if hashNameElement == nil {
		hashName = "sha256"
	} else if h, ok := hashNameElement.(types.String); ok {
		hashName = string(h)
	} else if k, ok := hashNameElement.(types.Keyword); ok {
		hashName = string(k)
	} else if s, ok := hashNameElement.(types.Symbol); ok {
		hashName = string(s)
	} else {
		err = errors.New(":hash must be a string, keyword, or symbol if specified")
		return
	}
	hashName = strings.ToLower(hashName)

	var hash hash.Hash
	if hashName == "md5" {
		hash = md5.New()
	} else if hashName == "sha" || hashName == "sha1" || hashName == "sha-1" {
		hash = sha1.New()
	} else if hashName == "sha224" || hashName == "sha-224" {
		hash = sha256.New224()
	} else if hashName == "sha256" || hashName == "sha-256" {
		hash = sha256.New()
	} else if hashName == "sha384" || hashName == "sha-384" {
		hash = sha512.New384()
	} else if hashName == "sha512/224" || hashName == "sha-512/224" {
		hash = sha512.New512_224()
	} else if hashName == "sha512/256" || hashName == "sha-512/256" {
		hash = sha512.New512_256()
	} else if hashName == "sha512" || hashName == "sha-512" {
		hash = sha512.New()
	} else {
		err = errors.New(fmt.Sprint("unknown hash name: ", hashName))
		return
	}

	var fileName string
	var fileElem = inputMap[types.Keyword("file")]
	if fileElem == nil {
		err = errors.New(":file argument is required")
		return
	} else if f, ok := fileElem.(types.String); ok {
		fileName = string(f)
	} else {
		err = errors.New(":file argument must be a string")
		return
	}

	file, err := os.Open(fileName)
	if err != nil {
		return
	}
	hashOut := bufio.NewWriterSize(hash, hash.BlockSize())
	wrote, err := io.Copy(hashOut, file)
	hashOut.Flush()
	if err != nil {
		return
	}
	out = types.Map{
		types.Keyword("success"): types.Bool(true),
		types.Keyword("size"):    types.Int(wrote),
		types.Keyword("file"):    types.String(fileName),
		types.Keyword("hash"):    types.Keyword(hashName),
		types.Keyword("digest"):  types.String(fmt.Sprintf("%x", hash.Sum(nil))),
	}
	return
}