refc: 重构了 nfctl

This commit is contained in:
loveuer
2024-12-26 19:40:39 +08:00
parent d8d771aec6
commit ad6b4fe7b6
27 changed files with 504 additions and 831 deletions

40
nft/tool/clone.go Normal file
View File

@ -0,0 +1,40 @@
package tool
import (
"fmt"
"net/url"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
func Clone(projectDir string, repoURL string) error {
ins, err := url.Parse(repoURL)
if err != nil {
return err
}
uri := fmt.Sprintf("%s://%s%s", ins.Scheme, ins.Host, ins.Path)
opt := &git.CloneOptions{
URL: uri,
Depth: 1,
InsecureSkipTLS: true,
SingleBranch: true,
}
if ins.User != nil {
password, _ := ins.User.Password()
opt.Auth = &http.BasicAuth{
Username: ins.User.Username(),
Password: password,
}
}
_, err = git.PlainClone(projectDir, false, opt)
if err != nil {
return err
}
return nil
}

34
nft/tool/ctx.go Normal file
View File

@ -0,0 +1,34 @@
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
}