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 }
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) }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }