From ffa76d2c7d9fd1907e20cd430f6a5641ef729390 Mon Sep 17 00:00:00 2001 From: Bai Yingjie Date: Wed, 16 Aug 2023 02:27:56 +0000 Subject: [PATCH] simplify -update URL format --- daemon.go | 10 +++++----- docs/daemon.md | 6 +++--- gshell_test.go | 2 +- gshellbuilder.go | 17 ++++++++++------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/daemon.go b/daemon.go index 34cd6a3..e1eb40d 100644 --- a/daemon.go +++ b/daemon.go @@ -575,8 +575,8 @@ var daemonKnownMsgs = []as.KnownMessage{ } type updater struct { - urlFmt string - lg *log.Logger + url string + lg *log.Logger } type gshellBin struct { @@ -594,7 +594,7 @@ func (msg tryUpdate) Handle(stream as.ContextStream) (reply interface{}) { updtr := stream.GetContext().(*updater) updtr.lg.Debugf("tryUpdate: %v", msg) - rev, err := httpOp.readFile(fmt.Sprintf(updtr.urlFmt, "rev")) + rev, err := httpOp.readFile(updtr.url + "/rev") if err != nil { return err } @@ -610,7 +610,7 @@ func (msg tryUpdate) Handle(stream as.ContextStream) (reply interface{}) { return ErrNoUpdate } - checksum, err := httpOp.readFile(fmt.Sprintf(updtr.urlFmt, "md5sum")) + checksum, err := httpOp.readFile(updtr.url + "/md5sum") if err != nil { return err } @@ -631,7 +631,7 @@ func (msg tryUpdate) Handle(stream as.ContextStream) (reply interface{}) { return fmt.Errorf("arch %s not supported", msg.arch) } - bin, err := httpOp.readFile(fmt.Sprintf(updtr.urlFmt, "gshell."+msg.arch)) + bin, err := httpOp.readFile(updtr.url + "/gshell." + msg.arch) if err != nil { return err } diff --git a/docs/daemon.md b/docs/daemon.md index 105737d..00e2702 100644 --- a/docs/daemon.md +++ b/docs/daemon.md @@ -116,7 +116,7 @@ chown -R nobody:nogroup . && chmod ugo+s bin/gshell exit # start gshell daemon at log level info -bin/gshell -loglevel info daemon -wd rootregistry -registry Your_Server_IP:11985 -bcast 9923 -root -repo github.com/godevsig/grepo/master -update http://10.10.10.10:8088/gshell/release/latest/%s & +bin/gshell -loglevel info daemon -wd rootregistry -registry Your_Server_IP:11985 -bcast 9923 -root -repo github.com/godevsig/grepo/master -update http://10.10.10.10:8088/gshell/release/latest/ & # after gshell daemon started, check code repo contents bin/gshell repo ls @@ -186,7 +186,7 @@ For example, to autoupdate gshell binary from official github repo, here we don' we just use github download page: ``` -bin/gshell -loglevel info daemon -wd rootregistry -registry 10.10.10.10:11985 -bcast 9923 -root -repo github.com/godevsig/grepo/master -update https://github.com/godevsig/gshellos/releases/latest/download/%s & +bin/gshell -loglevel info daemon -wd rootregistry -registry 10.10.10.10:11985 -bcast 9923 -root -repo github.com/godevsig/grepo/master -update https://github.com/godevsig/gshellos/releases/latest/download/ & ``` Another example is autoupdating from private http file server: @@ -194,7 +194,7 @@ Another example is autoupdating from private http file server: ```shell cd /path/to/gshell # start root gshell daemon -bin/gshell -loglevel info daemon -wd rootregistry -registry 10.10.10.10:11985 -bcast 9923 -root -repo github.com/godevsig/grepo/master -update http://10.10.10.10:8088/gshell/release/latest/%s & +bin/gshell -loglevel info daemon -wd rootregistry -registry 10.10.10.10:11985 -bcast 9923 -root -repo github.com/godevsig/grepo/master -update http://10.10.10.10:8088/gshell/release/latest/ & # start file server on the same node(10.10.10.10) of root gshell daemon bin/gshell run util/fileserver/cmd/fileserver.go -dir /path/contains/gshell/release ``` diff --git a/gshell_test.go b/gshell_test.go index c74cba3..770a9c8 100644 --- a/gshell_test.go +++ b/gshell_test.go @@ -770,7 +770,7 @@ func TestMain(m *testing.M) { cmdstr := "-test.run ^TestRunMain$ -test.coverprofile=.test/l2_gshelld" + randID() + ".cov -- " cmdstr += "-loglevel debug daemon -wd .working -registry 127.0.0.1:11985 -bcast 9923 " cmdstr += "-root -repo . " - cmdstr += "-update http://127.0.0.1:9001/%s" + cmdstr += "-update http://127.0.0.1:9001" go func() { output, _ := exec.Command("gshell.tester", strings.Split(cmdstr, " ")...).CombinedOutput() fmt.Println(string(output)) diff --git a/gshellbuilder.go b/gshellbuilder.go index 0d66ac7..8ddc775 100644 --- a/gshellbuilder.go +++ b/gshellbuilder.go @@ -140,17 +140,19 @@ func addDaemonCmd() { if len(*lanBroadcastPort) == 0 { scope &= ^as.ScopeLAN // not ScopeLAN } - if len(*updateURL) != 0 || *rootRegistry { + updateURL := *updateURL + if len(updateURL) != 0 || *rootRegistry { if scope&as.ScopeWAN != as.ScopeWAN { return errors.New("root registry address not set") } } + codeRepo := *codeRepo crs := &codeRepoSvc{} - if len(*codeRepo) != 0 { - fi, err := os.Stat(*codeRepo) + if len(codeRepo) != 0 { + fi, err := os.Stat(codeRepo) if err != nil || !fi.Mode().IsDir() { - crs.httpRepoInfo = strings.Split(*codeRepo, "/") + crs.httpRepoInfo = strings.Split(codeRepo, "/") if len(crs.httpRepoInfo) != 4 { return errors.New("wrong repo format") } @@ -158,7 +160,7 @@ func addDaemonCmd() { return errors.New("http feature not enabled, check build tags") } } else { - crs.localRepoPath, _ = filepath.Abs(*codeRepo) + crs.localRepoPath, _ = filepath.Abs(codeRepo) } } @@ -202,11 +204,12 @@ func addDaemonCmd() { s.EnableRootRegistry() s.EnableIPObserver() - if len(*updateURL) != 0 { + if len(updateURL) != 0 { if httpOp == nil { return errors.New("http feature not enabled, check build tags") } - updtr := &updater{urlFmt: *updateURL, lg: lg} + updateURL = strings.TrimSuffix(updateURL, "/") + updtr := &updater{url: updateURL, lg: lg} if err := s.Publish("updater", updaterKnownMsgs, as.OnNewStreamFunc(func(ctx as.Context) { ctx.SetContext(updtr) }),