Skip to content

Commit 855eaf7

Browse files
committed
feat: show running status with ascii logo
1 parent 635766a commit 855eaf7

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

args.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"flag"
77
"log"
88
"os"
9+
"path/filepath"
910
"sync"
1011
"text/template"
1112
)
@@ -14,7 +15,8 @@ const (
1415
DefaultHost = `localhost:3927`
1516
DefaultScheme = `http`
1617
DefaultUrlPrefix = `/`
17-
UsageTmpl = `Usage: {{.Exec}} [OPTIONS]
18+
UsageTmpl = `{{.Logo}}
19+
Usage: {{.Exec}} [OPTIONS]
1820
1921
GoShare shares file and directory by HTTP or FTP protocol
2022
@@ -27,12 +29,12 @@ Options:
2729
-v, --version Print version information and quit
2830
2931
Examples:
30-
{{.Exec}} -host example.io -path /opt/share0/releases/
31-
{{.Exec}} -host {{.Host}} -path /opt/share0/releases/
32-
{{.Exec}} --host {{.Host}} --url-prefix /releases/ --path /opt/share0/releases/
33-
{{.Exec}} --host={{.Host}} --url-prefix=/releases/ --path=/opt/share0/releases/
32+
{{.Exec}} -host example.io -path {{.ExamplePath}}
33+
{{.Exec}} -host {{.Host}} -path {{.ExamplePath}}
34+
{{.Exec}} --host {{.Host}} --url-prefix /{{.ExampleUrlPrefix}} --path {{.ExamplePath}}
35+
{{.Exec}} --host={{.Host}} --url-prefix=/{{.ExampleUrlPrefix}} --path={{.ExamplePath}}
3436
35-
See more about {{.App}} at {{.AppLink}}
37+
See more about {{.App}} at {{.Link}}
3638
`
3739
)
3840

@@ -126,21 +128,27 @@ func (a *Argument) Usage() string {
126128
a.Parse()
127129
tmpl := template.Must(template.New("usage tmpl").Parse(UsageTmpl))
128130
data := struct {
129-
App string
130-
AppLink string
131-
Exec string
132-
Host string
133-
Path string
134-
Scheme string
135-
UrlPrefix string
131+
Logo string
132+
Exec string
133+
Host string
134+
Path string
135+
Scheme string
136+
UrlPrefix string
137+
ExamplePath string
138+
ExampleUrlPrefix string
139+
App string
140+
Link string
136141
}{
137-
App: App,
138-
AppLink: AppLink,
139-
Exec: os.Args[0],
140-
Host: DefaultHost,
141-
Path: DefaultPath,
142-
Scheme: DefaultScheme,
143-
UrlPrefix: DefaultUrlPrefix,
142+
Logo: Logo,
143+
Exec: os.Args[0],
144+
Host: DefaultHost,
145+
Path: DefaultPath,
146+
Scheme: DefaultScheme,
147+
UrlPrefix: DefaultUrlPrefix,
148+
ExamplePath: CurrentDirMust(),
149+
ExampleUrlPrefix: filepath.Base(CurrentDirMust()),
150+
App: App,
151+
Link: Link,
144152
}
145153
buf := bytes.Buffer{}
146154
if err := tmpl.Execute(&buf, data); err != nil {

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929

3030
info, err := os.Stat(path)
3131
if os.IsNotExist(err) {
32-
fmt.Println("Error: No such file or directory: " + path)
32+
fmt.Println(err.Error())
3333
fmt.Println(arg.Usage())
3434
return
3535
}
@@ -39,6 +39,6 @@ func main() {
3939
dir, file = filepath.Dir(path), filepath.Base(path)
4040
}
4141
go StartHttpFileService(host, dir, urlPrefix)
42-
fmt.Printf("Share files by the URL %s://%s%s%s\n", scheme, host, urlPrefix, file)
42+
fmt.Println(RunningStatus(dir, host, scheme, urlPrefix, file))
4343
<-quit
4444
}

util.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,19 @@ func CurrentDirMust() string {
5454

5555
const (
5656
App = `GoShare`
57-
AppLink = `https://github.com/duruyao/goshare`
57+
Link = `https://github.com/duruyao/goshare`
5858
Version = `1.0.0`
5959
ReleaseDate = `2022-05-23`
6060
VersionSerialTmpl = `{{.App}} {{.Version}} ({{.ReleaseDate}})`
61+
Logo = `
62+
_____ _____ _
63+
/ ____| / ____| |
64+
| | __ ___| (___ | |__ __ _ _ __ ___
65+
| | |_ |/ _ \\___ \| '_ \ / _' | '__/ _ \
66+
| |__| | (_) |___) | | | | (_| | | | __/
67+
\_____|\___/_____/|_| |_|\__,_|_| \___|
68+
69+
`
6170
)
6271

6372
// VersionSerial returns version serial.
@@ -86,3 +95,38 @@ func FixedUrlPrefix(urlPrefix string) string {
8695
}
8796
return AbsPathMust("/"+urlPrefix) + "/"
8897
}
98+
99+
const (
100+
RunningStatusTmpl = `{{.Logo}}
101+
{{.App}} is handing directory '{{.Dir}}' and listening on '{{.Host}}'
102+
103+
Access your shared files via this URL {{.Scheme}}://{{.Host}}{{.UrlPrefix}}{{.File}}
104+
`
105+
)
106+
107+
// RunningStatus returns server running status.
108+
func RunningStatus(dir string, host string, scheme string, urlPrefix string, file string) string {
109+
tmpl := template.Must(template.New("running status tmpl").Parse(RunningStatusTmpl))
110+
data := struct {
111+
Logo string
112+
App string
113+
Dir string
114+
Host string
115+
Scheme string
116+
UrlPrefix string
117+
File string
118+
}{
119+
Logo: Logo,
120+
App: App,
121+
Dir: dir,
122+
Host: host,
123+
Scheme: scheme,
124+
UrlPrefix: urlPrefix,
125+
File: file,
126+
}
127+
buf := bytes.Buffer{}
128+
if err := tmpl.Execute(&buf, data); err != nil {
129+
log.Fatalln(err)
130+
}
131+
return buf.String()
132+
}

0 commit comments

Comments
 (0)