Skip to content

Commit

Permalink
able to use .surgeignore file
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Sep 23, 2023
1 parent fc846a8 commit 9ac9fe6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 71 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 YieldRay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
117 changes: 47 additions & 70 deletions api/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/fs"
"k8s.io/helm/pkg/ignore"
"net/http"
"os"
"path/filepath"
Expand All @@ -18,17 +19,6 @@ import (
surgeUtils "github.com/yieldray/surgecli/utils"
)

// src 必须为绝对路径!故返回值也是绝对路径
// 返回一个目录的父目录,正斜线分隔路径
// 例如:/a/b 返回 /a,/a/b/ 返回 /a
func toParent(src string) string {
const SEP = string(filepath.Separator)
p := src + SEP
p = filepath.Dir(p)
p = filepath.Dir(p)
return filepath.ToSlash(p) + "/"
}

// client = &http.Client{}
// domain = domainplaceholder.surge.sh
// src = <the directory path>
Expand All @@ -39,12 +29,9 @@ func Upload(client *http.Client, token, domain, src string, onEventStream func(b
}
// 获取绝对路径,保证tar是压缩了一个文件夹而不是其内容(当src为当前目录时)
src, _ = filepath.Abs(src)
// 获取src的父目录路径,写入tar时用绝对路径删除此前缀
parentSrc := toParent(src)

if err != nil {
return err
}
computeTarPath := computeTarPathFn(src)
computeIgnore := computeIgnoreFn(src)

buf := new(bytes.Buffer)
gw := gzip.NewWriter(buf)
Expand All @@ -54,31 +41,19 @@ func Upload(client *http.Client, token, domain, src string, onEventStream func(b
fileCount = 0
projectSize = 0

ignoreList := surgeIgnore(src)

err = filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

// 路径转换为 unix 格式
unixPath := filepath.ToSlash(path)

// 默认跳过文件夹
if info.IsDir() && info.Name() != "." {
defaultIgnoreList := []string{".git", ".*", ".*.*~", "node_modules", "bower_components"}
for _, pattern := range defaultIgnoreList {
matched, _ := filepath.Match(pattern, info.Name())
// fmt.Println(info.Name(), pattern, matched)
if matched {
return filepath.SkipDir
}
}
}
tarPath := computeTarPath(path)
isIgnore := computeIgnore(path, tarPath)

// 自定义文件跳过
if filterForIgnore(ignoreList, unixPath, info) {
return nil
if isIgnore {
if info.IsDir() {
return filepath.SkipDir // skip entire dir
}
return nil // skip this file
}

// 不处理非标准文件
Expand All @@ -98,14 +73,8 @@ func Upload(client *http.Client, token, domain, src string, onEventStream func(b
return err
}

// 写入文件名:dir是一级根目录
name, cut := strings.CutPrefix(unixPath, parentSrc)
hdr.Name = strings.TrimPrefix(name, string(filepath.Separator))
if !cut {
// 正常情况下必定能够移除父目录的路径,否则panic
panic(fmt.Sprintf("path=%s parentPath=%s", unixPath, parentSrc))
// TODO:应该有更好的方法来移除父目录路径
}
// 写入文件名
hdr.Name = tarPath

// 写入文件信息
if err := tw.WriteHeader(hdr); err != nil {
Expand Down Expand Up @@ -193,39 +162,47 @@ func Upload(client *http.Client, token, domain, src string, onEventStream func(b
return nil
}

// 读取目录下的 .surgeignore 文件
func surgeIgnore(src string) []string {
ignoreList := []string{}

p := filepath.Join(src, ".surgeignore")

file, err := os.Open(p)
if err != nil {
return ignoreList
}
defer file.Close()

scanner := bufio.NewScanner(file)
func computeTarPathFn(src string) func(path string) string {
const SEP = string(filepath.Separator)
p := src + SEP
p = filepath.Dir(p)
p = filepath.Dir(p)
p = filepath.ToSlash(p) + "/"

for scanner.Scan() {
ignoreList = append(ignoreList, scanner.Text())
// 移除父路径,仅留下作为tar名称的路径 (Unix格式)
return func(filePath string) string {
unixPath := filepath.ToSlash(filePath)
name, _ := strings.CutPrefix(unixPath, p)
return strings.TrimPrefix(name, string(filepath.Separator))
}

return ignoreList
}

// 返回 ture 则被跳过
func filterForIgnore(ignoreList []string, path string, info fs.FileInfo) bool {
if strings.HasPrefix(info.Name(), ".") {
return true
}
func computeIgnoreFn(src string) func(fullPath, tarPath string) bool {
p := filepath.Join(src, ".surgeignore")
rules, rulesErr := ignore.ParseFile(p)
return func(fullPath, tarPath string) bool {
fileInfo, err := os.Stat(fullPath)
if err != nil {
return true // we can not access the file, so ignore
}

for _, pattern := range ignoreList {
matched, _ := filepath.Match(pattern, info.Name())
if matched {
return true
// 这是surge文档规定的默认ingore列表
defaultIgnoreList := []string{".git", ".*", ".*.*~", "node_modules", "bower_components"}
for _, pattern := range defaultIgnoreList {
matched, _ := filepath.Match(pattern, filepath.Base(fullPath))
if matched {
return true
}
}

if rulesErr != nil {
return false // rules got error, do not ignore
}
return rules.Ignore(removeTopParent(tarPath), fileInfo)
}
}

return false
func removeTopParent(path string) string {
dir := filepath.Dir(path)
return filepath.ToSlash(filepath.Join(filepath.Base(dir), filepath.Base(path)))
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ go 1.20

require (
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d
github.com/urfave/cli/v2 v2.25.3
github.com/urfave/cli/v2 v2.25.7
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
k8s.io/helm v2.17.0+incompatible
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.25.3 h1:VJkt6wvEBOoSjPFQvOkv6iWIrsJyCrKGtCtxXWwmGeY=
github.com/urfave/cli/v2 v2.25.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
k8s.io/helm v2.17.0+incompatible h1:Bpn6o1wKLYqKM3+Osh8e+1/K2g/GsQJ4F4yNF2+deao=
k8s.io/helm v2.17.0+incompatible/go.mod h1:LZzlS4LQBHfciFOurYBFkCMTaZ0D1l+p0teMg7TSULI=

0 comments on commit 9ac9fe6

Please sign in to comment.