Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 16 additions & 71 deletions export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ import (
"io"
"log"
"os"
"path/filepath"

"github.com/bitrise-io/go-utils/v2/archive"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/pathutil"
"github.com/bitrise-io/go-utils/ziputil"
)

const (
filesType = "files"
foldersType = "folders"
mixedFileAndFolderType = "mixed"
)

// Exporter ...
Expand Down Expand Up @@ -64,80 +57,32 @@ func (e *Exporter) ExportOutputFile(key, sourcePath, destinationPath string) err

// ExportOutputFilesZip is a convenience method for creating a ZIP archive from sourcePaths at zipPath and then
// exporting the absolute path of the ZIP with ExportOutput()
func (e *Exporter) ExportOutputFilesZip(key string, sourcePaths []string, zipPath string) error {
tempZipPath, err := zipFilePath()
if err != nil {
return err
func (e *Exporter) ExportOutputFilesZip(key string, sourcePaths []string, zipPath string, flatten bool) error {
for _, path := range sourcePaths {
info, err := os.Stat(path)
if err != nil {
return err
}
if info.IsDir() {
return fmt.Errorf("one of the provided file paths is a directory: %s", path)
}
}

// We have separate zip functions for files and folders and that is the main reason we cannot have mixed
// paths (files and also folders) in the input. It has to be either folders or files. Everything
// else leads to an error.
inputType, err := getInputType(sourcePaths)
err := archive.ZipFromFiles(sourcePaths, zipPath, flatten)
if err != nil {
return err
}
switch inputType {
case filesType:
err = ziputil.ZipFiles(sourcePaths, tempZipPath)
case foldersType:
err = ziputil.ZipDirs(sourcePaths, tempZipPath)
case mixedFileAndFolderType:
return fmt.Errorf("source path list (%s) contains a mix of files and folders", sourcePaths)
default:
return fmt.Errorf("source path list (%s) is empty", sourcePaths)
}

if err != nil {
return err
}

return e.ExportOutputFile(key, tempZipPath, zipPath)
return e.ExportOutput(key, zipPath)
}

func zipFilePath() (string, error) {
tmpDir, err := pathutil.NewPathProvider().CreateTempDir("__export_tmp_dir__")
func (e *Exporter) ExportOutputDirZip(key string, dirPath string, zipPath string) error {
err := archive.ZipFromDir(dirPath, zipPath)
if err != nil {
return "", err
}

return filepath.Join(tmpDir, "temp-zip-file.zip"), nil
}

func getInputType(sourcePths []string) (string, error) {
var folderCount, fileCount int
pathChecker := pathutil.NewPathChecker()

for _, path := range sourcePths {
exist, err := pathChecker.IsDirExists(path)
if err != nil {
return "", err
}

if exist {
folderCount++
continue
}

exist, err = pathChecker.IsPathExists(path)
if err != nil {
return "", err
}

if exist {
fileCount++
}
}

if fileCount == len(sourcePths) {
return filesType, nil
} else if folderCount == len(sourcePths) {
return foldersType, nil
} else if 0 < folderCount && 0 < fileCount {
return mixedFileAndFolderType, nil
return fmt.Errorf("failed to create zip archive from %s: %w", dirPath, err)
}

return "", nil
return e.ExportOutput(key, zipPath)
}

func copyFile(source, destination string) error {
Expand Down
22 changes: 11 additions & 11 deletions export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/env"
pathutil2 "github.com/bitrise-io/go-utils/v2/pathutil"
"github.com/bitrise-io/go-utils/v2/pathutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func TestExportOutputFile(t *testing.T) {
requireEnvmanContainsValueForKey(t, "my_key", destinationPath, envmanStorePath)
}

func TestZipDirectoriesAndExportOutput(t *testing.T) {
func TestExportOutputDirZip(t *testing.T) {
tmpDir := t.TempDir()

envmanStorePath, envmanClearFn := setupEnvman(t)
Expand All @@ -56,28 +56,28 @@ func TestZipDirectoriesAndExportOutput(t *testing.T) {
require.NoError(t, err)
}()

sourceA := filepath.Join(tmpDir, "sourceA")
require.NoError(t, os.MkdirAll(sourceA, 0777))
sourceA := filepath.Join(tmpDir, "sourceA.txt")
require.NoError(t, ioutil.WriteFile(sourceA, []byte("hello"), 0777))

sourceB := filepath.Join(tmpDir, "sourceB")
require.NoError(t, os.MkdirAll(sourceB, 0777))
sourceB := filepath.Join(tmpDir, "sourceB.txt")
require.NoError(t, ioutil.WriteFile(sourceB, []byte(strings.Repeat("bitrise", 10)), 0777))

destinationZip := filepath.Join(tmpDir, "destination.zip")
destinationZip := filepath.Join(t.TempDir(), "destination.zip")

key := "EXPORTED_ZIP_PATH"
e := NewExporter(command.NewFactory(env.NewRepository()))
require.NoError(t, e.ExportOutputFilesZip(key, []string{sourceA, sourceB}, destinationZip))
require.NoError(t, e.ExportOutputDirZip(key, tmpDir, destinationZip))

// destination should exist
exist, err := pathutil2.NewPathChecker().IsPathExists(destinationZip)
exist, err := pathutil.NewPathChecker().IsPathExists(destinationZip)
require.NoError(t, err)
require.Equal(t, true, exist, tmpDir)

// destination should be exported
requireEnvmanContainsValueForKey(t, key, destinationZip, envmanStorePath)
}

func TestZipFilesAndExportOutput(t *testing.T) {
func TestExportOutputFilesZip(t *testing.T) {
tmpDir := t.TempDir()

envmanStorePath, envmanClearFn := setupEnvman(t)
Expand All @@ -104,7 +104,7 @@ func TestZipFilesAndExportOutput(t *testing.T) {
require.NoError(t, e.ExportOutputFilesZip(key, sourceFilePaths, destinationZip))

// destination should exist
exist, err := pathutil2.NewPathChecker().IsPathExists(destinationZip)
exist, err := pathutil.NewPathChecker().IsPathExists(destinationZip)
require.NoError(t, err)
require.Equal(t, true, exist, tmpDir)

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/bitrise-io/go-steputils/v2
go 1.17

require (
github.com/bitrise-io/go-utils v1.0.1
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.13
github.com/bmatcuk/doublestar/v4 v4.2.0
github.com/docker/go-units v0.4.0
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/bitrise-io/go-utils v1.0.1 h1:e7mepVBkVN1DXRPESNXb0djEw6bxB6B93p/Q74zzcvk=
github.com/bitrise-io/go-utils v1.0.1/go.mod h1:ZY1DI+fEpZuFpO9szgDeICM4QbqoWVt0RSY3tRI1heY=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.13 h1:QtAAfm/FpMDv/PnDxgzylVbbSx21pyl7+5T/ToJnWAQ=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.13/go.mod h1:gZWtM7PLn1VOroa4gN1La/24aRVc0jg5R701jTsPaO8=
github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA=
Expand All @@ -16,7 +14,6 @@ github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9n
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
Expand All @@ -30,20 +27,9 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=