@@ -2,6 +2,7 @@ package main
22
33import (
44 "bufio"
5+ "bytes"
56 "flag"
67 "fmt"
78 "net"
@@ -10,6 +11,7 @@ import (
1011 "path/filepath"
1112 "regexp"
1213 "strings"
14+ "text/template"
1315 "time"
1416
1517 "github.com/alessio/shellescape"
@@ -33,7 +35,7 @@ var f = flag.NewFlagSet("lcode-hub@"+VERSION, flag.ExitOnError)
3335
3436func init () {
3537 f .StringVar (& args .addr , "addr" , "127.0.0.1:4349" , "local-hub listen addr" )
36- f .StringVar (& args .hello , "hello" , "webdav://%s. lo.shynome.com:4349%s " , "" )
38+ f .StringVar (& args .hello , "hello" , "webdav://{{.host}}. lo.shynome.com:4349{{.path}} " , "" )
3739 f .StringVar (& args .localdomain , "localdomain" , ".lo.shynome.com" , "" )
3840}
3941
@@ -56,6 +58,7 @@ func main() {
5658 bash .VERSION = VERSION
5759 go func () {
5860 var format = regexp .MustCompile (`^\d+-(.+)-(\d+)$` )
61+ helloTpl := To1 (template .New ("hello" ).Parse (args .hello + "\n " ))
5962 for client := range bash .Connected () {
6063 go func (c * webdav.Client ) {
6164 fmt .Println ("client connected" , c .ID )
@@ -66,21 +69,26 @@ func main() {
6669 }
6770 id := fmt .Sprintf ("%s-%s" , f [2 ], f [1 ])
6871 noEditTargets := true
72+ var output bytes.Buffer
6973 for _ , t := range c .Targets () {
70- var hello string
7174 switch {
7275 case strings .HasPrefix (t , "/dev/null" ):
7376 t = strings .TrimPrefix (t , "/dev/null" )
74- hello = fmt .Sprintf ( "this target is not exists: %s" , t )
77+ fmt .Fprintf ( & output , "this target is not exists: %s" , t )
7578 case strings .HasPrefix (t , "/dev/err" ):
7679 t = strings .TrimPrefix (t , "/dev/err" )
77- hello = fmt .Sprintf ( "this target cannot be opened: %s" , t )
80+ fmt .Fprintf ( & output , "this target cannot be opened: %s" , t )
7881 default :
7982 noEditTargets = false
80- hello = fmt .Sprintf (args .hello , id , t )
83+ helloTpl .Execute (& output , map [string ]string {
84+ "host" : id ,
85+ "path" : t ,
86+ })
8187 }
82- c .Exec (fmt .Sprintf (">&2 echo lo: %s" , shellescape .Quote (hello )))
8388 }
89+ hello := string (output .Bytes ())
90+ hello = strings .ReplaceAll (hello , "\n " , "\n lo: " )
91+ c .Exec (fmt .Sprintf (">&2 echo lo: %s" , shellescape .Quote (hello )))
8492 if noEditTargets {
8593 c .Exec (fmt .Sprintf (">&2 echo lo: no editable targets, exit" ))
8694 c .Close ()
0 commit comments