Skip to content

Commit

Permalink
Remove caster dependency (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored Mar 13, 2023
1 parent f7c9679 commit 3a461c5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/guiguan/caster v0.0.0-20191104051807-3736c4464f38
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/lestrrat-go/jwx v1.2.25
github.com/logrusorgru/aurora v2.0.3+incompatible
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/guiguan/caster v0.0.0-20191104051807-3736c4464f38 h1:oWETJozNAt29o9b03jPJ8mjQTk8XklRXEZiXBECoNpg=
github.com/guiguan/caster v0.0.0-20191104051807-3736c4464f38/go.mod h1:giU/iWwQIOg/ND1ecR8raoyROxojrXL9osppnuI7MRY=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
Expand Down
27 changes: 12 additions & 15 deletions internal/cli/universal_login_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/auth0/go-auth0/management"
"github.com/fsnotify/fsnotify"
"github.com/guiguan/caster"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -346,14 +345,14 @@ func previewTemplate(ctx context.Context, data *TemplateData) error {
}
defer listener.Close()

broadcaster, err := broadcastTemplateChanges(ctx, data.Filename)
changesChan, err := broadcastTemplateChanges(ctx, data.Filename)
if err != nil {
return err
}

requestTimeout := 10 * time.Minute
server := &http.Server{
Handler: buildRoutes(requestTimeout, data, broadcaster),
Handler: buildRoutes(requestTimeout, data, changesChan),
ReadTimeout: requestTimeout + time.Minute,
WriteTimeout: requestTimeout + time.Minute,
}
Expand Down Expand Up @@ -384,16 +383,13 @@ func previewTemplate(ctx context.Context, data *TemplateData) error {
func buildRoutes(
requestTimeout time.Duration,
data *TemplateData,
broadcaster *caster.Caster,
changesChan chan bool,
) *http.ServeMux {
router := http.NewServeMux()

router.HandleFunc("/dynamic/events", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

changes, _ := broadcaster.Sub(ctx, 1)
defer broadcaster.Unsub(changes)

writeStatus := func(w http.ResponseWriter, code int) {
msg := fmt.Sprintf("%d - %s", code, http.StatusText(http.StatusGone))
http.Error(w, msg, code)
Expand All @@ -404,7 +400,7 @@ func buildRoutes(
writeStatus(w, http.StatusGone)
case <-time.After(requestTimeout):
writeStatus(w, http.StatusRequestTimeout)
case <-changes:
case <-changesChan:
writeStatus(w, http.StatusOK)
}
})
Expand All @@ -431,8 +427,8 @@ func buildRoutes(
return router
}

func broadcastTemplateChanges(ctx context.Context, filename string) (*caster.Caster, error) {
publisher := caster.New(ctx)
func broadcastTemplateChanges(ctx context.Context, filename string) (chan bool, error) {
changesChan := make(chan bool)

watcher, err := fsnotify.NewWatcher()
if err != nil {
Expand All @@ -442,12 +438,13 @@ func broadcastTemplateChanges(ctx context.Context, filename string) (*caster.Cas
go func() {
for {
select {
case _, ok := <-watcher.Events:
case event, ok := <-watcher.Events:
if !ok {
return
}
publisher.Pub(true)

if event.Op&fsnotify.Write == fsnotify.Write {
changesChan <- true
}
case _, ok := <-watcher.Errors:
if !ok {
return
Expand All @@ -459,12 +456,12 @@ func broadcastTemplateChanges(ctx context.Context, filename string) (*caster.Cas
go func() {
<-ctx.Done()
watcher.Close()
publisher.Close()
close(changesChan)
}()

if err := watcher.Add(filepath.Dir(filename)); err != nil {
return nil, err
}

return publisher, nil
return changesChan, nil
}

0 comments on commit 3a461c5

Please sign in to comment.