diff --git a/action.yml b/action.yml index 8bc8d3f..3d6befa 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/cmd/main.go b/cmd/main.go index caaed53..86a74fc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,6 +20,7 @@ const ( uploaderFlagAccessKey = "access_key" uploaderFlagAccessSecret = "access_secret" uploaderFlagBucket = "bucket" + uploaderFlagExclude = "exclude" // uploader environments uploaderEnvAccessKey = "UPTOC_UPLOADER_AK" @@ -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)", + }, } ) @@ -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) } diff --git a/core/engine.go b/core/engine.go index c048a2a..07e014c 100644 --- a/core/engine.go +++ b/core/engine.go @@ -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 } @@ -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 } diff --git a/core/engine_test.go b/core/engine_test.go index b47e5ad..a97f829 100644 --- a/core/engine_test.go +++ b/core/engine_test.go @@ -3,6 +3,7 @@ package core import ( "io/ioutil" "os" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -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")) +} diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 86c56c8..e1b3ce4 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -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}