Skip to content

Commit

Permalink
Merge pull request #6 from gliderlabs/master
Browse files Browse the repository at this point in the history
v0.3.2
  • Loading branch information
progrium committed Aug 5, 2015
2 parents f09c209 + c31d5a2 commit 0011e45
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NAME=sigil
ARCH=$(shell uname -m)
ORG=gliderlabs
VERSION=0.3.1
VERSION=0.3.2

build:
mkdir -p build/Linux && GOOS=linux CGO_ENABLED=0 go build -a \
Expand Down
82 changes: 80 additions & 2 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func init() {
"render": Render,
"exists": Exists,
"dir": Dir,
"dirs": Dirs,
"files": Files,
"uniq": Uniq,
"drop": Drop,
"append": Append,
"stdin": Stdin,
})
}
Expand Down Expand Up @@ -262,8 +267,8 @@ func Exists(filename string) bool {
return true
}

func Dir(path string) ([]string, error) {
var files []string
func Dir(path string) ([]interface{}, error) {
var files []interface{}
dir, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
Expand All @@ -274,6 +279,48 @@ func Dir(path string) ([]string, error) {
return files, nil
}

func Dirs(path string) ([]interface{}, error) {
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 Files(path string) ([]interface{}, error) {
var files []interface{}
dir, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, fi := range dir {
if !fi.IsDir() {
files = append(files, fi.Name())
}
}
return files, nil
}

func Uniq(in ...[]interface{}) []interface{} {
m := make(map[interface{}]bool)
for i := range in {
for _, v := range in[i] {
m[v] = true
}
}
var uniq []interface{}
for k, _ := range m {
uniq = append(uniq, k)
}
return uniq
}

type stdinStr string

func Stdin() (stdinStr, error) {
Expand All @@ -283,3 +330,34 @@ func Stdin() (stdinStr, error) {
}
return stdinStr(data), nil
}

func Append(item interface{}, items []interface{}) []interface{} {
return append(items, item)
}

func Drop(item interface{}, items []interface{}) ([]interface{}, error) {
var out []interface{}
pattern, isstr := item.(string)
if isstr {
for i := range items {
str, ok := items[i].(string)
if !ok {
return nil, fmt.Errorf("all elements must be a string to drop a string")
}
match, err := path.Match(pattern, str)
if err != nil {
return nil, fmt.Errorf("bad pattern: %s", pattern)
}
if !match {
out = append(out, items[i])
}
}
return out, nil
}
for i := range items {
if item != items[i] {
out = append(out, items[i])
}
}
return out, nil
}

0 comments on commit 0011e45

Please sign in to comment.