Skip to content

Commit dea0ffc

Browse files
authored
cmd/gf: add command gf doc (#3634)
1 parent d5825ab commit dea0ffc

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

cmd/gf/gfcmd/gfcmd.go

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func GetCommand(ctx context.Context) (*Command, error) {
8888
cmd.Docker,
8989
cmd.Install,
9090
cmd.Version,
91+
cmd.Doc,
9192
)
9293
if err != nil {
9394
return nil, err

cmd/gf/internal/cmd/cmd_doc.go

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
2+
//
3+
// This Source Code Form is subject to the terms of the MIT License.
4+
// If a copy of the MIT was not distributed with this file,
5+
// You can obtain one at https://github.com/gogf/gf.
6+
7+
package cmd
8+
9+
import (
10+
"context"
11+
"io"
12+
"net/http"
13+
"os"
14+
"path"
15+
"path/filepath"
16+
"time"
17+
18+
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
19+
"github.com/gogf/gf/v2/encoding/gcompress"
20+
"github.com/gogf/gf/v2/frame/g"
21+
"github.com/gogf/gf/v2/os/gfile"
22+
)
23+
24+
const (
25+
// DocURL is the download address of the document
26+
DocURL = "https://github.com/gogf/gf/archive/refs/heads/gh-pages.zip"
27+
)
28+
29+
var (
30+
Doc = cDoc{}
31+
)
32+
33+
type cDoc struct {
34+
g.Meta `name:"doc" brief:"download https://pages.goframe.org/ to run locally"`
35+
}
36+
37+
type cDocInput struct {
38+
g.Meta `name:"doc" config:"gfcli.doc"`
39+
Path string `short:"p" name:"path" brief:"download docs directory path, default is \"%temp%/goframe\""`
40+
Port int `short:"o" name:"port" brief:"http server port, default is 8080" d:"8080"`
41+
Update bool `short:"u" name:"update" brief:"clean docs directory and update docs"`
42+
Clean bool `short:"c" name:"clean" brief:"clean docs directory"`
43+
Proxy string `short:"x" name:"proxy" brief:"proxy for download, such as https://hub.gitmirror.com/;https://ghproxy.com/;https://ghproxy.net/;https://ghps.cc/"`
44+
}
45+
46+
type cDocOutput struct{}
47+
48+
func (c cDoc) Index(ctx context.Context, in cDocInput) (out *cDocOutput, err error) {
49+
docs := NewDocSetting(ctx, in)
50+
mlog.Print("Directory where the document is downloaded:", docs.TempDir)
51+
if in.Clean {
52+
mlog.Print("Cleaning document directory")
53+
err = docs.Clean()
54+
if err != nil {
55+
mlog.Print("Failed to clean document directory:", err)
56+
return
57+
}
58+
return
59+
}
60+
if in.Update {
61+
mlog.Print("Cleaning old document directory")
62+
err = docs.Clean()
63+
if err != nil {
64+
mlog.Print("Failed to clean old document directory:", err)
65+
return
66+
}
67+
}
68+
err = docs.DownloadDoc()
69+
if err != nil {
70+
mlog.Print("Failed to download document:", err)
71+
return
72+
}
73+
s := g.Server()
74+
s.SetServerRoot(docs.DocDir)
75+
s.SetPort(in.Port)
76+
s.SetDumpRouterMap(false)
77+
mlog.Printf("Access address http://127.0.0.1:%d", in.Port)
78+
s.Run()
79+
return
80+
}
81+
82+
// DocSetting doc setting
83+
type DocSetting struct {
84+
TempDir string
85+
DocURL string
86+
DocDir string
87+
DocZipFile string
88+
}
89+
90+
// NewDocSetting new DocSetting
91+
func NewDocSetting(ctx context.Context, in cDocInput) *DocSetting {
92+
fileName := "gf-doc-md.zip"
93+
tempDir := in.Path
94+
if tempDir == "" {
95+
tempDir = gfile.Temp("goframe/docs")
96+
} else {
97+
tempDir = gfile.Abs(path.Join(tempDir, "docs"))
98+
}
99+
100+
return &DocSetting{
101+
TempDir: filepath.FromSlash(tempDir),
102+
DocDir: filepath.FromSlash(path.Join(tempDir, "gf-gh-pages")),
103+
DocURL: in.Proxy + DocURL,
104+
DocZipFile: filepath.FromSlash(path.Join(tempDir, fileName)),
105+
}
106+
107+
}
108+
109+
// Clean clean the temporary directory
110+
func (d *DocSetting) Clean() error {
111+
if _, err := os.Stat(d.TempDir); err == nil {
112+
err = gfile.Remove(d.TempDir)
113+
if err != nil {
114+
mlog.Print("Failed to delete temporary directory:", err)
115+
return err
116+
}
117+
}
118+
return nil
119+
}
120+
121+
// DownloadDoc download the document
122+
func (d *DocSetting) DownloadDoc() error {
123+
if _, err := os.Stat(d.TempDir); err != nil {
124+
err = gfile.Mkdir(d.TempDir)
125+
if err != nil {
126+
mlog.Print("Failed to create temporary directory:", err)
127+
return nil
128+
}
129+
}
130+
// Check if the file exists
131+
if _, err := os.Stat(d.DocDir); err == nil {
132+
mlog.Print("Document already exists, no need to download and unzip")
133+
return nil
134+
}
135+
136+
if _, err := os.Stat(d.DocZipFile); err == nil {
137+
mlog.Print("File already exists, no need to download")
138+
} else {
139+
mlog.Printf("File does not exist, start downloading: %s", d.DocURL)
140+
startTime := time.Now()
141+
// Download the file
142+
resp, err := http.Get(d.DocURL)
143+
if err != nil {
144+
mlog.Print("Failed to download file:", err)
145+
return err
146+
}
147+
defer resp.Body.Close()
148+
149+
// Create the file
150+
out, err := os.Create(d.DocZipFile)
151+
if err != nil {
152+
mlog.Print("Failed to create file:", err)
153+
return err
154+
}
155+
defer out.Close()
156+
157+
// Write the response body to the file
158+
_, err = io.Copy(out, resp.Body)
159+
if err != nil {
160+
mlog.Print("Failed to write file:", err)
161+
return err
162+
}
163+
mlog.Printf("Download successful, time-consuming: %v", time.Since(startTime))
164+
}
165+
166+
mlog.Print("Start unzipping the file...")
167+
// Unzip the file
168+
err := gcompress.UnZipFile(d.DocZipFile, d.TempDir)
169+
if err != nil {
170+
mlog.Print("Failed to unzip the file, please run again:", err)
171+
gfile.Remove(d.DocZipFile)
172+
return err
173+
}
174+
175+
mlog.Print("Download and unzip successful")
176+
return nil
177+
}

0 commit comments

Comments
 (0)