Skip to content

Commit

Permalink
feat: support exclude some directories and files #9
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Jun 7, 2020
1 parent 15f8c7b commit 87d9bc8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
10 changes: 4 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ inputs:
bucket:
description: 'specify bucket name of the cloud platform'
required: true
exclude:
description: 'specify exclude the given comma separated directories'
required: false
dist:
description: 'dist'
description: 'specify dist of the local directory'
required: true

runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.driver }}
- ${{ inputs.region }}
- ${{ inputs.bucket }}
- ${{ inputs.dist }}
12 changes: 7 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
uploaderFlagAccessKey = "access_key"
uploaderFlagAccessSecret = "access_secret"
uploaderFlagBucket = "bucket"
uploaderFlagExclude = "exclude"

// uploader environments
uploaderEnvAccessKey = "UPTOC_UPLOADER_AK"
Expand Down Expand Up @@ -62,6 +63,10 @@ var (
Usage: "specify bucket name of the cloud platform",
Required: true,
},
cli.StringFlag{
Name: uploaderFlagExclude,
Usage: "specify exclude the given comma separated directories (example: --exclude=.cache,test)",
},
}
)

Expand All @@ -85,18 +90,15 @@ func action(c *cli.Context) {
accessKey := c.String(uploaderFlagAccessKey)
accessSecret := c.String(uploaderFlagAccessSecret)
bucketName := c.String(uploaderFlagBucket)
exclude := c.String(uploaderFlagExclude)
uploadDriver, err := uploader.New(driver, region, accessKey, accessSecret, bucketName)
if err != nil {
log.Fatalln(err)
}

dirPath := c.Args().First()
if !strings.HasSuffix(dirPath, "/") {
dirPath += "/"
}

e := core.NewEngine(uploadDriver)
if err := e.LoadAndCompareObjects(dirPath); err != nil {
if err := e.LoadAndCompareObjects(dirPath, strings.Split(exclude, ",")...); err != nil {
log.Fatalln(err)
}

Expand Down
26 changes: 22 additions & 4 deletions core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func NewEngine(uploadDriver uploader.Driver) *Engine {
}

// LoadAndCompareObjects loads local files and compare with the remote objects
func (e *Engine) LoadAndCompareObjects(localDir string) error {
localObjects, err := loadLocalObjects(localDir)
func (e *Engine) LoadAndCompareObjects(localDir string, excludePaths ...string) error {
localObjects, err := loadLocalObjects(localDir, excludePaths)
if err != nil {
return err
}
Expand Down Expand Up @@ -108,10 +108,28 @@ func objectNotMatch(object uploader.Object, objects []uploader.Object) bool {
return false
}

func loadLocalObjects(dirPath string) ([]uploader.Object, error) {
func shouldExclude(dirPath, filePath string, excludePaths []string) bool {
for _, ePath := range excludePaths {
if strings.HasPrefix(filePath, dirPath+strings.TrimPrefix(ePath, "/")) {
return true
}
}

return false
}

func loadLocalObjects(dirPath string, excludePaths []string) ([]uploader.Object, error) {
if !strings.HasSuffix(dirPath, "/") {
dirPath += "/"
}

localObjects := make([]uploader.Object, 0)
visitor := func(filePath string, info os.FileInfo, err error) error {
if info.IsDir() {
if os.IsNotExist(err) {
return err
}

if info.IsDir() || shouldExclude(dirPath, filePath, excludePaths) {
return nil
}

Expand Down
18 changes: 14 additions & 4 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -34,16 +35,25 @@ func TestEngine(t *testing.T) {
assert.NoError(t, os.RemoveAll(tmp))
assert.NoError(t, os.Mkdir(tmp, os.FileMode(0755)))
files := map[string]string{
"abc1.txt": "abcabc",
"abc2.txt": "112233",
"abc3.txt": "445566",
"abc1.txt": "abcabc",
"abc2.txt": "112233",
"abc3.txt": "445566",
"exclude/test.txt": "abc123",
}
for name, content := range files {
if strings.HasPrefix(name, "exclude") {
os.Mkdir(tmp+"exclude", os.FileMode(0744))
}
assert.NoError(t, ioutil.WriteFile(tmp+name, []byte(content), os.FileMode(0644)))
}

// test
e := NewEngine(&mockUploader{})
assert.NoError(t, e.LoadAndCompareObjects(tmp))
assert.NoError(t, e.LoadAndCompareObjects(tmp, "/exclude"))
assert.NoError(t, e.Sync())
}

func TestNOTExistDir(t *testing.T) {
e := NewEngine(&mockUploader{})
assert.Error(t, e.LoadAndCompareObjects("tmp233", "/exclude"))
}
9 changes: 8 additions & 1 deletion scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#!/bin/sh -l

uptoc --driver "$1" --region "$2" --bucket "$3" "$4"
ARGS="--driver ${INPUT_DRIVER} --region ${INPUT_REGION} --bucket ${INPUT_BUCKET}"
if [ "$INPUT_EXCLUDE" ]; then
ARGS+=" --exclude $INPUT_EXCLUDE"
fi
ARGS+=" $INPUT_DIST"

# shellcheck disable=SC2086
uptoc ${ARGS}

0 comments on commit 87d9bc8

Please sign in to comment.