From 3a461c5b9d98bd0747859344add9de7dfcb5fa37 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Mon, 13 Mar 2023 13:53:42 +0100 Subject: [PATCH] Remove caster dependency (#668) --- go.mod | 1 - go.sum | 2 -- internal/cli/universal_login_templates.go | 27 ++++++++++------------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index a3a498616..a596b5ea1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 8b807eb14..3ec5f097e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/cli/universal_login_templates.go b/internal/cli/universal_login_templates.go index 2cad42fd0..8ced5c5f5 100644 --- a/internal/cli/universal_login_templates.go +++ b/internal/cli/universal_login_templates.go @@ -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" @@ -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, } @@ -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) @@ -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) } }) @@ -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 { @@ -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 @@ -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 }