From f5a074a2655e168361e42377ce066c51603f5016 Mon Sep 17 00:00:00 2001 From: DisposaBoy Date: Mon, 9 Mar 2020 06:58:30 +0000 Subject: [PATCH] don' report new updates if the client tag is greater than the server motd tag re: https://github.com/DisposaBoy/GoSublime/issues/964 --- extension-example/extension-example.go | 2 +- mg/motd.go | 27 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/extension-example/extension-example.go b/extension-example/extension-example.go index 6184b46..6135816 100644 --- a/extension-example/extension-example.go +++ b/extension-example/extension-example.go @@ -17,7 +17,7 @@ func Margo(m mg.Args) { m.Use( // MOTD keeps you updated about new versions and important announcements // - // It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD` + // It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD (check for updates)` // // Interval can be set in order to enable automatic update fetching. // diff --git a/mg/motd.go b/mg/motd.go index d7a2f10..8463d59 100644 --- a/mg/motd.go +++ b/mg/motd.go @@ -42,7 +42,7 @@ type motdState struct { // MOTD keeps you updated about new versions and important announcements // -// It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD` +// It adds a new command `motd.sync` available via the UserCmd palette as `Sync MOTD (check for updates)` // // Interval can be set in order to enable automatic update fetching. // @@ -96,7 +96,7 @@ func (m *MOTD) Reduce(mx *Ctx) *State { case RunCmd: st = st.AddBuiltinCmds(BuiltinCmd{Name: "motd.sync", Run: m.motdSyncCmd}) case QueryUserCmds: - st = st.AddUserCmds(UserCmd{Title: "Sync MOTD", Name: "motd.sync"}) + st = st.AddUserCmds(UserCmd{Title: "Sync MOTD (check for updates)", Name: "motd.sync"}) case motdAct: m.msg = act.msg } @@ -179,14 +179,35 @@ func (m *MOTD) sync(mx *Ctx) error { return nil } +func (_ *MOTD) fmtTag(s string) (string, error) { + var y, m, d, n int + scanned, err := fmt.Sscanf(s, "%02d.%02d.%02d-%d", &y, &m, &d, &n) + if scanned < 4 { + _, err = fmt.Sscanf(s, "%02d.%02d.%02d", &y, &m, &d) + } + if err != nil { + return "", err + } + if n <= 0 { + n = 1 + } + return fmt.Sprintf("%02d.%02d.%02d-%d", y, m, d, n), nil +} + func (m *MOTD) dispatchMsg(mx *Ctx, ms motdState) { res := ms.Result act := motdAct{} ctag := mx.Editor.Client.Tag + srvTag, srvTagErr := m.fmtTag(res.Tag) + cliTag, cliTagErr := m.fmtTag(ctag) switch { case ctag == "": mx.Log.Println("motd: client tag is undefined; you might need to restart the editor") - case res.Tag != ctag: + case srvTagErr != nil: + mx.Log.Println("motd: cannot parse ser`ver tag:", srvTagErr) + case cliTagErr != nil: + mx.Log.Println("motd: cannot parse client tag:", cliTagErr) + case cliTag < srvTag: act.msg = res.Message } mx.Store.Dispatch(act)