mirror of
https://github.com/rwxrob/bonzai
synced 2024-11-14 18:12:59 +00:00
Move util into main or other packages
This commit is contained in:
parent
47f7819e4a
commit
24c788bc8d
@ -1,20 +1,7 @@
|
||||
/*
|
||||
Copyright 2021 Robert S. Muhlestein.
|
||||
// Copyright 2022 Robert S. Muhlestein.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package util
|
||||
package bonzai
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,20 +1,7 @@
|
||||
/*
|
||||
Copyright 2021 Robert S. Muhlestein.
|
||||
// Copyright 2022 Robert S. Muhlestein.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package util
|
||||
package bonzai
|
||||
|
||||
import (
|
||||
"os"
|
@ -1,4 +1,4 @@
|
||||
package util
|
||||
package bonzai
|
||||
|
||||
import (
|
||||
"log"
|
@ -1,11 +1,11 @@
|
||||
package util_test
|
||||
package bonzai_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
ht "net/http/httptest"
|
||||
|
||||
"github.com/rwxrob/bonzai/util"
|
||||
"github.com/rwxrob/bonzai"
|
||||
)
|
||||
|
||||
func ExampleCompareUpdated() {
|
||||
@ -31,10 +31,10 @@ func ExampleCompareUpdated() {
|
||||
same := ht.NewServer(handler)
|
||||
defer same.Close()
|
||||
|
||||
fmt.Println(util.CompareUpdated(20220322080542, older.URL))
|
||||
fmt.Println(util.CompareUpdated(20220322080542, newer.URL))
|
||||
fmt.Println(util.CompareUpdated(20220322080542, same.URL))
|
||||
fmt.Println(util.CompareUpdated(20220322080542, "foobar"))
|
||||
fmt.Println(bonzai.CompareUpdated(20220322080542, older.URL))
|
||||
fmt.Println(bonzai.CompareUpdated(20220322080542, newer.URL))
|
||||
fmt.Println(bonzai.CompareUpdated(20220322080542, same.URL))
|
||||
fmt.Println(bonzai.CompareUpdated(20220322080542, "foobar"))
|
||||
|
||||
// Output:
|
||||
// -1
|
||||
@ -66,10 +66,10 @@ func ExampleCompareVersions() {
|
||||
same := ht.NewServer(handler)
|
||||
defer same.Close()
|
||||
|
||||
fmt.Println(util.CompareVersions(`v0.0.2`, older.URL))
|
||||
fmt.Println(util.CompareVersions(`v0.0.2`, newer.URL))
|
||||
fmt.Println(util.CompareVersions(`v0.0.2`, same.URL))
|
||||
fmt.Println(util.CompareVersions(`v0.0.2`, "foobar"))
|
||||
fmt.Println(bonzai.CompareVersions(`v0.0.2`, older.URL))
|
||||
fmt.Println(bonzai.CompareVersions(`v0.0.2`, newer.URL))
|
||||
fmt.Println(bonzai.CompareVersions(`v0.0.2`, same.URL))
|
||||
fmt.Println(bonzai.CompareVersions(`v0.0.2`, "foobar"))
|
||||
|
||||
// Output:
|
||||
// 1
|
@ -1,34 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/rwxrob/fn/maps"
|
||||
)
|
||||
|
||||
// Files returns a slice of strings matching the names of the files
|
||||
// within the given directory adding a slash to the end of any
|
||||
// directories and escaping any spaces by adding backslash. Note that
|
||||
// this (and all functions of the bonzai package) assume forward slash
|
||||
// path separators because no path argument should ever be passed to any
|
||||
// bonzai command or high-level library that does not use forward slash
|
||||
// paths. Commands should always use the comp.Files completer instead of
|
||||
// host shell completion.
|
||||
func Files(dir string) []string {
|
||||
if dir == "" {
|
||||
dir = "."
|
||||
}
|
||||
files := []string{}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return files
|
||||
}
|
||||
names := maps.MarkDirs(entries)
|
||||
if dir == "." {
|
||||
return names
|
||||
}
|
||||
if dir[len(dir)-1] != '/' {
|
||||
dir += "/"
|
||||
}
|
||||
return maps.EscSpace(maps.Prefix(names, dir))
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package util_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/rwxrob/bonzai/util"
|
||||
"github.com/rwxrob/fn/each"
|
||||
)
|
||||
|
||||
func ExampleFiles() {
|
||||
each.Println(util.Files("testdata/files"))
|
||||
// Output:
|
||||
// testdata/files/bar
|
||||
// testdata/files/blah
|
||||
// testdata/files/dir1/
|
||||
// testdata/files/foo
|
||||
// testdata/files/other
|
||||
// testdata/files/some
|
||||
}
|
||||
|
||||
func ExampleFiles_spaces() {
|
||||
each.Println(util.Files("testdata/files/dir1"))
|
||||
// Output:
|
||||
// testdata/files/dir1/some\ thing
|
||||
}
|
||||
|
||||
func ExampleFiles_empty() {
|
||||
os.Chdir("testdata/files")
|
||||
defer os.Chdir("../..")
|
||||
each.Println(util.Files(""))
|
||||
// Output:
|
||||
// bar
|
||||
// blah
|
||||
// dir1/
|
||||
// foo
|
||||
// other
|
||||
// some
|
||||
}
|
||||
|
||||
func ExampleFiles_not_Directory() {
|
||||
fmt.Println(util.Files("none"))
|
||||
// Output:
|
||||
// []
|
||||
}
|
17
util/func.go
17
util/func.go
@ -1,17 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FuncName makes a best effort attempt to return the string name of the
|
||||
// passed function. Anonymous functions are named "funcN" where N is the
|
||||
// order of appearance within the current scope. Note that this function
|
||||
// will panic if not passed a function.
|
||||
func FuncName(i any) string {
|
||||
p := runtime.FuncForPC(reflect.ValueOf(i).Pointer())
|
||||
n := strings.Split(p.Name(), `.`)
|
||||
return n[len(n)-1]
|
||||
}
|
@ -1 +0,0 @@
|
||||
package util_test
|
@ -1,45 +0,0 @@
|
||||
package util
|
||||
|
||||
type stacked struct {
|
||||
val any
|
||||
left *stacked
|
||||
right *stacked
|
||||
}
|
||||
|
||||
// Stack implements a simple stack data structure using a linked list.
|
||||
type Stack struct {
|
||||
first *stacked
|
||||
last *stacked
|
||||
}
|
||||
|
||||
// Push will add an item of any type to the stack in front of the
|
||||
// others.
|
||||
func (s *Stack) Push(it any) {
|
||||
n := new(stacked)
|
||||
n.val = it
|
||||
if s.first == nil {
|
||||
s.first = n
|
||||
s.last = n
|
||||
return
|
||||
}
|
||||
s.last.right = n
|
||||
n.left = s.last
|
||||
s.last = n
|
||||
}
|
||||
|
||||
// Pop will remove the most recently added item and return it.
|
||||
func (s *Stack) Pop() any {
|
||||
if s.last == nil {
|
||||
return nil
|
||||
}
|
||||
popped := s.last.val
|
||||
s.last = s.last.left
|
||||
return popped
|
||||
}
|
||||
|
||||
func (s *Stack) Peek() any {
|
||||
if s.last == nil {
|
||||
return nil
|
||||
}
|
||||
return s.last.val
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package util_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/rwxrob/bonzai/util"
|
||||
)
|
||||
|
||||
func ExampleStack() {
|
||||
s := new(util.Stack)
|
||||
s.Push("some")
|
||||
s.Push("another")
|
||||
fmt.Println(s.Peek())
|
||||
fmt.Println(s.Pop())
|
||||
fmt.Println(s.Peek())
|
||||
s.Push(1)
|
||||
fmt.Println(s.Peek())
|
||||
s.Pop()
|
||||
s.Pop()
|
||||
fmt.Println(s.Peek())
|
||||
// Output:
|
||||
// another
|
||||
// another
|
||||
// some
|
||||
// 1
|
||||
// <nil>
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Lines transforms the input into a string and then divides that string
|
||||
// up into lines (\r?\n) suitable for functional map operations.
|
||||
func Lines[T any](in T) []string {
|
||||
buf := fmt.Sprintf("%v", in)
|
||||
lines := []string{}
|
||||
scan := bufio.NewScanner(strings.NewReader(buf))
|
||||
for scan.Scan() {
|
||||
lines = append(lines, scan.Text())
|
||||
}
|
||||
return lines
|
||||
}
|
@ -1 +0,0 @@
|
||||
package util_test
|
0
util/testdata/files/bar
vendored
0
util/testdata/files/bar
vendored
0
util/testdata/files/blah
vendored
0
util/testdata/files/blah
vendored
0
util/testdata/files/dir1/some thing
vendored
0
util/testdata/files/dir1/some thing
vendored
0
util/testdata/files/foo
vendored
0
util/testdata/files/foo
vendored
0
util/testdata/files/other
vendored
0
util/testdata/files/other
vendored
0
util/testdata/files/some
vendored
0
util/testdata/files/some
vendored
Loading…
Reference in New Issue
Block a user