Improve "watching for ..." logging

This commit is contained in:
Bjørn Erik Pedersen 2015-11-23 16:32:06 +01:00
parent 098a0c819a
commit 831e936846
5 changed files with 59 additions and 38 deletions

View file

@ -150,14 +150,15 @@ func server(cmd *cobra.Command, args []string) {
// Watch runs its own server as part of the routine // Watch runs its own server as part of the routine
if serverWatch { if serverWatch {
watched := getDirList() watchDirs := getDirList()
workingDir := helpers.AbsPathify(viper.GetString("WorkingDir")) baseWatchDir := helpers.AbsPathify(viper.GetString("WorkingDir"))
for i, dir := range watched { for i, dir := range watchDirs {
watched[i], _ = helpers.GetRelativePath(dir, workingDir) watchDirs[i], _ = helpers.GetRelativePath(dir, baseWatchDir)
} }
unique := strings.Join(helpers.RemoveSubpaths(watched), ",")
jww.FEEDBACK.Printf("Watching for changes in %s/{%s}\n", workingDir, unique) rootWatchDirs := strings.Join(helpers.UniqueStrings(helpers.ExtractRootPaths(watchDirs)), ",")
jww.FEEDBACK.Printf("Watching for changes in %s/{%s}\n", baseWatchDir, rootWatchDirs)
err := NewWatcher(serverPort) err := NewWatcher(serverPort)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

View file

@ -90,6 +90,19 @@ func FirstUpper(s string) string {
return string(unicode.ToUpper(r)) + s[n:] return string(unicode.ToUpper(r)) + s[n:]
} }
// UniqueStrings returns a new slice with any duplicates removed.
func UniqueStrings(s []string) []string {
unique := make([]string, 0)
set := map[string]interface{}{}
for _, val := range s {
if _, ok := set[val]; !ok {
unique = append(unique, val)
set[val] = val
}
}
return unique
}
// ReaderToBytes takes an io.Reader argument, reads from it // ReaderToBytes takes an io.Reader argument, reads from it
// and returns bytes. // and returns bytes.
func ReaderToBytes(lines io.Reader) []byte { func ReaderToBytes(lines io.Reader) []byte {

View file

@ -164,6 +164,15 @@ func _BenchmarkReaderContains(b *testing.B) {
} }
} }
func TestUniqueStrings(t *testing.T) {
in := []string{"a", "b", "a", "b", "c", "", "a", "", "d"}
output := UniqueStrings(in)
expected := []string{"a", "b", "c", "", "d"}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Expected %#v, got %#v\n", expected, output)
}
}
func TestFindAvailablePort(t *testing.T) { func TestFindAvailablePort(t *testing.T) {
addr, err := FindAvailablePort() addr, err := FindAvailablePort()
assert.Nil(t, err) assert.Nil(t, err)

View file

@ -450,37 +450,27 @@ func prettifyPath(in string, b filepathPathBridge) string {
return b.Join(b.Dir(in), name, "index"+ext) return b.Join(b.Dir(in), name, "index"+ext)
} }
// RemoveSubpaths takes a list of paths and removes everything that // Extract the root paths from the supplied list of paths.
// contains another path in the list as a prefix. Ignores any empty // The resulting root path will not contain any file separators, but there
// strings. Used mostly for logging. // may be duplicates.
// // So "/content/section/" becomes "content"
// e.g. ["hello/world", "hello", "foo/bar", ""] -> ["hello", "foo/bar"] func ExtractRootPaths(paths []string) []string {
func RemoveSubpaths(paths []string) []string { r := make([]string, len(paths))
a := make([]string, 0) for i, p := range paths {
for _, cur := range paths { root := filepath.ToSlash(p)
// ignore trivial case if strings.Contains(root, "/") {
if cur == "" { sections := strings.Split(root, "/")
continue for _, section := range sections {
} if section != "" {
root = section
isDupe := false
for i, old := range a {
if strings.HasPrefix(cur, old) {
isDupe = true
break
} else if strings.HasPrefix(old, cur) {
a[i] = cur
isDupe = true
break break
} }
} }
if !isDupe {
a = append(a, cur)
} }
r[i] = root
} }
return r
return a
} }
// FindCWD returns the current working directory from where the Hugo // FindCWD returns the current working directory from where the Hugo

View file

@ -625,11 +625,19 @@ func TestPrettifyPath(t *testing.T) {
} }
func TestRemoveSubpaths(t *testing.T) { func TestExtractRootPaths(t *testing.T) {
got := RemoveSubpaths([]string{"hello", "hello/world", "foo/bar", ""}) tests := []struct {
expect := []string{"hello", "foo/bar"} input []string
if !reflect.DeepEqual(got, expect) { expected []string
t.Errorf("Expected %q but got %q", expect, got) }{{[]string{filepath.FromSlash("a/b"), filepath.FromSlash("a/b/c/"), "b",
filepath.FromSlash("/c/d"), filepath.FromSlash("d/"), filepath.FromSlash("//e//")},
[]string{"a", "a", "b", "c", "d", "e"}}}
for _, test := range tests {
output := ExtractRootPaths(test.input)
if !reflect.DeepEqual(output, test.expected) {
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
}
} }
} }