From 855eaf7a22db1be8d189d81135e10f12df070118 Mon Sep 17 00:00:00 2001 From: duruyao Date: Thu, 26 May 2022 12:06:49 +0800 Subject: [PATCH] feat: show running status with ascii logo --- args.go | 48 ++++++++++++++++++++++++++++-------------------- main.go | 4 ++-- util.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 23 deletions(-) diff --git a/args.go b/args.go index 042c0ec..beaf07d 100644 --- a/args.go +++ b/args.go @@ -6,6 +6,7 @@ import ( "flag" "log" "os" + "path/filepath" "sync" "text/template" ) @@ -14,7 +15,8 @@ const ( DefaultHost = `localhost:3927` DefaultScheme = `http` DefaultUrlPrefix = `/` - UsageTmpl = `Usage: {{.Exec}} [OPTIONS] + UsageTmpl = `{{.Logo}} +Usage: {{.Exec}} [OPTIONS] GoShare shares file and directory by HTTP or FTP protocol @@ -27,12 +29,12 @@ Options: -v, --version Print version information and quit Examples: - {{.Exec}} -host example.io -path /opt/share0/releases/ - {{.Exec}} -host {{.Host}} -path /opt/share0/releases/ - {{.Exec}} --host {{.Host}} --url-prefix /releases/ --path /opt/share0/releases/ - {{.Exec}} --host={{.Host}} --url-prefix=/releases/ --path=/opt/share0/releases/ + {{.Exec}} -host example.io -path {{.ExamplePath}} + {{.Exec}} -host {{.Host}} -path {{.ExamplePath}} + {{.Exec}} --host {{.Host}} --url-prefix /{{.ExampleUrlPrefix}} --path {{.ExamplePath}} + {{.Exec}} --host={{.Host}} --url-prefix=/{{.ExampleUrlPrefix}} --path={{.ExamplePath}} -See more about {{.App}} at {{.AppLink}} +See more about {{.App}} at {{.Link}} ` ) @@ -126,21 +128,27 @@ func (a *Argument) Usage() string { a.Parse() tmpl := template.Must(template.New("usage tmpl").Parse(UsageTmpl)) data := struct { - App string - AppLink string - Exec string - Host string - Path string - Scheme string - UrlPrefix string + Logo string + Exec string + Host string + Path string + Scheme string + UrlPrefix string + ExamplePath string + ExampleUrlPrefix string + App string + Link string }{ - App: App, - AppLink: AppLink, - Exec: os.Args[0], - Host: DefaultHost, - Path: DefaultPath, - Scheme: DefaultScheme, - UrlPrefix: DefaultUrlPrefix, + Logo: Logo, + Exec: os.Args[0], + Host: DefaultHost, + Path: DefaultPath, + Scheme: DefaultScheme, + UrlPrefix: DefaultUrlPrefix, + ExamplePath: CurrentDirMust(), + ExampleUrlPrefix: filepath.Base(CurrentDirMust()), + App: App, + Link: Link, } buf := bytes.Buffer{} if err := tmpl.Execute(&buf, data); err != nil { diff --git a/main.go b/main.go index c6567cb..9043812 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { info, err := os.Stat(path) if os.IsNotExist(err) { - fmt.Println("Error: No such file or directory: " + path) + fmt.Println(err.Error()) fmt.Println(arg.Usage()) return } @@ -39,6 +39,6 @@ func main() { dir, file = filepath.Dir(path), filepath.Base(path) } go StartHttpFileService(host, dir, urlPrefix) - fmt.Printf("Share files by the URL %s://%s%s%s\n", scheme, host, urlPrefix, file) + fmt.Println(RunningStatus(dir, host, scheme, urlPrefix, file)) <-quit } diff --git a/util.go b/util.go index 46ceb4f..be2e841 100644 --- a/util.go +++ b/util.go @@ -54,10 +54,19 @@ func CurrentDirMust() string { const ( App = `GoShare` - AppLink = `https://github.com/duruyao/goshare` + Link = `https://github.com/duruyao/goshare` Version = `1.0.0` ReleaseDate = `2022-05-23` VersionSerialTmpl = `{{.App}} {{.Version}} ({{.ReleaseDate}})` + Logo = ` + _____ _____ _ + / ____| / ____| | + | | __ ___| (___ | |__ __ _ _ __ ___ + | | |_ |/ _ \\___ \| '_ \ / _' | '__/ _ \ + | |__| | (_) |___) | | | | (_| | | | __/ + \_____|\___/_____/|_| |_|\__,_|_| \___| + +` ) // VersionSerial returns version serial. @@ -86,3 +95,38 @@ func FixedUrlPrefix(urlPrefix string) string { } return AbsPathMust("/"+urlPrefix) + "/" } + +const ( + RunningStatusTmpl = `{{.Logo}} +{{.App}} is handing directory '{{.Dir}}' and listening on '{{.Host}}' + +Access your shared files via this URL {{.Scheme}}://{{.Host}}{{.UrlPrefix}}{{.File}} +` +) + +// RunningStatus returns server running status. +func RunningStatus(dir string, host string, scheme string, urlPrefix string, file string) string { + tmpl := template.Must(template.New("running status tmpl").Parse(RunningStatusTmpl)) + data := struct { + Logo string + App string + Dir string + Host string + Scheme string + UrlPrefix string + File string + }{ + Logo: Logo, + App: App, + Dir: dir, + Host: host, + Scheme: scheme, + UrlPrefix: urlPrefix, + File: file, + } + buf := bytes.Buffer{} + if err := tmpl.Execute(&buf, data); err != nil { + log.Fatalln(err) + } + return buf.String() +}