Exemplo n.º 1
0
func Trim(in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return "", fmt.Errorf("trim must be given a string")
	}
	return strings.Trim(in_, " \n"), nil
}
Exemplo n.º 2
0
func Match(pattern string, in interface{}) (bool, error) {
	str, _, ok := sigil.String(in)
	if !ok {
		return false, fmt.Errorf("match must be given a string")
	}
	return path.Match(pattern, str)
}
Exemplo n.º 3
0
func Replace(old, new string, in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return "", fmt.Errorf("replace must be given a string")
	}
	return strings.Replace(in_, old, new, -1), nil
}
Exemplo n.º 4
0
func Upper(in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return "", fmt.Errorf("upper must be given a string")
	}
	return strings.ToUpper(in_), nil
}
Exemplo n.º 5
0
func Capitalize(in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return "", fmt.Errorf("capitalize must be given a string")
	}
	return strings.Title(in_), nil
}
Exemplo n.º 6
0
func HttpGet(in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return "", fmt.Errorf("httpget must be given a string")
	}
	resp, err := http.Get(in_)
	if err != nil {
		return "", err
	}
	return sigil.NamedReader{resp.Body, "<" + in_ + ">"}, nil
}
Exemplo n.º 7
0
func Exists(in interface{}) (bool, error) {
	filename, _, ok := sigil.String(in)
	if !ok {
		return false, fmt.Errorf("exists must be given a string")
	}
	_, err := sigil.LookPath(filename)
	if err != nil {
		return false, nil
	}
	return true, nil
}
Exemplo n.º 8
0
func Split(delim string, in interface{}) ([]interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return nil, fmt.Errorf("split must be given a string")
	}
	var elements []interface{}
	for _, v := range strings.Split(in_, delim) {
		elements = append(elements, v)
	}
	return elements, nil
}
Exemplo n.º 9
0
func Dir(in interface{}) ([]interface{}, error) {
	path, _, ok := sigil.String(in)
	if !ok {
		return nil, fmt.Errorf("dir must be given a string")
	}
	var files []interface{}
	dir, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}
	for _, fi := range dir {
		files = append(files, fi.Name())
	}
	return files, nil
}
Exemplo n.º 10
0
func Substring(slice string, in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return nil, fmt.Errorf("substr must be given a string")
	}
	s := strings.Split(slice, ":")
	start, err := strconv.Atoi(s[0])
	if err != nil {
		start = 0
	}
	end, err := strconv.Atoi(s[1])
	if err != nil {
		return nil, fmt.Errorf("substr needs slice expression as 'start:end' ")
	}
	return in_[start:end], nil
}
Exemplo n.º 11
0
func Dirs(in interface{}) ([]interface{}, error) {
	path, _, ok := sigil.String(in)
	if !ok {
		return nil, fmt.Errorf("dirs must be given a string")
	}
	var dirs []interface{}
	dir, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}
	for _, fi := range dir {
		if fi.IsDir() {
			dirs = append(dirs, fi.Name())
		}
	}
	return dirs, nil
}
Exemplo n.º 12
0
func Shell(in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return "", fmt.Errorf("sh must be given a string")
	}
	args, err := shlex.Split(in_)
	if err != nil {
		return "", err
	}
	path, err := exec.LookPath(args[0])
	if err != nil {
		return "", err
	}
	out, err := exec.Command(path, args[1:]...).Output()
	if err != nil {
		return "", err
	}
	return string(out), nil
}
Exemplo n.º 13
0
func Indent(indent string, in interface{}) (interface{}, error) {
	in_, _, ok := sigil.String(in)
	if !ok {
		return "", fmt.Errorf("indent must be given a string")
	}
	var indented []string
	lines := strings.Split(in_, "\n")
	indented = append(indented, lines[0])
	if len(lines) > 1 {
		for _, line := range lines[1:] {
			if line != "" {
				indented = append(indented, indent+line)
			} else {
				indented = append(indented, line)
			}
		}
	}
	return strings.Join(indented, "\n"), nil
}
Exemplo n.º 14
0
func read(file interface{}) ([]byte, error) {
	reader, ok := file.(sigil.NamedReader)
	if ok {
		return ioutil.ReadAll(reader)
	}
	path, _, ok := sigil.String(file)
	if !ok {
		return []byte{}, fmt.Errorf("file must be stream or path string")
	}
	filepath, err := sigil.LookPath(path)
	if err != nil {
		return []byte{}, err
	}
	data, err := ioutil.ReadFile(filepath)
	if err != nil {
		return []byte{}, err
	}
	return data, nil
}