-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
78 lines (63 loc) · 2.04 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package utils
import (
"fmt"
"path/filepath"
"strings"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/pathutil"
)
func zip(sourceDir, destinationZipPth string) error {
parentDir := filepath.Dir(sourceDir)
dirName := filepath.Base(sourceDir)
cmd := command.New("/usr/bin/zip", "-rTy", destinationZipPth, dirName)
cmd.SetDir(parentDir)
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return fmt.Errorf("Failed to zip dir: %s, output: %s, error: %s", sourceDir, out, err)
}
return nil
}
func exportEnvironmentWithEnvman(keyStr, valueStr string) error {
cmd := command.New("envman", "add", "--key", keyStr)
cmd.SetStdin(strings.NewReader(valueStr))
return cmd.Run()
}
// ExportOutputDir ...
func ExportOutputDir(sourceDirPth, destinationDirPth, envKey string) error {
if sourceDirPth != destinationDirPth {
if err := command.CopyDir(sourceDirPth, destinationDirPth, true); err != nil {
return err
}
}
return exportEnvironmentWithEnvman(envKey, destinationDirPth)
}
// ExportOutputFile ...
func ExportOutputFile(sourcePth, destinationPth, envKey string) error {
if sourcePth != destinationPth {
if err := command.CopyFile(sourcePth, destinationPth); err != nil {
return err
}
}
return exportEnvironmentWithEnvman(envKey, destinationPth)
}
// ExportOutputFileContent ...
func ExportOutputFileContent(content, destinationPth, envKey string) error {
if err := fileutil.WriteStringToFile(destinationPth, content); err != nil {
return err
}
return ExportOutputFile(destinationPth, destinationPth, envKey)
}
// ExportOutputDirAsZip ...
func ExportOutputDirAsZip(sourceDirPth, destinationPth, envKey string) error {
tmpDir, err := pathutil.NormalizedOSTempDirPath("__export_tmp_dir__")
if err != nil {
return err
}
base := filepath.Base(sourceDirPth)
tmpZipFilePth := filepath.Join(tmpDir, base+".zip")
if err := zip(sourceDirPth, tmpZipFilePth); err != nil {
return err
}
return ExportOutputFile(tmpZipFilePth, destinationPth, envKey)
}