@@ -19,8 +19,8 @@ package cmd
1919import (
2020 "fmt"
2121 "os"
22- "path/filepath"
2322 "strings"
23+ "time"
2424
2525 "github.com/pkg/errors"
2626 "github.com/spf13/cobra"
@@ -81,49 +81,48 @@ func monitorSpecDirs(args ...string) {
8181 done = make (chan error , 1 )
8282
8383 go func () {
84+ var (
85+ // don't print registry content more often than this
86+ oneSecond = 1 * time .Second
87+ timer * time.Timer
88+ )
89+
8490 if len (args ) == 0 {
8591 args = []string {"all" }
8692 }
8793
88- cdiPrintRegistry (args ... )
94+ timer = time .NewTimer (oneSecond )
95+ refresh := timer .C
96+
97+ defer func () {
98+ timer .Stop ()
99+ }()
89100
90101 for {
91102 select {
92- case evt , ok := <- dirWatch .Events :
103+ case _ , ok := <- dirWatch .Events :
93104 if ! ok {
94105 close (done )
95106 return
96107 }
97108
98- if evt .Op != fsnotify .Write && evt .Op != fsnotify .Remove {
99- continue
100- }
101-
102- name , ext := filepath .Base (evt .Name ), filepath .Ext (evt .Name )
103- if ext != ".json" && ext != ".yaml" {
104- fmt .Printf ("ignoring %s %q (not a CDI Spec)...\n " , evt .Op , evt .Name )
105- continue
106- }
107-
108- if name != "" && (name [0 ] == '.' || name [0 ] == '#' ) {
109- fmt .Printf ("ignoring probable editor temporary file %q...\n " , evt .Name )
110- continue
111- }
112-
113- fmt .Printf ("refreshing CDI registry (%s changed)...\n " , evt .Name )
114-
115- if err = registry .Refresh (); err != nil {
116- fmt .Printf (" => refresh failed: %v\n " , err )
117- } else {
118- fmt .Printf (" => refresh OK\n " )
119- cdiPrintRegistry (args ... )
109+ if refresh != nil {
110+ if ! timer .Stop () {
111+ <- timer .C
112+ }
120113 }
114+ timer .Reset (oneSecond )
115+ refresh = timer .C
121116
122117 case err , ok := <- dirWatch .Errors :
123118 if ok {
124119 done <- err
125120 }
126121 return
122+
123+ case _ = <- refresh :
124+ refresh = nil
125+ cdiPrintRegistry (args ... )
127126 }
128127 }
129128 }()
0 commit comments