mirror of https://github.com/synctv-org/synctv
Feat: update sys notify
parent
2285bc86b9
commit
390fe2cdb7
@ -1,24 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/synctv-org/synctv/internal/bootstrap"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Init(cmd *cobra.Command, args []string) error {
|
|
||||||
bootstrap.InitSysNotify()
|
|
||||||
bootstrap.InitConfig()
|
|
||||||
bootstrap.InitLog()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var InitCmd = &cobra.Command{
|
|
||||||
Use: "init",
|
|
||||||
Short: "init and check config",
|
|
||||||
Long: `auto create config file or check config, and auto add new key and delete old key`,
|
|
||||||
RunE: Init,
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
RootCmd.AddCommand(InitCmd)
|
|
||||||
}
|
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type BootstrapConf func(*Bootstrap)
|
||||||
|
|
||||||
|
func WithContext(ctx context.Context) BootstrapConf {
|
||||||
|
return func(b *Bootstrap) {
|
||||||
|
b.ctx = ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithTask(f ...BootstrapFunc) BootstrapConf {
|
||||||
|
return func(b *Bootstrap) {
|
||||||
|
b.task = append(b.task, f...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Bootstrap struct {
|
||||||
|
task []BootstrapFunc
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(conf ...BootstrapConf) *Bootstrap {
|
||||||
|
b := &Bootstrap{}
|
||||||
|
for _, c := range conf {
|
||||||
|
c(b)
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
type BootstrapFunc func(context.Context) error
|
||||||
|
|
||||||
|
func (b *Bootstrap) Add(f ...BootstrapFunc) *Bootstrap {
|
||||||
|
b.task = append(b.task, f...)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bootstrap) Run() error {
|
||||||
|
for _, f := range b.task {
|
||||||
|
if err := f(b.ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@ -1,11 +1,16 @@
|
|||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import sysnotify "github.com/synctv-org/synctv/utils/sysNotify"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
sysnotify "github.com/synctv-org/synctv/utils/sysNotify"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
SysNotify *sysnotify.SysNotify
|
SysNotify sysnotify.SysNotify
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitSysNotify() {
|
func InitSysNotify(ctx context.Context) error {
|
||||||
SysNotify = sysnotify.New()
|
SysNotify.Init()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,69 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/synctv-org/synctv/internal/version"
|
||||||
|
sysnotify "github.com/synctv-org/synctv/utils/sysNotify"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitCheckUpdate(ctx context.Context) error {
|
||||||
|
v, err := version.NewVersionInfo()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("get version info error: %v", err)
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
t := time.NewTicker(time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
var (
|
||||||
|
need bool
|
||||||
|
latest string
|
||||||
|
url string
|
||||||
|
once sync.Once
|
||||||
|
)
|
||||||
|
SysNotify.RegisterSysNotifyTask(0, sysnotify.NewSysNotifyTask(
|
||||||
|
"check-update",
|
||||||
|
sysnotify.NotifyTypeEXIT,
|
||||||
|
func() error {
|
||||||
|
if need {
|
||||||
|
log.Infof("new version (%s) available: %s", latest, url)
|
||||||
|
log.Infof("run 'synctv self-update' to auto update")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
))
|
||||||
|
for range t.C {
|
||||||
|
l, err := v.CheckLatest(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("check update error: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
latest = l
|
||||||
|
b, err := v.NeedUpdate(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("check update error: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
need = b
|
||||||
|
if b {
|
||||||
|
u, err := v.LatestBinaryURL(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("check update error: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
url = u
|
||||||
|
}
|
||||||
|
once.Do(func() {
|
||||||
|
if b {
|
||||||
|
log.Infof("new version (%s) available: %s", latest, url)
|
||||||
|
log.Infof("run 'synctv self-update' to auto update")
|
||||||
|
}
|
||||||
|
t.Reset(time.Hour * 6)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue