Compare commits
	
		
			1 Commits
		
	
	
		
			c108679fc9
			...
			Release-nf
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					d72d2a8302 | 
							
								
								
									
										34
									
								
								.github/workflows/nfctl.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										34
									
								
								.github/workflows/nfctl.yml
									
									
									
									
										vendored
									
									
								
							@@ -2,10 +2,10 @@ name: Auto Build
 | 
				
			|||||||
on:
 | 
					on:
 | 
				
			||||||
  push:
 | 
					  push:
 | 
				
			||||||
    branches:
 | 
					    branches:
 | 
				
			||||||
      - 'release/nfctl/*'
 | 
					      - 'master'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
env:
 | 
					env:
 | 
				
			||||||
  RELEASE_VERSION: v24.09.23-r1
 | 
					  RELEASE_VERSION: v24.07.13-r1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
jobs:
 | 
					jobs:
 | 
				
			||||||
  build-job:
 | 
					  build-job:
 | 
				
			||||||
@@ -25,38 +25,36 @@ jobs:
 | 
				
			|||||||
          go-version: '1.20'
 | 
					          go-version: '1.20'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: build linux amd64
 | 
					      - name: build linux amd64
 | 
				
			||||||
        run: CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-linux_amd64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
 | 
					        run: CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o dist/nfctl-linux_amd64-$RELEASE_VERSION nft/nfctl/main.go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: build linux arm64
 | 
					      - name: build linux arm64
 | 
				
			||||||
        run: CGO_ENABLE=0 GOOS=linux GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-linux_arm64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
 | 
					        run: CGO_ENABLE=0 GOOS=linux GOARCH=arm64 go build -ldflags='-s -w' -o dist/nfctl-linux_arm64-$RELEASE_VERSION nft/nfctl/main.go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: build windows amd64
 | 
					      - name: build windows amd64
 | 
				
			||||||
        run: CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-win_amd64-${{ env.RELEASE_VERSION }}.exe" nft/nfctl/main.go
 | 
					        run: CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -ldflags='-s -w' -o dist/nfctl-win_amd64-$RELEASE_VERSION.exe nft/nfctl/main.go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: build windows arm64
 | 
					      - name: build windows arm64
 | 
				
			||||||
        run: CGO_ENABLE=0 GOOS=windows GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-win_arm64-${{ env.RELEASE_VERSION }}.exe" nft/nfctl/main.go
 | 
					        run: CGO_ENABLE=0 GOOS=windows GOARCH=arm64 go build -ldflags='-s -w' -o dist/nfctl-win_arm64-$RELEASE_VERSION.exe nft/nfctl/main.go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: build darwin amd64
 | 
					      - name: build darwin amd64
 | 
				
			||||||
        run: CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-darwin_amd64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
 | 
					        run: CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -ldflags='-s -w' -o dist/nfctl-darwin_arm64-$RELEASE_VERSION nft/nfctl/main.go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: build darwin arm64
 | 
					      - name: build darwin arm64
 | 
				
			||||||
        run: CGO_ENABLE=0 GOOS=darwin GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
 | 
					        run: CGO_ENABLE=0 GOOS=darwin GOARCH=arm64 go build -ldflags='-s -w' -o dist/nfctl-darwin_arm64-$RELEASE_VERSION nft/nfctl/main.go
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: show all builds
 | 
					 | 
				
			||||||
        run: ls -lash dist
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: create releases
 | 
					      - name: create releases
 | 
				
			||||||
        id: create_releases
 | 
					        id: create_releases
 | 
				
			||||||
        uses: "marvinpinto/action-automatic-releases@latest"
 | 
					        uses: "marvinpinto/action-automatic-releases@latest"
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          automatic_release_tag: "Release-nfctl-${{ env.RELEASE_VERSION }}"
 | 
					          automatic_release_tag: "Release-nfctl-$RELEASE_VERSION"
 | 
				
			||||||
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
 | 
					          repo_token: "${{ secrets.GITHUB_TOKEN }}"
 | 
				
			||||||
          title: "Release_${{ env.RELEASE_VERSION }}"
 | 
					          title: "Release_$RELEASE_VERSION"
 | 
				
			||||||
          prerelease: false
 | 
					          prerelease: false
 | 
				
			||||||
          files: |
 | 
					          files: |
 | 
				
			||||||
            dist/nfctl-linux_amd64-${{ env.RELEASE_VERSION }}
 | 
					            dist/nfctl-linux_amd64-$RELEASE_VERSION
 | 
				
			||||||
            dist/nfctl-linux_arm64-${{ env.RELEASE_VERSION }}
 | 
					            dist/nfctl-linux_arm64-$RELEASE_VERSION
 | 
				
			||||||
            dist/nfctl-win_amd64-${{ env.RELEASE_VERSION }}.exe
 | 
					            dist/nfctl-win_amd64-$RELEASE_VERSION.exe
 | 
				
			||||||
            dist/nfctl-win_arm64-${{ env.RELEASE_VERSION }}.exe
 | 
					            dist/nfctl-win_arm64-$RELEASE_VERSION.exe
 | 
				
			||||||
            dist/nfctl-darwin_amd64-${{ env.RELEASE_VERSION }}
 | 
					            dist/nfctl-darwin_arm64-$RELEASE_VERSION
 | 
				
			||||||
            dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}
 | 
					            dist/nfctl-darwin_arm64-$RELEASE_VERSION
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										39
									
								
								app.go
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								app.go
									
									
									
									
									
								
							@@ -5,15 +5,13 @@ import (
 | 
				
			|||||||
	"crypto/tls"
 | 
						"crypto/tls"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/internal/bytesconv"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"sync"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/internal/bytesconv"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
@@ -31,8 +29,6 @@ type App struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	trees methodTrees
 | 
						trees methodTrees
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pool *sync.Pool
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	maxParams   uint16
 | 
						maxParams   uint16
 | 
				
			||||||
	maxSections uint16
 | 
						maxSections uint16
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -44,34 +40,13 @@ type App struct {
 | 
				
			|||||||
	removeExtraSlash       bool // false
 | 
						removeExtraSlash       bool // false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a *App) allocateContext() *Ctx {
 | 
					 | 
				
			||||||
	var (
 | 
					 | 
				
			||||||
		skippedNodes = make([]skippedNode, 0, a.maxSections)
 | 
					 | 
				
			||||||
		v            = make(Params, 0, a.maxParams)
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	ctx := Ctx{
 | 
					 | 
				
			||||||
		lock:         sync.Mutex{},
 | 
					 | 
				
			||||||
		app:          a,
 | 
					 | 
				
			||||||
		index:        -1,
 | 
					 | 
				
			||||||
		locals:       make(map[string]any),
 | 
					 | 
				
			||||||
		handlers:     make([]HandlerFunc, 0),
 | 
					 | 
				
			||||||
		skippedNodes: &skippedNodes,
 | 
					 | 
				
			||||||
		params:       &v,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return &ctx
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
 | 
					func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
		err error
 | 
							err error
 | 
				
			||||||
		c   = a.pool.Get().(*Ctx)
 | 
							c   = newContext(a, writer, request)
 | 
				
			||||||
		nfe = new(Err)
 | 
							nfe = new(Err)
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.reset(writer, request)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err = c.verify(); err != nil {
 | 
						if err = c.verify(); err != nil {
 | 
				
			||||||
		if errors.As(err, nfe) {
 | 
							if errors.As(err, nfe) {
 | 
				
			||||||
			_ = c.Status(nfe.Status).SendString(nfe.Msg)
 | 
								_ = c.Status(nfe.Status).SendString(nfe.Msg)
 | 
				
			||||||
@@ -83,8 +58,6 @@ func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	a.handleHTTPRequest(c)
 | 
						a.handleHTTPRequest(c)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	a.pool.Put(c)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a *App) run(ln net.Listener) error {
 | 
					func (a *App) run(ln net.Listener) error {
 | 
				
			||||||
@@ -164,7 +137,9 @@ func (a *App) addRoute(method, path string, handlers ...HandlerFunc) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a *App) handleHTTPRequest(c *Ctx) {
 | 
					func (a *App) handleHTTPRequest(c *Ctx) {
 | 
				
			||||||
	var err error
 | 
						var (
 | 
				
			||||||
 | 
							err error
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	httpMethod := c.Request.Method
 | 
						httpMethod := c.Request.Method
 | 
				
			||||||
	rPath := c.Request.URL.Path
 | 
						rPath := c.Request.URL.Path
 | 
				
			||||||
@@ -288,7 +263,7 @@ func redirectFixedPath(c *Ctx, root *node, trailingSlash bool) bool {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func redirectRequest(c *Ctx) {
 | 
					func redirectRequest(c *Ctx) {
 | 
				
			||||||
	req := c.Request
 | 
						req := c.Request
 | 
				
			||||||
	// rPath := req.URL.Path
 | 
						//rPath := req.URL.Path
 | 
				
			||||||
	rURL := req.URL.String()
 | 
						rURL := req.URL.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	code := http.StatusMovedPermanently // Permanent redirect, request with GET method
 | 
						code := http.StatusMovedPermanently // Permanent redirect, request with GET method
 | 
				
			||||||
@@ -296,7 +271,7 @@ func redirectRequest(c *Ctx) {
 | 
				
			|||||||
		code = http.StatusTemporaryRedirect
 | 
							code = http.StatusTemporaryRedirect
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
 | 
						//debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	http.Redirect(c.Writer, req, rURL, code)
 | 
						http.Redirect(c.Writer, req, rURL, code)
 | 
				
			||||||
	c.writermem.WriteHeaderNow()
 | 
						c.writermem.WriteHeaderNow()
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										98
									
								
								ctx.go
									
									
									
									
									
								
							
							
						
						
									
										98
									
								
								ctx.go
									
									
									
									
									
								
							@@ -2,23 +2,21 @@ package nf
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"html/template"
 | 
						"github.com/loveuer/nf/internal/sse"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"mime/multipart"
 | 
						"mime/multipart"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/google/uuid"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/internal/sse"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var forwardHeaders = []string{"CF-Connecting-IP", "X-Forwarded-For", "X-Real-Ip"}
 | 
					var (
 | 
				
			||||||
 | 
						forwardHeaders = []string{"CF-Connecting-IP", "X-Forwarded-For", "X-Real-Ip"}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Ctx struct {
 | 
					type Ctx struct {
 | 
				
			||||||
	lock       sync.Mutex
 | 
						lock       sync.Mutex
 | 
				
			||||||
@@ -38,29 +36,35 @@ type Ctx struct {
 | 
				
			|||||||
	fullPath     string
 | 
						fullPath     string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Ctx) reset(w http.ResponseWriter, r *http.Request) {
 | 
					func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx {
 | 
				
			||||||
	traceId := r.Header.Get(TraceKey)
 | 
					
 | 
				
			||||||
	if traceId == "" {
 | 
						skippedNodes := make([]skippedNode, 0, app.maxSections)
 | 
				
			||||||
		traceId = uuid.Must(uuid.NewV7()).String()
 | 
						v := make(Params, 0, app.maxParams)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx := &Ctx{
 | 
				
			||||||
 | 
							lock:       sync.Mutex{},
 | 
				
			||||||
 | 
							Request:    request,
 | 
				
			||||||
 | 
							path:       request.URL.Path,
 | 
				
			||||||
 | 
							method:     request.Method,
 | 
				
			||||||
 | 
							StatusCode: 200,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							app:          app,
 | 
				
			||||||
 | 
							index:        -1,
 | 
				
			||||||
 | 
							locals:       map[string]interface{}{},
 | 
				
			||||||
 | 
							handlers:     make([]HandlerFunc, 0),
 | 
				
			||||||
 | 
							skippedNodes: &skippedNodes,
 | 
				
			||||||
 | 
							params:       &v,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.writermem.reset(w)
 | 
						ctx.writermem = responseWriter{
 | 
				
			||||||
 | 
							ResponseWriter: writer,
 | 
				
			||||||
	c.Request = r.WithContext(context.WithValue(r.Context(), TraceKey, traceId))
 | 
							size:           -1,
 | 
				
			||||||
	c.Writer = &c.writermem
 | 
							status:         0,
 | 
				
			||||||
	c.handlers = nil
 | 
					 | 
				
			||||||
	c.index = -1
 | 
					 | 
				
			||||||
	c.path = r.URL.Path
 | 
					 | 
				
			||||||
	c.method = r.Method
 | 
					 | 
				
			||||||
	c.StatusCode = 200
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	c.fullPath = ""
 | 
					 | 
				
			||||||
	*c.params = (*c.params)[:0]
 | 
					 | 
				
			||||||
	*c.skippedNodes = (*c.skippedNodes)[:0]
 | 
					 | 
				
			||||||
	for key := range c.locals {
 | 
					 | 
				
			||||||
		delete(c.locals, key)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	c.writermem.Header().Set(TraceKey, traceId)
 | 
					
 | 
				
			||||||
 | 
						ctx.Writer = &ctx.writermem
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return ctx
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Ctx) Locals(key string, value ...interface{}) interface{} {
 | 
					func (c *Ctx) Locals(key string, value ...interface{}) interface{} {
 | 
				
			||||||
@@ -92,7 +96,9 @@ func (c *Ctx) Path(overWrite ...string) string {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Ctx) Cookies(key string, defaultValue ...string) string {
 | 
					func (c *Ctx) Cookies(key string, defaultValue ...string) string {
 | 
				
			||||||
	dv := ""
 | 
						var (
 | 
				
			||||||
 | 
							dv = ""
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(defaultValue) > 0 {
 | 
						if len(defaultValue) > 0 {
 | 
				
			||||||
		dv = defaultValue[0]
 | 
							dv = defaultValue[0]
 | 
				
			||||||
@@ -106,10 +112,6 @@ func (c *Ctx) Cookies(key string, defaultValue ...string) string {
 | 
				
			|||||||
	return cookie.Value
 | 
						return cookie.Value
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Ctx) Context() context.Context {
 | 
					 | 
				
			||||||
	return c.Request.Context()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *Ctx) Next() error {
 | 
					func (c *Ctx) Next() error {
 | 
				
			||||||
	c.index++
 | 
						c.index++
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -271,30 +273,23 @@ func (c *Ctx) Status(code int) *Ctx {
 | 
				
			|||||||
	c.lock.Lock()
 | 
						c.lock.Lock()
 | 
				
			||||||
	defer c.lock.Unlock()
 | 
						defer c.lock.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.Writer.WriteHeader(code)
 | 
						c.writermem.WriteHeader(code)
 | 
				
			||||||
	c.StatusCode = c.writermem.status
 | 
						c.StatusCode = c.writermem.status
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return c
 | 
						return c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Set set response header
 | 
					 | 
				
			||||||
func (c *Ctx) Set(key string, value string) {
 | 
					func (c *Ctx) Set(key string, value string) {
 | 
				
			||||||
	c.Writer.Header().Set(key, value)
 | 
						c.writermem.Header().Set(key, value)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddHeader add response header
 | 
					 | 
				
			||||||
func (c *Ctx) AddHeader(key string, value string) {
 | 
					 | 
				
			||||||
	c.Writer.Header().Add(key, value)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// SetHeader set response header
 | 
					 | 
				
			||||||
func (c *Ctx) SetHeader(key string, value string) {
 | 
					func (c *Ctx) SetHeader(key string, value string) {
 | 
				
			||||||
	c.Writer.Header().Set(key, value)
 | 
						c.writermem.Header().Set(key, value)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Ctx) SendStatus(code int) error {
 | 
					func (c *Ctx) SendStatus(code int) error {
 | 
				
			||||||
	c.Status(code)
 | 
						c.Status(code)
 | 
				
			||||||
	c.Writer.WriteHeaderNow()
 | 
						c.writermem.WriteHeaderNow()
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -312,7 +307,7 @@ func (c *Ctx) Writef(format string, values ...interface{}) (int, error) {
 | 
				
			|||||||
func (c *Ctx) JSON(data interface{}) error {
 | 
					func (c *Ctx) JSON(data interface{}) error {
 | 
				
			||||||
	c.SetHeader("Content-Type", MIMEApplicationJSON)
 | 
						c.SetHeader("Content-Type", MIMEApplicationJSON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	encoder := json.NewEncoder(c.Writer)
 | 
						encoder := json.NewEncoder(&c.writermem)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err := encoder.Encode(data); err != nil {
 | 
						if err := encoder.Encode(data); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
@@ -344,21 +339,6 @@ func (c *Ctx) HTML(html string) error {
 | 
				
			|||||||
	return err
 | 
						return err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Ctx) RenderHTML(name, html string, obj any) error {
 | 
					 | 
				
			||||||
	c.SetHeader("Content-Type", "text/html")
 | 
					 | 
				
			||||||
	t, err := template.New(name).Parse(html)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return t.Execute(c.Writer, obj)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *Ctx) Redirect(url string, code int) error {
 | 
					 | 
				
			||||||
	http.Redirect(c.Writer, c.Request, url, code)
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *Ctx) Write(data []byte) (int, error) {
 | 
					func (c *Ctx) Write(data []byte) (int, error) {
 | 
				
			||||||
	return c.Writer.Write(data)
 | 
						return c.writermem.Write(data)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								go.mod
									
									
									
									
									
								
							@@ -4,6 +4,7 @@ go 1.20
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/fatih/color v1.17.0
 | 
						github.com/fatih/color v1.17.0
 | 
				
			||||||
 | 
						github.com/go-git/go-billy/v5 v5.5.0
 | 
				
			||||||
	github.com/go-git/go-git/v5 v5.12.0
 | 
						github.com/go-git/go-git/v5 v5.12.0
 | 
				
			||||||
	github.com/google/uuid v1.6.0
 | 
						github.com/google/uuid v1.6.0
 | 
				
			||||||
	github.com/spf13/cobra v1.8.1
 | 
						github.com/spf13/cobra v1.8.1
 | 
				
			||||||
@@ -17,8 +18,6 @@ require (
 | 
				
			|||||||
	github.com/cyphar/filepath-securejoin v0.2.4 // indirect
 | 
						github.com/cyphar/filepath-securejoin v0.2.4 // indirect
 | 
				
			||||||
	github.com/emirpasic/gods v1.18.1 // indirect
 | 
						github.com/emirpasic/gods v1.18.1 // indirect
 | 
				
			||||||
	github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
 | 
						github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
 | 
				
			||||||
	github.com/go-git/go-billy/v5 v5.5.0 // indirect
 | 
					 | 
				
			||||||
	github.com/go-resty/resty/v2 v2.16.2
 | 
					 | 
				
			||||||
	github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
 | 
						github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
 | 
				
			||||||
	github.com/inconshreveable/mousetrap v1.1.0 // indirect
 | 
						github.com/inconshreveable/mousetrap v1.1.0 // indirect
 | 
				
			||||||
	github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
 | 
						github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
 | 
				
			||||||
@@ -26,14 +25,15 @@ require (
 | 
				
			|||||||
	github.com/mattn/go-colorable v0.1.13 // indirect
 | 
						github.com/mattn/go-colorable v0.1.13 // indirect
 | 
				
			||||||
	github.com/mattn/go-isatty v0.0.20 // indirect
 | 
						github.com/mattn/go-isatty v0.0.20 // indirect
 | 
				
			||||||
	github.com/pjbgf/sha1cd v0.3.0 // indirect
 | 
						github.com/pjbgf/sha1cd v0.3.0 // indirect
 | 
				
			||||||
 | 
						github.com/savioxavier/termlink v1.3.0 // indirect
 | 
				
			||||||
	github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
 | 
						github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
 | 
				
			||||||
	github.com/skeema/knownhosts v1.2.2 // indirect
 | 
						github.com/skeema/knownhosts v1.2.2 // indirect
 | 
				
			||||||
	github.com/spf13/pflag v1.0.5 // indirect
 | 
						github.com/spf13/pflag v1.0.5 // indirect
 | 
				
			||||||
	github.com/xanzy/ssh-agent v0.3.3 // indirect
 | 
						github.com/xanzy/ssh-agent v0.3.3 // indirect
 | 
				
			||||||
	golang.org/x/crypto v0.25.0 // indirect
 | 
						golang.org/x/crypto v0.21.0 // indirect
 | 
				
			||||||
	golang.org/x/mod v0.12.0 // indirect
 | 
						golang.org/x/mod v0.12.0 // indirect
 | 
				
			||||||
	golang.org/x/net v0.27.0 // indirect
 | 
						golang.org/x/net v0.22.0 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.22.0 // indirect
 | 
						golang.org/x/sys v0.18.0 // indirect
 | 
				
			||||||
	golang.org/x/tools v0.13.0 // indirect
 | 
						golang.org/x/tools v0.13.0 // indirect
 | 
				
			||||||
	gopkg.in/warnings.v0 v0.1.2 // indirect
 | 
						gopkg.in/warnings.v0 v0.1.2 // indirect
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										21
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								go.sum
									
									
									
									
									
								
							@@ -30,8 +30,6 @@ github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgF
 | 
				
			|||||||
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
 | 
					github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
 | 
				
			||||||
github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys=
 | 
					github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys=
 | 
				
			||||||
github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY=
 | 
					github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY=
 | 
				
			||||||
github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg=
 | 
					 | 
				
			||||||
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
 | 
					 | 
				
			||||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
 | 
					github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
 | 
				
			||||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 | 
					github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 | 
				
			||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
 | 
					github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
 | 
				
			||||||
@@ -62,6 +60,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
 | 
				
			|||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
					github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
				
			||||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
 | 
					github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
 | 
				
			||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 | 
					github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 | 
				
			||||||
 | 
					github.com/savioxavier/termlink v1.3.0 h1:3Gl4FzQjUyiHzmoEDfmWEhgIwDiJY4poOQHP+k8ReA4=
 | 
				
			||||||
 | 
					github.com/savioxavier/termlink v1.3.0/go.mod h1:5T5ePUlWbxCHIwyF8/Ez1qufOoGM89RCg9NvG+3G3gc=
 | 
				
			||||||
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
 | 
					github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
 | 
				
			||||||
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
 | 
					github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
 | 
				
			||||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
 | 
					github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
 | 
				
			||||||
@@ -83,8 +83,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
 | 
				
			|||||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 | 
					golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 | 
				
			||||||
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
 | 
					golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
 | 
				
			||||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
 | 
					golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
 | 
				
			||||||
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
 | 
					golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
 | 
				
			||||||
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
 | 
					golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
 | 
				
			||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
 | 
					golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
 | 
				
			||||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
 | 
					golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
 | 
				
			||||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
 | 
					golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
 | 
				
			||||||
@@ -96,8 +96,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
 | 
				
			|||||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
 | 
					golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
 | 
				
			||||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
 | 
					golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
 | 
				
			||||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
 | 
					golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
 | 
				
			||||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
 | 
					golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
 | 
				
			||||||
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
 | 
					golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
 | 
				
			||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
					golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
				
			||||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
					golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
				
			||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
					golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
				
			||||||
@@ -116,14 +116,14 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
				
			|||||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
					golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
				
			||||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
					golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
				
			||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
					golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
				
			||||||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
 | 
					golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
 | 
				
			||||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 | 
					golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 | 
				
			||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 | 
					golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 | 
				
			||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 | 
					golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 | 
				
			||||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
 | 
					golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
 | 
				
			||||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
 | 
					golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
 | 
				
			||||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
 | 
					golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
 | 
				
			||||||
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
 | 
					golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
 | 
				
			||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
					golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
				
			||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 | 
					golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 | 
				
			||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 | 
					golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 | 
				
			||||||
@@ -131,8 +131,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 | 
				
			|||||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 | 
					golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 | 
				
			||||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 | 
					golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 | 
				
			||||||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
 | 
					golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
 | 
				
			||||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
 | 
					golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
 | 
				
			||||||
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
 | 
					 | 
				
			||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
					golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
				
			||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 | 
					golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 | 
				
			||||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
 | 
					golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,11 +2,12 @@ package nf
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/google/uuid"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/log"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"runtime/debug"
 | 
						"runtime/debug"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/log"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewRecover(enableStackTrace bool) HandlerFunc {
 | 
					func NewRecover(enableStackTrace bool) HandlerFunc {
 | 
				
			||||||
@@ -19,7 +20,7 @@ func NewRecover(enableStackTrace bool) HandlerFunc {
 | 
				
			|||||||
					os.Stderr.WriteString(fmt.Sprintf("recovered from panic: %v\n", r))
 | 
										os.Stderr.WriteString(fmt.Sprintf("recovered from panic: %v\n", r))
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// serveError(c, 500, []byte(fmt.Sprint(r)))
 | 
									//serveError(c, 500, []byte(fmt.Sprint(r)))
 | 
				
			||||||
				_ = c.Status(500).SendString(fmt.Sprint(r))
 | 
									_ = c.Status(500).SendString(fmt.Sprint(r))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}()
 | 
							}()
 | 
				
			||||||
@@ -28,18 +29,33 @@ func NewRecover(enableStackTrace bool) HandlerFunc {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewLogger() HandlerFunc {
 | 
					func NewLogger(traceHeader ...string) HandlerFunc {
 | 
				
			||||||
 | 
						Header := "X-Trace-ID"
 | 
				
			||||||
 | 
						if len(traceHeader) > 0 && traceHeader[0] != "" {
 | 
				
			||||||
 | 
							Header = traceHeader[0]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return func(c *Ctx) error {
 | 
						return func(c *Ctx) error {
 | 
				
			||||||
		var (
 | 
							var (
 | 
				
			||||||
			now   = time.Now()
 | 
								now   = time.Now()
 | 
				
			||||||
 | 
								trace = c.Get(Header)
 | 
				
			||||||
			logFn func(msg string, data ...any)
 | 
								logFn func(msg string, data ...any)
 | 
				
			||||||
			ip    = c.IP()
 | 
								ip    = c.IP()
 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if trace == "" {
 | 
				
			||||||
 | 
								trace = uuid.Must(uuid.NewV7()).String()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							c.SetHeader(Header, trace)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							traces := strings.Split(trace, "-")
 | 
				
			||||||
 | 
							shortTrace := traces[len(traces)-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err := c.Next()
 | 
							err := c.Next()
 | 
				
			||||||
		duration := time.Since(now)
 | 
							duration := time.Since(now)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		msg := fmt.Sprintf("NF | %v | %15s | %3d | %s | %6s | %s", c.Context().Value(TraceKey), ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
 | 
							msg := fmt.Sprintf("NF | %s | %15s | %3d | %s | %6s | %s", shortTrace, ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		switch {
 | 
							switch {
 | 
				
			||||||
		case c.StatusCode >= 500:
 | 
							case c.StatusCode >= 500:
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										47
									
								
								nf.go
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								nf.go
									
									
									
									
									
								
							@@ -1,13 +1,10 @@
 | 
				
			|||||||
package nf
 | 
					package nf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "sync"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	banner   = "  _  _     _     ___                 _ \n | \\| |___| |_  | __|__ _  _ _ _  __| |\n | .` / _ \\  _| | _/ _ \\ || | ' \\/ _` |\n |_|\\_\\___/\\__| |_|\\___/\\_,_|_||_\\__,_|\n "
 | 
						banner = "  _  _     _     ___                 _ \n | \\| |___| |_  | __|__ _  _ _ _  __| |\n | .` / _ \\  _| | _/ _ \\ || | ' \\/ _` |\n |_|\\_\\___/\\__| |_|\\___/\\_,_|_||_\\__,_|\n "
 | 
				
			||||||
	_404     = "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Not Found</title><style>body{background:#333;margin:0;color:#ccc;display:flex;align-items:center;max-height:100vh;height:100vh;justify-content:center}textarea{min-height:5rem;min-width:20rem;text-align:center;border:none;background:0 0;color:#ccc;resize:none;user-input:none;user-select:none;cursor:default;-webkit-user-select:none;-webkit-touch-callout:none;-moz-user-select:none;-ms-user-select:none;outline:0}</style></head><body><textarea id=\"banner\" readonly=\"readonly\"></textarea><script type=\"text/javascript\">let htmlCodes = [\n    ' _  _     _     ___                 _ ',\n    '| \\\\| |___| |_  | __|__ _  _ _ _  __| |',\n    '| .` / _ \\\\  _| | _/ _ \\\\ || | \\' \\\\/ _` |',\n    '|_|\\\\_\\\\___/\\\\__| |_|\\\\___/\\\\_,_|_||_\\\\__,_|'\n].join('\\n');\ndocument.querySelector('#banner').value = htmlCodes</script></body></html>"
 | 
						_404   = "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Not Found</title><style>body{background:#333;margin:0;color:#ccc;display:flex;align-items:center;max-height:100vh;height:100vh;justify-content:center}textarea{min-height:5rem;min-width:20rem;text-align:center;border:none;background:0 0;color:#ccc;resize:none;user-input:none;user-select:none;cursor:default;-webkit-user-select:none;-webkit-touch-callout:none;-moz-user-select:none;-ms-user-select:none;outline:0}</style></head><body><textarea id=\"banner\" readonly=\"readonly\"></textarea><script type=\"text/javascript\">let htmlCodes = [\n    ' _  _     _     ___                 _ ',\n    '| \\\\| |___| |_  | __|__ _  _ _ _  __| |',\n    '| .` / _ \\\\  _| | _/ _ \\\\ || | \\' \\\\/ _` |',\n    '|_|\\\\_\\\\___/\\\\__| |_|\\\\___/\\\\_,_|_||_\\\\__,_|'\n].join('\\n');\ndocument.querySelector('#banner').value = htmlCodes</script></body></html>"
 | 
				
			||||||
	_405     = `405 Method Not Allowed`
 | 
						_405   = `405 Method Not Allowed`
 | 
				
			||||||
	_500     = `500 Internal Server Error`
 | 
						_500   = `500 Internal Server Error`
 | 
				
			||||||
	TraceKey = "X-Trace-Id"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Map map[string]interface{}
 | 
					type Map map[string]interface{}
 | 
				
			||||||
@@ -25,24 +22,26 @@ type Config struct {
 | 
				
			|||||||
	DisableRecover      bool `json:"-"`
 | 
						DisableRecover      bool `json:"-"`
 | 
				
			||||||
	DisableHttpErrorLog bool `json:"-"`
 | 
						DisableHttpErrorLog bool `json:"-"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// EnableNotImplementHandler bool        `json:"-"`
 | 
						//EnableNotImplementHandler bool        `json:"-"`
 | 
				
			||||||
	NotFoundHandler         HandlerFunc `json:"-"`
 | 
						NotFoundHandler         HandlerFunc `json:"-"`
 | 
				
			||||||
	MethodNotAllowedHandler HandlerFunc `json:"-"`
 | 
						MethodNotAllowedHandler HandlerFunc `json:"-"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var defaultConfig = &Config{
 | 
					var (
 | 
				
			||||||
	BodyLimit: 4 * 1024 * 1024,
 | 
						defaultConfig = &Config{
 | 
				
			||||||
	NotFoundHandler: func(c *Ctx) error {
 | 
							BodyLimit: 4 * 1024 * 1024,
 | 
				
			||||||
		c.Set("Content-Type", MIMETextHTML)
 | 
							NotFoundHandler: func(c *Ctx) error {
 | 
				
			||||||
		_, err := c.Status(404).Write([]byte(_404))
 | 
								c.Set("Content-Type", MIMETextHTML)
 | 
				
			||||||
		return err
 | 
								_, err := c.Status(404).Write([]byte(_404))
 | 
				
			||||||
	},
 | 
								return err
 | 
				
			||||||
	MethodNotAllowedHandler: func(c *Ctx) error {
 | 
							},
 | 
				
			||||||
		c.Set("Content-Type", MIMETextPlain)
 | 
							MethodNotAllowedHandler: func(c *Ctx) error {
 | 
				
			||||||
		_, err := c.Status(405).Write([]byte(_405))
 | 
								c.Set("Content-Type", MIMETextPlain)
 | 
				
			||||||
		return err
 | 
								_, err := c.Status(405).Write([]byte(_405))
 | 
				
			||||||
	},
 | 
								return err
 | 
				
			||||||
}
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func New(config ...Config) *App {
 | 
					func New(config ...Config) *App {
 | 
				
			||||||
	app := &App{
 | 
						app := &App{
 | 
				
			||||||
@@ -52,8 +51,6 @@ func New(config ...Config) *App {
 | 
				
			|||||||
			root:     true,
 | 
								root:     true,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		pool: &sync.Pool{},
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		redirectTrailingSlash:  true,  // true
 | 
							redirectTrailingSlash:  true,  // true
 | 
				
			||||||
		redirectFixedPath:      false, // false
 | 
							redirectFixedPath:      false, // false
 | 
				
			||||||
		handleMethodNotAllowed: true,  // false
 | 
							handleMethodNotAllowed: true,  // false
 | 
				
			||||||
@@ -91,9 +88,5 @@ func New(config ...Config) *App {
 | 
				
			|||||||
		app.Use(NewRecover(true))
 | 
							app.Use(NewRecover(true))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	app.pool.New = func() any {
 | 
					 | 
				
			||||||
		return app.allocateContext()
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return app
 | 
						return app
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,123 +0,0 @@
 | 
				
			|||||||
package loading
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Type int
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const (
 | 
					 | 
				
			||||||
	TypeProcessing Type = iota
 | 
					 | 
				
			||||||
	TypeInfo
 | 
					 | 
				
			||||||
	TypeSuccess
 | 
					 | 
				
			||||||
	TypeWarning
 | 
					 | 
				
			||||||
	TypeError
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (t Type) Symbol() string {
 | 
					 | 
				
			||||||
	switch t {
 | 
					 | 
				
			||||||
	case TypeSuccess:
 | 
					 | 
				
			||||||
		return "✔️  "
 | 
					 | 
				
			||||||
	case TypeWarning:
 | 
					 | 
				
			||||||
		return "❗ "
 | 
					 | 
				
			||||||
	case TypeError:
 | 
					 | 
				
			||||||
		return "❌ "
 | 
					 | 
				
			||||||
	case TypeInfo:
 | 
					 | 
				
			||||||
		return "❕ "
 | 
					 | 
				
			||||||
	default:
 | 
					 | 
				
			||||||
		return ""
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type _msg struct {
 | 
					 | 
				
			||||||
	msg string
 | 
					 | 
				
			||||||
	t   Type
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var frames = []string{"|", "/", "-", "\\"}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func Do(ctx context.Context, fn func(ctx context.Context, print func(msg string, types ...Type)) error) (err error) {
 | 
					 | 
				
			||||||
	start := time.Now()
 | 
					 | 
				
			||||||
	ch := make(chan *_msg)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	go func() {
 | 
					 | 
				
			||||||
		var (
 | 
					 | 
				
			||||||
			m          *_msg
 | 
					 | 
				
			||||||
			ok         bool
 | 
					 | 
				
			||||||
			processing string
 | 
					 | 
				
			||||||
		)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		defer func() {
 | 
					 | 
				
			||||||
			fmt.Printf("\r\033[K")
 | 
					 | 
				
			||||||
		}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		for {
 | 
					 | 
				
			||||||
			for _, frame := range frames {
 | 
					 | 
				
			||||||
				select {
 | 
					 | 
				
			||||||
				case <-ctx.Done():
 | 
					 | 
				
			||||||
					return
 | 
					 | 
				
			||||||
				case m, ok = <-ch:
 | 
					 | 
				
			||||||
					if !ok || m == nil {
 | 
					 | 
				
			||||||
						return
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
					switch m.t {
 | 
					 | 
				
			||||||
					case TypeProcessing:
 | 
					 | 
				
			||||||
						if m.msg != "" {
 | 
					 | 
				
			||||||
							processing = m.msg
 | 
					 | 
				
			||||||
						}
 | 
					 | 
				
			||||||
					case TypeInfo,
 | 
					 | 
				
			||||||
						TypeSuccess,
 | 
					 | 
				
			||||||
						TypeWarning,
 | 
					 | 
				
			||||||
						TypeError:
 | 
					 | 
				
			||||||
						// Clear the loading animation
 | 
					 | 
				
			||||||
						fmt.Printf("\r\033[K")
 | 
					 | 
				
			||||||
						fmt.Printf("%s%s\n", m.t.Symbol(), m.msg)
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
				default:
 | 
					 | 
				
			||||||
					elapsed := time.Since(start).Seconds()
 | 
					 | 
				
			||||||
					if processing != "" {
 | 
					 | 
				
			||||||
						fmt.Printf("\r\033[K%s  %s (%.2fs)", frame, processing, elapsed)
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
					time.Sleep(100 * time.Millisecond)
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	printFn := func(msg string, types ...Type) {
 | 
					 | 
				
			||||||
		if msg == "" {
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		m := &_msg{
 | 
					 | 
				
			||||||
			msg: msg,
 | 
					 | 
				
			||||||
			t:   TypeProcessing,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if len(types) > 0 {
 | 
					 | 
				
			||||||
			m.t = types[0]
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		ch <- m
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	done := make(chan struct{})
 | 
					 | 
				
			||||||
	go func() {
 | 
					 | 
				
			||||||
		if err = fn(ctx, printFn); err != nil {
 | 
					 | 
				
			||||||
			ch <- &_msg{msg: err.Error(), t: TypeError}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		close(ch)
 | 
					 | 
				
			||||||
		done <- struct{}{}
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	select {
 | 
					 | 
				
			||||||
	case <-ctx.Done():
 | 
					 | 
				
			||||||
	case <-done:
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return err
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,25 +0,0 @@
 | 
				
			|||||||
package loading
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"os/signal"
 | 
					 | 
				
			||||||
	"syscall"
 | 
					 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestLoadingPrint(t *testing.T) {
 | 
					 | 
				
			||||||
	ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
 | 
					 | 
				
			||||||
	defer cancel()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Do(ctx, func(ctx context.Context, print func(msg string, types ...Type)) error {
 | 
					 | 
				
			||||||
		print("start task 1...")
 | 
					 | 
				
			||||||
		time.Sleep(3 * time.Second)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		print("warning...1", TypeWarning)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		time.Sleep(2 * time.Second)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -21,7 +21,7 @@ var (
 | 
				
			|||||||
		os.Exit(1)
 | 
							os.Exit(1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	DefaultLogger = &logger{
 | 
						defaultLogger = &logger{
 | 
				
			||||||
		Mutex:      sync.Mutex{},
 | 
							Mutex:      sync.Mutex{},
 | 
				
			||||||
		timeFormat: "2006-01-02T15:04:05",
 | 
							timeFormat: "2006-01-02T15:04:05",
 | 
				
			||||||
		writer:     os.Stdout,
 | 
							writer:     os.Stdout,
 | 
				
			||||||
@@ -36,32 +36,32 @@ var (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func SetTimeFormat(format string) {
 | 
					func SetTimeFormat(format string) {
 | 
				
			||||||
	DefaultLogger.SetTimeFormat(format)
 | 
						defaultLogger.SetTimeFormat(format)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func SetLogLevel(level LogLevel) {
 | 
					func SetLogLevel(level LogLevel) {
 | 
				
			||||||
	DefaultLogger.SetLogLevel(level)
 | 
						defaultLogger.SetLogLevel(level)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Debug(msg string, data ...any) {
 | 
					func Debug(msg string, data ...any) {
 | 
				
			||||||
	DefaultLogger.Debug(msg, data...)
 | 
						defaultLogger.Debug(msg, data...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func Info(msg string, data ...any) {
 | 
					func Info(msg string, data ...any) {
 | 
				
			||||||
	DefaultLogger.Info(msg, data...)
 | 
						defaultLogger.Info(msg, data...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Warn(msg string, data ...any) {
 | 
					func Warn(msg string, data ...any) {
 | 
				
			||||||
	DefaultLogger.Warn(msg, data...)
 | 
						defaultLogger.Warn(msg, data...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Error(msg string, data ...any) {
 | 
					func Error(msg string, data ...any) {
 | 
				
			||||||
	DefaultLogger.Error(msg, data...)
 | 
						defaultLogger.Error(msg, data...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Panic(msg string, data ...any) {
 | 
					func Panic(msg string, data ...any) {
 | 
				
			||||||
	DefaultLogger.Panic(msg, data...)
 | 
						defaultLogger.Panic(msg, data...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Fatal(msg string, data ...any) {
 | 
					func Fatal(msg string, data ...any) {
 | 
				
			||||||
	DefaultLogger.Fatal(msg, data...)
 | 
						defaultLogger.Fatal(msg, data...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,21 +1,16 @@
 | 
				
			|||||||
package tool
 | 
					package clone
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/url"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/go-git/go-git/v5"
 | 
						"github.com/go-git/go-git/v5"
 | 
				
			||||||
 | 
						_ "github.com/go-git/go-git/v5"
 | 
				
			||||||
	"github.com/go-git/go-git/v5/plumbing/transport/http"
 | 
						"github.com/go-git/go-git/v5/plumbing/transport/http"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/log"
 | 
				
			||||||
 | 
						"net/url"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Clone(projectDir string, repoURL string) error {
 | 
					func Clone(pwd string, ins *url.URL) error {
 | 
				
			||||||
	ins, err := url.Parse(repoURL)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	uri := fmt.Sprintf("%s://%s%s", ins.Scheme, ins.Host, ins.Path)
 | 
						uri := fmt.Sprintf("%s://%s%s", ins.Scheme, ins.Host, ins.Path)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	opt := &git.CloneOptions{
 | 
						opt := &git.CloneOptions{
 | 
				
			||||||
		URL:             uri,
 | 
							URL:             uri,
 | 
				
			||||||
		Depth:           1,
 | 
							Depth:           1,
 | 
				
			||||||
@@ -31,7 +26,8 @@ func Clone(projectDir string, repoURL string) error {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_, err = git.PlainClone(projectDir, false, opt)
 | 
						log.Info("start clone %s", uri)
 | 
				
			||||||
 | 
						_, err := git.PlainClone(pwd, false, opt)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
							
								
								
									
										19
									
								
								nft/nfctl/cmd/cmd.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								nft/nfctl/cmd/cmd.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "github.com/spf13/cobra"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						Root = &cobra.Command{
 | 
				
			||||||
 | 
							Use:   "nfctl",
 | 
				
			||||||
 | 
							Short: "nfctl: easy start your nf backend work",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						initNew()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Root.AddCommand(
 | 
				
			||||||
 | 
							versionCmd,
 | 
				
			||||||
 | 
							cmdNew,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										133
									
								
								nft/nfctl/cmd/new.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								nft/nfctl/cmd/new.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,133 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/log"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/nfctl/clone"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/nfctl/opt"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/nfctl/tp"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
						"net/url"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						cmdNew = &cobra.Command{
 | 
				
			||||||
 | 
							Use:   "new",
 | 
				
			||||||
 | 
							Short: "nfctl new: start new project",
 | 
				
			||||||
 | 
							Example: `nfctl new {project} -t ultone [recommend]
 | 
				
			||||||
 | 
					nfctl new {project} -t https://github.com/loveuer/ultone.git
 | 
				
			||||||
 | 
					nfctl new {project} --template http://username:token@my.gitlab.com/my-zone/my-repo.git
 | 
				
			||||||
 | 
					`,
 | 
				
			||||||
 | 
							SilenceUsage: true,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						template    string
 | 
				
			||||||
 | 
						disableInit bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						preTemplateMap = map[string]string{
 | 
				
			||||||
 | 
							"ultone": "https://gitcode.com/loveuer/ultone.git",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func initNew() {
 | 
				
			||||||
 | 
						cmdNew.Flags().BoolVar(&opt.Debug, "debug", false, "debug mode")
 | 
				
			||||||
 | 
						cmdNew.Flags().StringVarP(&template, "template", "t", "", "template name/url[example:ultone, https://github.com/xxx/yyy.git]")
 | 
				
			||||||
 | 
						cmdNew.Flags().BoolVar(&disableInit, "without-init", false, "don't run template init script")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmdNew.RunE = func(cmd *cobra.Command, args []string) error {
 | 
				
			||||||
 | 
							if opt.Debug {
 | 
				
			||||||
 | 
								log.SetLogLevel(log.LogLevelDebug)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var (
 | 
				
			||||||
 | 
								err        error
 | 
				
			||||||
 | 
								urlIns     *url.URL
 | 
				
			||||||
 | 
								pwd        string
 | 
				
			||||||
 | 
								projectDir string
 | 
				
			||||||
 | 
								initBs     []byte
 | 
				
			||||||
 | 
								renderBs   []byte
 | 
				
			||||||
 | 
								scripts    []tp.Cmd
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if len(args) == 0 {
 | 
				
			||||||
 | 
								return fmt.Errorf("project name required")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if pwd, err = os.Getwd(); err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("get work dir err")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							projectDir = path.Join(pwd, args[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if _, err = os.Stat(projectDir); !errors.Is(err, os.ErrNotExist) {
 | 
				
			||||||
 | 
								return fmt.Errorf("project folder already exist")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if err = os.MkdirAll(projectDir, 0750); err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("create project dir err: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							defer func() {
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									_ = os.RemoveAll(projectDir)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if template == "" {
 | 
				
			||||||
 | 
								// todo no template new project
 | 
				
			||||||
 | 
								return fmt.Errorf("😥create basic project(without template) comming soon...")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							cloneUrl := template
 | 
				
			||||||
 | 
							if ptUrl, ok := preTemplateMap[cloneUrl]; ok {
 | 
				
			||||||
 | 
								cloneUrl = ptUrl
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if urlIns, err = url.Parse(cloneUrl); err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("invalid clone url: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if err = clone.Clone(projectDir, urlIns); err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("clone template err: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if initBs, err = os.ReadFile(path.Join(projectDir, ".nfctl")); err != nil {
 | 
				
			||||||
 | 
								if errors.Is(err, os.ErrNotExist) {
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return fmt.Errorf("read nfctl script file err: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if renderBs, err = tp.RenderVar(initBs, map[string]any{
 | 
				
			||||||
 | 
								"PROJECT_NAME": args[0],
 | 
				
			||||||
 | 
							}); err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("render template init script err: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if scripts, err = tp.ParseCmd(projectDir, renderBs); err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("parse template init script err: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for _, script := range scripts {
 | 
				
			||||||
 | 
								if opt.Debug {
 | 
				
			||||||
 | 
									log.Debug("start script:\n%s\n", script.String())
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if err = script.Execute(); err != nil {
 | 
				
			||||||
 | 
									return fmt.Errorf("execute template init script err: %v", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if err = os.RemoveAll(path.Join(projectDir, ".git")); err != nil {
 | 
				
			||||||
 | 
								log.Warn("remove .git folder err: %s", err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							log.Info("🎉 create project [%s] 成功!!!", args[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										17
									
								
								nft/nfctl/cmd/version.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								nft/nfctl/cmd/version.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					package cmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/log"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/nfctl/version"
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						versionCmd = &cobra.Command{
 | 
				
			||||||
 | 
							Use:   "version",
 | 
				
			||||||
 | 
							Short: "print nfctl version and exit",
 | 
				
			||||||
 | 
							Run: func(cmd *cobra.Command, args []string) {
 | 
				
			||||||
 | 
								log.Info("version: %s", version.Version)
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
@@ -1,149 +0,0 @@
 | 
				
			|||||||
package cmd
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"bufio"
 | 
					 | 
				
			||||||
	"bytes"
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"path"
 | 
					 | 
				
			||||||
	"path/filepath"
 | 
					 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
	"text/template"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/loading"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/log"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/nfctl/internal/opt"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/tool"
 | 
					 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var newCmd = &cobra.Command{
 | 
					 | 
				
			||||||
	Use:           "new",
 | 
					 | 
				
			||||||
	Short:         "new a nf project",
 | 
					 | 
				
			||||||
	Example:       "nfctl new <project> -t ultone [options]",
 | 
					 | 
				
			||||||
	RunE:          doNew,
 | 
					 | 
				
			||||||
	SilenceErrors: true,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func initNew() *cobra.Command {
 | 
					 | 
				
			||||||
	newCmd.Flags().StringVarP(&opt.Cfg.New.Template, "template", "t", "ultone", "template name/url[example:ultone, https://gitea.loveuer.com/loveuer/ultone.git]")
 | 
					 | 
				
			||||||
	newCmd.Flags().BoolVar(&opt.Cfg.New.DisableInitScript, "disable-init-script", false, "disable init script(.nfctl)")
 | 
					 | 
				
			||||||
	return newCmd
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func doNew(cmd *cobra.Command, args []string) (err error) {
 | 
					 | 
				
			||||||
	if len(args) == 0 {
 | 
					 | 
				
			||||||
		return errors.New("必须提供 project 名称")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if strings.HasSuffix(args[0], "/") {
 | 
					 | 
				
			||||||
		return errors.New("project 名称不能以 / 结尾")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	base := path.Base(args[0])
 | 
					 | 
				
			||||||
	if strings.HasPrefix(base, ".") {
 | 
					 | 
				
			||||||
		return errors.New("project 名称不能以 . 开头")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return loading.Do(cmd.Context(), func(ctx context.Context, print func(msg string, types ...loading.Type)) error {
 | 
					 | 
				
			||||||
		print("开始新建项目: "+args[0], loading.TypeInfo)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		pwd, err := os.Getwd()
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		moduleName := args[0]
 | 
					 | 
				
			||||||
		pwd = path.Join(filepath.ToSlash(pwd), base)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		log.Debug("cmd.new: new project, pwd = %s, name = %s, template = %s", pwd, moduleName, opt.Cfg.New.Template)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		print("开始下载模板: "+opt.Cfg.New.Template, loading.TypeProcessing)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		repo := opt.Cfg.New.Template
 | 
					 | 
				
			||||||
		if v, ok := opt.TemplateMap[repo]; ok {
 | 
					 | 
				
			||||||
			repo = v
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if err = tool.Clone(pwd, repo); err != nil {
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		print("下载模板完成: "+opt.Cfg.New.Template, loading.TypeSuccess)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if err = os.RemoveAll(path.Join(pwd, ".git")); err != nil {
 | 
					 | 
				
			||||||
			print(err.Error(), loading.TypeWarning)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		print("开始初始化项目: "+args[0], loading.TypeProcessing)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if err = filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				return err
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if info.IsDir() {
 | 
					 | 
				
			||||||
				return nil
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "go.mod") {
 | 
					 | 
				
			||||||
				var content []byte
 | 
					 | 
				
			||||||
				if content, err = os.ReadFile(path); err != nil {
 | 
					 | 
				
			||||||
					print("初始化文件失败: "+err.Error(), loading.TypeWarning)
 | 
					 | 
				
			||||||
					print("开始初始化项目: "+args[0], loading.TypeProcessing)
 | 
					 | 
				
			||||||
					return nil
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				scanner := bufio.NewScanner(bytes.NewReader(content))
 | 
					 | 
				
			||||||
				replaced := make([]string, 0, 16)
 | 
					 | 
				
			||||||
				for scanner.Scan() {
 | 
					 | 
				
			||||||
					line := scanner.Text()
 | 
					 | 
				
			||||||
					// 操作 go.mod 文件时, 忽略 toolchain 行, 以更好的兼容 go1.20
 | 
					 | 
				
			||||||
					if strings.HasSuffix(path, "go.mod") && strings.HasPrefix(line, "toolchain") {
 | 
					 | 
				
			||||||
						continue
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
					replaced = append(replaced, strings.ReplaceAll(line, opt.Cfg.New.Template, moduleName))
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				if err = os.WriteFile(path, []byte(strings.Join(replaced, "\n")), 0o644); err != nil {
 | 
					 | 
				
			||||||
					return err
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return nil
 | 
					 | 
				
			||||||
		}); err != nil {
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		var (
 | 
					 | 
				
			||||||
			render *template.Template
 | 
					 | 
				
			||||||
			rf     *os.File
 | 
					 | 
				
			||||||
		)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if render, err = template.New(base).Parse(opt.README); err != nil {
 | 
					 | 
				
			||||||
			log.Debug("cmd.new: new text template err, err = %s", err.Error())
 | 
					 | 
				
			||||||
			print("生成 readme 失败", loading.TypeWarning)
 | 
					 | 
				
			||||||
			goto END
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if rf, err = os.OpenFile(path.Join(pwd, "readme.md"), os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0o644); err != nil {
 | 
					 | 
				
			||||||
			log.Debug("cmd.new: new readme file err, err = %s", err.Error())
 | 
					 | 
				
			||||||
			print("生成 readme 失败", loading.TypeWarning)
 | 
					 | 
				
			||||||
			goto END
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		defer rf.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if err = render.Execute(rf, map[string]any{
 | 
					 | 
				
			||||||
			"project_name": base,
 | 
					 | 
				
			||||||
		}); err != nil {
 | 
					 | 
				
			||||||
			log.Debug("cmd.new: template execute err, err = %s", err.Error())
 | 
					 | 
				
			||||||
			print("生成 readme 失败", loading.TypeWarning)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	END:
 | 
					 | 
				
			||||||
		print(fmt.Sprintf("项目: %s 初始化成功", args[0]), loading.TypeSuccess)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,41 +0,0 @@
 | 
				
			|||||||
package cmd
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/log"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/nfctl/internal/opt"
 | 
					 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var rootCmd = &cobra.Command{
 | 
					 | 
				
			||||||
	Use:   "nfctl",
 | 
					 | 
				
			||||||
	Short: "nfctl is a tool for quick start a nf projects",
 | 
					 | 
				
			||||||
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
 | 
					 | 
				
			||||||
		if opt.Cfg.Debug {
 | 
					 | 
				
			||||||
			log.SetLogLevel(log.LogLevelDebug)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if opt.Cfg.Version {
 | 
					 | 
				
			||||||
			doVersion(cmd, args)
 | 
					 | 
				
			||||||
			os.Exit(0)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if !opt.Cfg.DisableUpdate {
 | 
					 | 
				
			||||||
			doUpdate(cmd.Context())
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	},
 | 
					 | 
				
			||||||
	DisableSuggestions: true,
 | 
					 | 
				
			||||||
	SilenceUsage:       true,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Run: func(cmd *cobra.Command, args []string) {},
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func initRoot(cmds ...*cobra.Command) {
 | 
					 | 
				
			||||||
	rootCmd.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
 | 
					 | 
				
			||||||
	rootCmd.PersistentFlags().BoolVar(&opt.Cfg.DisableUpdate, "disable-update", false, "disable self update")
 | 
					 | 
				
			||||||
	rootCmd.PersistentFlags().BoolVarP(&opt.Cfg.Version, "version", "v", false, "print nfctl version")
 | 
					 | 
				
			||||||
	rootCmd.AddCommand(cmds...)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,83 +0,0 @@
 | 
				
			|||||||
package cmd
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"crypto/tls"
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"regexp"
 | 
					 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	resty "github.com/go-resty/resty/v2"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/loading"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/log"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/nfctl/internal/opt"
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/tool"
 | 
					 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var updateCmd = &cobra.Command{
 | 
					 | 
				
			||||||
	Use:   "update",
 | 
					 | 
				
			||||||
	Short: "update nfctl self",
 | 
					 | 
				
			||||||
	RunE: func(cmd *cobra.Command, args []string) error {
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	},
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func initUpdate() *cobra.Command {
 | 
					 | 
				
			||||||
	return updateCmd
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func doUpdate(ctx context.Context) (err error) {
 | 
					 | 
				
			||||||
	return loading.Do(tool.TimeoutCtx(ctx, 30), func(ctx context.Context, print func(msg string, types ...loading.Type)) error {
 | 
					 | 
				
			||||||
		print("正在检查更新...")
 | 
					 | 
				
			||||||
		tip := "❗ 请尝试手动更新: go install github.com/loveuer/nf/nft/nfctl@master"
 | 
					 | 
				
			||||||
		version := ""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		var rr *resty.Response
 | 
					 | 
				
			||||||
		if rr, err = resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).R().
 | 
					 | 
				
			||||||
			SetContext(ctx).
 | 
					 | 
				
			||||||
			Get(opt.VersionURL); err != nil {
 | 
					 | 
				
			||||||
			err = fmt.Errorf("检查更新失败: %s\n%s", err.Error(), tip)
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		log.Debug("cmd.update: url = %s, raw_response = %s", opt.VersionURL, rr.String())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if rr.StatusCode() != 200 {
 | 
					 | 
				
			||||||
			err = fmt.Errorf("检查更新失败: %s\n%s", rr.Status(), tip)
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		reg := regexp.MustCompile(`const Version = "v\d{2}\.\d{2}\.\d{2}-r\d{1,2}"`)
 | 
					 | 
				
			||||||
		for _, line := range strings.Split(rr.String(), "\n") {
 | 
					 | 
				
			||||||
			if reg.MatchString(line) {
 | 
					 | 
				
			||||||
				version = strings.TrimSpace(strings.TrimPrefix(line, "const Version = "))
 | 
					 | 
				
			||||||
				version = version[1 : len(version)-1]
 | 
					 | 
				
			||||||
				break
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if version == "" {
 | 
					 | 
				
			||||||
			err = fmt.Errorf("检查更新失败: 未找到版本信息\n%s", tip)
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		log.Debug("cmd.update: find version = %s, now_version = %s", version, opt.Version)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if version <= opt.Version {
 | 
					 | 
				
			||||||
			print(fmt.Sprintf("已是最新版本: %s", opt.Version), loading.TypeSuccess)
 | 
					 | 
				
			||||||
			return nil
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		print(fmt.Sprintf("发现新版本: %s", version), loading.TypeInfo)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		print(fmt.Sprintf("正在更新到 %s ...", version))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		time.Sleep(2 * time.Second)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		print("暂时无法自动更新, 请尝试手动更新: go install github.com/loveuer/nf/nft/nfctl@master", loading.TypeWarning)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,19 +0,0 @@
 | 
				
			|||||||
package cmd
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/nfctl/internal/opt"
 | 
					 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func initVersion() *cobra.Command {
 | 
					 | 
				
			||||||
	return &cobra.Command{
 | 
					 | 
				
			||||||
		Use: "version",
 | 
					 | 
				
			||||||
		Run: doVersion,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func doVersion(cmd *cobra.Command, args []string) {
 | 
					 | 
				
			||||||
	fmt.Printf("%s\nnfctl: %s\n\n", opt.Banner, opt.Version)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,26 +0,0 @@
 | 
				
			|||||||
package cmd
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"os"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func Init() {
 | 
					 | 
				
			||||||
	initRoot(
 | 
					 | 
				
			||||||
		initVersion(),
 | 
					 | 
				
			||||||
		initUpdate(),
 | 
					 | 
				
			||||||
		initNew(),
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func Run(ctx context.Context) {
 | 
					 | 
				
			||||||
	if err := rootCmd.ExecuteContext(ctx); err != nil {
 | 
					 | 
				
			||||||
		fmt.Printf("❌ %s\n", err.Error())
 | 
					 | 
				
			||||||
		time.Sleep(300 * time.Millisecond)
 | 
					 | 
				
			||||||
		os.Exit(1)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	time.Sleep(300 * time.Millisecond)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,21 +0,0 @@
 | 
				
			|||||||
package opt
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type _new struct {
 | 
					 | 
				
			||||||
	Template          string
 | 
					 | 
				
			||||||
	DisableInitScript bool
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type config struct {
 | 
					 | 
				
			||||||
	Debug         bool
 | 
					 | 
				
			||||||
	DisableUpdate bool
 | 
					 | 
				
			||||||
	Version       bool
 | 
					 | 
				
			||||||
	New           _new
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var Cfg = &config{}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var TemplateMap = map[string]string{
 | 
					 | 
				
			||||||
	"ultone": "https://gitea.loveuer.com/loveuer/ultone.git",
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const README = "# {{ .project_name }}\n\n### 启动\n- `go run . --help`\n- `go run .`\n\n### 构建\n- `go build -ldflags '-s -w' -o dist/{{ .project_name}}_app .`\n- `docker build -t <image> -f Dockerfile .`"
 | 
					 | 
				
			||||||
@@ -1,13 +0,0 @@
 | 
				
			|||||||
package opt
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const Version = "v24.12.27-r03"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// const VersionURL = "https://github.com/loveuer/nf/nft/nfctl/internal/opt/version.go"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const VersionURL = "https://raw.githubusercontent.com/loveuer/nf/refs/heads/master/nft/nfctl/internal/opt/version.go"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const Banner = `        ___    __  __
 | 
					 | 
				
			||||||
  ___  / _/___/ /_/ /
 | 
					 | 
				
			||||||
 / _ \/ _/ __/ __/ / 
 | 
					 | 
				
			||||||
/_//_/_/ \__/\__/_/  
 | 
					 | 
				
			||||||
                     `
 | 
					 | 
				
			||||||
@@ -1,10 +0,0 @@
 | 
				
			|||||||
package opt
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestBanner(t *testing.T) {
 | 
					 | 
				
			||||||
	fmt.Printf("%s\nnfctl: %s\n\n", Banner, Version)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -2,21 +2,25 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/nfctl/cmd"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/nfctl/version"
 | 
				
			||||||
	"os/signal"
 | 
						"os/signal"
 | 
				
			||||||
	"syscall"
 | 
						"syscall"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf/nft/nfctl/internal/cmd"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					 | 
				
			||||||
	time.Local = time.FixedZone("CST", 8*3600)
 | 
					 | 
				
			||||||
	cmd.Init()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
 | 
						ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
 | 
				
			||||||
	defer cancel()
 | 
						defer cancel()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmd.Run(ctx)
 | 
						version.Check()
 | 
				
			||||||
 | 
						defer version.Fn()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_ = cmd.Root.ExecuteContext(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						select {
 | 
				
			||||||
 | 
						case <-time.After(3 * time.Second):
 | 
				
			||||||
 | 
						case <-ctx.Done():
 | 
				
			||||||
 | 
						case <-version.OkCh:
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								nft/nfctl/opt/var.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								nft/nfctl/opt/var.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					package opt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						Debug bool
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
							
								
								
									
										23
									
								
								nft/nfctl/readme.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								nft/nfctl/readme.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					# nfctl
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 通过 nfctl 快速开启后台项目
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### 1. Installation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- ① `go install github.com/loveuer/nf/nft/nfctl@latest`
 | 
				
			||||||
 | 
					- ② download prebuild binary [release](https://github.com/loveuer/nf/releases)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### 2. Usage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- `nfctl new {project}`
 | 
				
			||||||
 | 
					- `nfctl new project -t ultone`
 | 
				
			||||||
 | 
					- `nfctl new project -t https://github.com/xxx/yyy.git`
 | 
				
			||||||
 | 
					- `nfctl new project --template https://gitcode/loveuer/ultone.git`
 | 
				
			||||||
 | 
					- `nfctl new project --template https://{username}:{password/token}@my.gitlab.com/name/project.git`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### 3. nfctl init script
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- `为方便模版的初始化, 可以采用 nfctl init script, 当 nfctl new project -t xxx 从模版开始项目时会自动执行`
 | 
				
			||||||
 | 
					- `具体的编写规则如下:`
 | 
				
			||||||
 | 
					  * [init 脚本规则](https://github.com/loveuer/nf/nft/nfctl/script.md) 或者
 | 
				
			||||||
 | 
					  * [国内](https://gitcode.com/loveuer/nf/nft/nfctl/script.md)
 | 
				
			||||||
							
								
								
									
										80
									
								
								nft/nfctl/tp/parse.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								nft/nfctl/tp/parse.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
				
			|||||||
 | 
					package tp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ParseCmd(pwd string, content []byte) ([]Cmd, error) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							err   error
 | 
				
			||||||
 | 
							cmds  = make([]Cmd, 0)
 | 
				
			||||||
 | 
							start = false
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						scanner := bufio.NewScanner(bytes.NewReader(content))
 | 
				
			||||||
 | 
						scanner.Buffer(make([]byte, 1024), 1024*1024*10)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						record := make([]string, 0)
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							line := strings.TrimSpace(scanner.Text())
 | 
				
			||||||
 | 
							if len(line) == 0 {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if !start && strings.HasPrefix(line, "#") {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if strings.HasPrefix(line, "!") {
 | 
				
			||||||
 | 
								if start {
 | 
				
			||||||
 | 
									return nil, fmt.Errorf("invalid content: unEOF cmd block found")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								start = true
 | 
				
			||||||
 | 
								record = append(record, line)
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if strings.HasPrefix(line, "EOF") {
 | 
				
			||||||
 | 
								start = false
 | 
				
			||||||
 | 
								if len(record) == 0 {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								var cmd Cmd
 | 
				
			||||||
 | 
								if cmd, err = ParseBlock(pwd, record); err != nil {
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								cmds = append(cmds, cmd)
 | 
				
			||||||
 | 
								record = record[:0]
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if start {
 | 
				
			||||||
 | 
								record = append(record, line)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err = scanner.Err(); err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return cmds, err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ParseBlock(pwd string, lines []string) (Cmd, error) {
 | 
				
			||||||
 | 
						switch lines[0] {
 | 
				
			||||||
 | 
						case "!replace content":
 | 
				
			||||||
 | 
							return newReplaceContent(pwd, lines[1:])
 | 
				
			||||||
 | 
						case "!replace name":
 | 
				
			||||||
 | 
							return newReplaceName(pwd, lines[1:])
 | 
				
			||||||
 | 
						case "!generate":
 | 
				
			||||||
 | 
							return newGenerate(pwd, lines[1:])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil, fmt.Errorf("invalid cmd block: unknown type: %s", lines[0])
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										37
									
								
								nft/nfctl/tp/parse_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								nft/nfctl/tp/parse_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					package tp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestParseInitFile(t *testing.T) {
 | 
				
			||||||
 | 
						bs, err := os.ReadFile("xtest")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatal(err.Error())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data := map[string]any{
 | 
				
			||||||
 | 
							"PROJECT_NAME": "myproject",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := RenderVar(bs, data)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatal(err.Error())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pwd, _ := os.Getwd()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmds, err := ParseCmd(pwd, result)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatal(err.Error())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, item := range cmds {
 | 
				
			||||||
 | 
							log.Info("one cmd => %s\n\n", item.String())
 | 
				
			||||||
 | 
							if err = item.Execute(); err != nil {
 | 
				
			||||||
 | 
								log.Fatal(err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								nft/nfctl/tp/render.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								nft/nfctl/tp/render.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					package tp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"text/template"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						_t *template.Template
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						_t = template.New("tp")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func RenderVar(t []byte, data map[string]any) ([]byte, error) {
 | 
				
			||||||
 | 
						tr, err := _t.Parse(string(t))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var buf bytes.Buffer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err = tr.Execute(&buf, data); err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return buf.Bytes(), nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										316
									
								
								nft/nfctl/tp/tp.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										316
									
								
								nft/nfctl/tp/tp.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,316 @@
 | 
				
			|||||||
 | 
					package tp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/log"
 | 
				
			||||||
 | 
						"io/fs"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Cmd interface {
 | 
				
			||||||
 | 
						String() string
 | 
				
			||||||
 | 
						Execute() error
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						_ Cmd = (*Generate)(nil)
 | 
				
			||||||
 | 
						_ Cmd = (*ReplaceContent)(nil)
 | 
				
			||||||
 | 
						_ Cmd = (*ReplaceName)(nil)
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Generate struct {
 | 
				
			||||||
 | 
						pwd      string
 | 
				
			||||||
 | 
						filename string
 | 
				
			||||||
 | 
						content  []string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Generate) String() string {
 | 
				
			||||||
 | 
						return fmt.Sprintf("!generate\n%s\n%s\n", t.filename, strings.Join(t.content, "\n"))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *Generate) Execute() error {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							err      error
 | 
				
			||||||
 | 
							location = t.filename
 | 
				
			||||||
 | 
							input    *os.File
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Debug("[Generate] generate[%s]", t.filename)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !path.IsAbs(t.filename) {
 | 
				
			||||||
 | 
							location = path.Join(t.pwd, t.filename)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err = os.MkdirAll(path.Dir(location), 0644); err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !strings.HasSuffix(location, "/") {
 | 
				
			||||||
 | 
							if input, err = os.OpenFile(location, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0744); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if len(t.content) > 0 {
 | 
				
			||||||
 | 
								content := strings.Join(t.content, "\n")
 | 
				
			||||||
 | 
								_, err = input.WriteString(content)
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newGenerate(pwd string, lines []string) (*Generate, error) {
 | 
				
			||||||
 | 
						if len(lines) == 0 {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("generate cmd require file/folder name")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &Generate{
 | 
				
			||||||
 | 
							pwd:      pwd,
 | 
				
			||||||
 | 
							filename: lines[0],
 | 
				
			||||||
 | 
							content:  lines[1:],
 | 
				
			||||||
 | 
						}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type replaceNameMatchType int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						replaceNameMatchReg replaceNameMatchType = iota + 1
 | 
				
			||||||
 | 
						replaceNameMatchExact
 | 
				
			||||||
 | 
						replaceNameMatchPrefix
 | 
				
			||||||
 | 
						replaceNameMatchSuffix
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (rm replaceNameMatchType) Label() string {
 | 
				
			||||||
 | 
						switch rm {
 | 
				
			||||||
 | 
						case replaceNameMatchReg:
 | 
				
			||||||
 | 
							return "reg"
 | 
				
			||||||
 | 
						case replaceNameMatchExact:
 | 
				
			||||||
 | 
							return "exact"
 | 
				
			||||||
 | 
						case replaceNameMatchPrefix:
 | 
				
			||||||
 | 
							return "prefix"
 | 
				
			||||||
 | 
						case replaceNameMatchSuffix:
 | 
				
			||||||
 | 
							return "suffix"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Panic("unknown replace match type: %v", rm)
 | 
				
			||||||
 | 
						return ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ReplaceContent struct {
 | 
				
			||||||
 | 
						pwd     string
 | 
				
			||||||
 | 
						name    string
 | 
				
			||||||
 | 
						content string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						targetName    string
 | 
				
			||||||
 | 
						matchType     replaceNameMatchType
 | 
				
			||||||
 | 
						fromContent   string
 | 
				
			||||||
 | 
						targetEmpty   bool
 | 
				
			||||||
 | 
						targetContent string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *ReplaceContent) String() string {
 | 
				
			||||||
 | 
						return fmt.Sprintf("!replace content\n%s\n%s\n", t.name, t.content)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *ReplaceContent) Execute() error {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							fn filepath.WalkFunc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							handler = func(location string) error {
 | 
				
			||||||
 | 
								bs, err := os.ReadFile(location)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								log.Debug("[ReplaceContent] handle[%s] replace [%s] => [%s]", location, t.fromContent, t.targetContent)
 | 
				
			||||||
 | 
								newbs, err := t.executeFile(bs)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return os.WriteFile(location, newbs, 0644)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						switch t.matchType {
 | 
				
			||||||
 | 
						case replaceNameMatchExact:
 | 
				
			||||||
 | 
							fn = func(location string, info fs.FileInfo, err error) error {
 | 
				
			||||||
 | 
								if location == path.Join(t.pwd, t.targetName) {
 | 
				
			||||||
 | 
									log.Debug("[ReplaceContent] exact match: %s", location)
 | 
				
			||||||
 | 
									return handler(location)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						case replaceNameMatchPrefix:
 | 
				
			||||||
 | 
							fn = func(location string, info fs.FileInfo, err error) error {
 | 
				
			||||||
 | 
								if strings.HasPrefix(path.Base(location), t.targetName) {
 | 
				
			||||||
 | 
									log.Debug("[ReplaceContent] prefix match: %s", location)
 | 
				
			||||||
 | 
									return handler(location)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						case replaceNameMatchSuffix:
 | 
				
			||||||
 | 
							fn = func(location string, info fs.FileInfo, err error) error {
 | 
				
			||||||
 | 
								if strings.HasSuffix(location, t.targetName) {
 | 
				
			||||||
 | 
									log.Debug("[ReplaceContent] suffix match: %s", location)
 | 
				
			||||||
 | 
									return handler(location)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						case replaceNameMatchReg:
 | 
				
			||||||
 | 
							fn = func(location string, info fs.FileInfo, err error) error {
 | 
				
			||||||
 | 
								if match, err := regexp.MatchString(t.targetName, location); err == nil && match {
 | 
				
			||||||
 | 
									log.Debug("[ReplaceContent] reg match: %s", location)
 | 
				
			||||||
 | 
									return handler(location)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return filepath.Walk(t.pwd, fn)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *ReplaceContent) executeFile(raw []byte) ([]byte, error) {
 | 
				
			||||||
 | 
						scanner := bufio.NewScanner(bytes.NewReader(raw))
 | 
				
			||||||
 | 
						scanner.Buffer(make([]byte, 1024), 1024*1024)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lines := make([]string, 0)
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							line := scanner.Text()
 | 
				
			||||||
 | 
							lines = append(
 | 
				
			||||||
 | 
								lines,
 | 
				
			||||||
 | 
								strings.ReplaceAll(line, t.fromContent, t.targetContent),
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return []byte(strings.Join(lines, "\n")), nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newReplaceContent(pwd string, lines []string) (*ReplaceContent, error) {
 | 
				
			||||||
 | 
						if len(lines) != 2 {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("invalid replace_content cmd: required 2 lines params")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							name      = lines[0]
 | 
				
			||||||
 | 
							content   = lines[1]
 | 
				
			||||||
 | 
							matchType replaceNameMatchType
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						names := strings.SplitN(name, " ", 2)
 | 
				
			||||||
 | 
						if len(names) != 2 {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("invalid replace_content cmd: name line, required: [reg/exact/prefix/shuffix] {filename}")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						switch names[0] {
 | 
				
			||||||
 | 
						case "exact":
 | 
				
			||||||
 | 
							matchType = replaceNameMatchExact
 | 
				
			||||||
 | 
						case "reg":
 | 
				
			||||||
 | 
							matchType = replaceNameMatchReg
 | 
				
			||||||
 | 
						case "prefix":
 | 
				
			||||||
 | 
							matchType = replaceNameMatchPrefix
 | 
				
			||||||
 | 
						case "suffix":
 | 
				
			||||||
 | 
							matchType = replaceNameMatchSuffix
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("invalid replace_content name match type, example: [reg *.go] [exact go.mod]")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							targetName    string = names[1]
 | 
				
			||||||
 | 
							targetEmpty          = false
 | 
				
			||||||
 | 
							targetContent string
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						contents := strings.SplitN(content, "=>", 2)
 | 
				
			||||||
 | 
						fromContent := strings.TrimSpace(contents[0])
 | 
				
			||||||
 | 
						if len(contents) == 1 {
 | 
				
			||||||
 | 
							targetEmpty = true
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if targetContent = strings.TrimSpace(contents[1]); targetContent == "" || targetContent == `""` || targetContent == `''` {
 | 
				
			||||||
 | 
								targetEmpty = true
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &ReplaceContent{
 | 
				
			||||||
 | 
							pwd:     pwd,
 | 
				
			||||||
 | 
							name:    name,
 | 
				
			||||||
 | 
							content: content,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							matchType:     matchType,
 | 
				
			||||||
 | 
							targetName:    targetName,
 | 
				
			||||||
 | 
							fromContent:   fromContent,
 | 
				
			||||||
 | 
							targetEmpty:   targetEmpty,
 | 
				
			||||||
 | 
							targetContent: targetContent,
 | 
				
			||||||
 | 
						}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ReplaceName struct {
 | 
				
			||||||
 | 
						pwd  string
 | 
				
			||||||
 | 
						line string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						targetEmpty   bool
 | 
				
			||||||
 | 
						fromContent   string
 | 
				
			||||||
 | 
						targetContent string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *ReplaceName) String() string {
 | 
				
			||||||
 | 
						return fmt.Sprintf("!replace name\n%s\n", t.line)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *ReplaceName) Execute() error {
 | 
				
			||||||
 | 
						fullpath := path.Join(t.pwd, t.fromContent)
 | 
				
			||||||
 | 
						if t.targetEmpty {
 | 
				
			||||||
 | 
							return os.RemoveAll(fullpath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ftpath := path.Join(t.pwd, t.targetContent)
 | 
				
			||||||
 | 
						return os.Rename(fullpath, ftpath)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newReplaceName(pwd string, lines []string) (*ReplaceName, error) {
 | 
				
			||||||
 | 
						if len(lines) != 1 {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("replace_name need one line param, for example: mian.go => main.go")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							content       = lines[0]
 | 
				
			||||||
 | 
							targetEmpty   = false
 | 
				
			||||||
 | 
							fromContent   string
 | 
				
			||||||
 | 
							targetContent string
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						contents := strings.SplitN(content, "=>", 2)
 | 
				
			||||||
 | 
						fromContent = strings.TrimSpace(contents[0])
 | 
				
			||||||
 | 
						if len(contents) == 1 {
 | 
				
			||||||
 | 
							targetEmpty = true
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if targetContent = strings.TrimSpace(contents[1]); targetContent == "" || targetContent == `""` || targetContent == `''` {
 | 
				
			||||||
 | 
								targetEmpty = true
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !targetEmpty {
 | 
				
			||||||
 | 
							if (strings.HasPrefix(targetContent, `"`) && strings.HasSuffix(targetContent, `"`)) || (strings.HasPrefix(targetContent, `'`) && strings.HasSuffix(targetContent, `'`)) {
 | 
				
			||||||
 | 
								targetContent = targetContent[1 : len(targetContent)-1]
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &ReplaceName{
 | 
				
			||||||
 | 
							pwd:  pwd,
 | 
				
			||||||
 | 
							line: content,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							targetEmpty:   targetEmpty,
 | 
				
			||||||
 | 
							fromContent:   fromContent,
 | 
				
			||||||
 | 
							targetContent: targetContent,
 | 
				
			||||||
 | 
						}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										68
									
								
								nft/nfctl/version/version.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								nft/nfctl/version/version.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
				
			|||||||
 | 
					package version
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/fatih/color"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf/nft/log"
 | 
				
			||||||
 | 
						"github.com/savioxavier/termlink"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Version = "v24.07.13-r1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						lk      = &sync.Mutex{}
 | 
				
			||||||
 | 
						empty   = func() {}
 | 
				
			||||||
 | 
						upgrade = func(v string) func() {
 | 
				
			||||||
 | 
							return func() {
 | 
				
			||||||
 | 
								color.Green("\n🎉 🎉 🎉 [nfctl] New Version Found: %s", v)
 | 
				
			||||||
 | 
								color.Cyan("Upgrade it with: [go install github.com/loveuer/nf/nft/nfctl@master]")
 | 
				
			||||||
 | 
								fmt.Print("Or Download by: ")
 | 
				
			||||||
 | 
								color.Cyan(termlink.Link("Releases", "https://github.com/loveuer/nf/releases"))
 | 
				
			||||||
 | 
								fmt.Println()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						Fn   = empty
 | 
				
			||||||
 | 
						OkCh = make(chan struct{}, 1)
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Check() {
 | 
				
			||||||
 | 
						ready := make(chan struct{})
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							ready <- struct{}{}
 | 
				
			||||||
 | 
							uri := "https://raw.gitcode.com/loveuer/nf/raw/master/nft/nfctl/version/version.go"
 | 
				
			||||||
 | 
							prefix := "const Version = "
 | 
				
			||||||
 | 
							resp, err := http.Get(uri)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Debug("[Check] http get[%s] err: %v", uri, err.Error())
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							defer resp.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							scanner := bufio.NewScanner(resp.Body)
 | 
				
			||||||
 | 
							scanner.Buffer(make([]byte, 16*1024), 1024*1024)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for scanner.Scan() {
 | 
				
			||||||
 | 
								line := scanner.Text()
 | 
				
			||||||
 | 
								log.Debug("[Check] version.go line: %s", line)
 | 
				
			||||||
 | 
								if strings.HasPrefix(line, prefix) {
 | 
				
			||||||
 | 
									v := strings.TrimPrefix(line, prefix)
 | 
				
			||||||
 | 
									if len(v) > 2 {
 | 
				
			||||||
 | 
										v = v[1 : len(v)-1]
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if v != "" && v > Version {
 | 
				
			||||||
 | 
										lk.Lock()
 | 
				
			||||||
 | 
										Fn = upgrade(v)
 | 
				
			||||||
 | 
										lk.Unlock()
 | 
				
			||||||
 | 
										OkCh <- struct{}{}
 | 
				
			||||||
 | 
										return
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						<-ready
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -2,10 +2,9 @@ package resp
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/loveuer/nf"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/loveuer/nf"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func handleEmptyMsg(status uint32, msg string) string {
 | 
					func handleEmptyMsg(status uint32, msg string) string {
 | 
				
			||||||
@@ -103,18 +102,6 @@ func Resp403(c *nf.Ctx, data any, msgs ...string) error {
 | 
				
			|||||||
	return Resp(c, 403, msg, err, data)
 | 
						return Resp(c, 403, msg, err, data)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Resp418(c *nf.Ctx, data any, msgs ...string) error {
 | 
					 | 
				
			||||||
	msg := MSG418
 | 
					 | 
				
			||||||
	err := ""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(msgs) > 0 && msgs[0] != "" {
 | 
					 | 
				
			||||||
		msg = fmt.Sprintf("%s: %s", msg, strings.Join(msgs, "; "))
 | 
					 | 
				
			||||||
		err = ""
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return Resp(c, 418, msg, err, data)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func Resp429(c *nf.Ctx, data any, msgs ...string) error {
 | 
					func Resp429(c *nf.Ctx, data any, msgs ...string) error {
 | 
				
			||||||
	msg := MSG429
 | 
						msg := MSG429
 | 
				
			||||||
	err := ""
 | 
						err := ""
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,6 @@ const (
 | 
				
			|||||||
	MSG401 = "登录已过期, 请重新登录"
 | 
						MSG401 = "登录已过期, 请重新登录"
 | 
				
			||||||
	MSG403 = "请求权限不足"
 | 
						MSG403 = "请求权限不足"
 | 
				
			||||||
	MSG404 = "请求资源未找到"
 | 
						MSG404 = "请求资源未找到"
 | 
				
			||||||
	MSG418 = "请求条件不满足, 请稍后再试"
 | 
					 | 
				
			||||||
	MSG429 = "请求过于频繁, 请稍后再试"
 | 
						MSG429 = "请求过于频繁, 请稍后再试"
 | 
				
			||||||
	MSG500 = "服务器开小差了, 请稍后再试"
 | 
						MSG500 = "服务器开小差了, 请稍后再试"
 | 
				
			||||||
	MSG501 = "功能开发中, 尽情期待"
 | 
						MSG501 = "功能开发中, 尽情期待"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,34 +0,0 @@
 | 
				
			|||||||
package tool
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func Timeout(seconds ...int) (ctx context.Context) {
 | 
					 | 
				
			||||||
	var duration time.Duration
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(seconds) > 0 && seconds[0] > 0 {
 | 
					 | 
				
			||||||
		duration = time.Duration(seconds[0]) * time.Second
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		duration = time.Duration(30) * time.Second
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	ctx, _ = context.WithTimeout(context.Background(), duration)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TimeoutCtx(ctx context.Context, seconds ...int) context.Context {
 | 
					 | 
				
			||||||
	var duration time.Duration
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(seconds) > 0 && seconds[0] > 0 {
 | 
					 | 
				
			||||||
		duration = time.Duration(seconds[0]) * time.Second
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		duration = time.Duration(30) * time.Second
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	nctx, _ := context.WithTimeout(ctx, duration)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return nctx
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										135
									
								
								readme.md
									
									
									
									
									
								
							
							
						
						
									
										135
									
								
								readme.md
									
									
									
									
									
								
							@@ -5,98 +5,63 @@
 | 
				
			|||||||
##### basic usage
 | 
					##### basic usage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- get param
 | 
					- get param
 | 
				
			||||||
 | 
					```go
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
					    app := nf.New()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ```go
 | 
					    app.Get("/hello/:name", func(c *nf.Ctx) error {
 | 
				
			||||||
  func main() {
 | 
					        name := c.Param("name")
 | 
				
			||||||
      app := nf.New()
 | 
					        return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      app.Get("/hello/:name", func(c *nf.Ctx) error {
 | 
					    log.Fatal(app.Run("0.0.0.0:80"))
 | 
				
			||||||
          name := c.Param("name")
 | 
					}
 | 
				
			||||||
          return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
 | 
					```
 | 
				
			||||||
      })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      log.Fatal(app.Run("0.0.0.0:80"))
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ```
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
- parse request query
 | 
					- parse request query
 | 
				
			||||||
 | 
					```go
 | 
				
			||||||
 | 
					func handleQuery(c *nf.Ctx) error {
 | 
				
			||||||
 | 
					    type Req struct {
 | 
				
			||||||
 | 
					        Name string   `query:"name"`
 | 
				
			||||||
 | 
					        Addr []string `query:"addr"`
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ```go
 | 
					    var (
 | 
				
			||||||
  func handleQuery(c *nf.Ctx) error {
 | 
					        err error
 | 
				
			||||||
      type Req struct {
 | 
					        req = Req{}
 | 
				
			||||||
          Name string   `query:"name"`
 | 
					    )
 | 
				
			||||||
          Addr []string `query:"addr"`
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var (
 | 
					    if err = c.QueryParser(&req); err != nil {
 | 
				
			||||||
          err error
 | 
					        return nf.NewNFError(400, err.Error())
 | 
				
			||||||
          req = Req{}
 | 
						}
 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if err = c.QueryParser(&req); err != nil {
 | 
					    return c.JSON(nf.Map{"query": req})
 | 
				
			||||||
          return nf.NewNFError(400, err.Error())
 | 
					}
 | 
				
			||||||
      }
 | 
					```
 | 
				
			||||||
 | 
					 | 
				
			||||||
      return c.JSON(nf.Map{"query": req})
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ```
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
- parse application/json body
 | 
					- parse application/json body
 | 
				
			||||||
 | 
					```go
 | 
				
			||||||
 | 
					func handlePost(c *nf.Ctx) error {
 | 
				
			||||||
 | 
					    type Req struct {
 | 
				
			||||||
 | 
					        Name string   `json:"name"`
 | 
				
			||||||
 | 
					        Addr []string `json:"addr"`
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ```go
 | 
					    var (
 | 
				
			||||||
  func handlePost(c *nf.Ctx) error {
 | 
					        err error
 | 
				
			||||||
      type Req struct {
 | 
					        req = Req{}
 | 
				
			||||||
          Name string   `json:"name"`
 | 
					        reqMap = make(map[string]interface{})
 | 
				
			||||||
          Addr []string `json:"addr"`
 | 
					    )
 | 
				
			||||||
      }
 | 
						
 | 
				
			||||||
 | 
					    if err = c.BodyParser(&req); err != nil {
 | 
				
			||||||
      var (
 | 
					        return nf.NewNFError(400, err.Error())
 | 
				
			||||||
          err error
 | 
					    }
 | 
				
			||||||
          req = Req{}
 | 
						
 | 
				
			||||||
          reqMap = make(map[string]interface{})
 | 
					    // can parse body multi times
 | 
				
			||||||
      )
 | 
					    if err = c.BodyParser(&reqMap); err != nil {
 | 
				
			||||||
 | 
					        return nf.NewNFError(400, err.Error())
 | 
				
			||||||
      if err = c.BodyParser(&req); err != nil {
 | 
					    }
 | 
				
			||||||
          return nf.NewNFError(400, err.Error())
 | 
						
 | 
				
			||||||
      }
 | 
					    return c.JSON(nf.Map{"struct": req, "map": reqMap})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
      // can parse body multi times
 | 
					```
 | 
				
			||||||
      if err = c.BodyParser(&reqMap); err != nil {
 | 
					 | 
				
			||||||
          return nf.NewNFError(400, err.Error())
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      return c.JSON(nf.Map{"struct": req, "map": reqMap})
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
- pass local value
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  ```go
 | 
					 | 
				
			||||||
  type User struct {
 | 
					 | 
				
			||||||
      Id int
 | 
					 | 
				
			||||||
      Username string
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  func main() {
 | 
					 | 
				
			||||||
      app := nf.New()
 | 
					 | 
				
			||||||
      app.Use(auth())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      app.Get("/item/list", list)
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  func auth() nf.HandlerFunc {
 | 
					 | 
				
			||||||
      return func(c *nf.Ctx) error {
 | 
					 | 
				
			||||||
          c.Locals("user", &User{Id: 1, Username:"user"})
 | 
					 | 
				
			||||||
          return c.Next()
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  func list(c *nf.Ctx) error {
 | 
					 | 
				
			||||||
      user, ok := c.Locals("user").(*User)
 | 
					 | 
				
			||||||
      if !ok {
 | 
					 | 
				
			||||||
          return c.Status(401).SendString("login required")
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      ...
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ```
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user