tar: read directly from stdin (#1728)
* tar: run directly from stdin * export UnTar function
This commit is contained in:
parent
d2f3e896cd
commit
2ea368dde8
|
|
@ -17,10 +17,9 @@ limitations under the License.
|
||||||
package buildcontext
|
package buildcontext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/gzip"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/constants"
|
"github.com/GoogleContainerTools/kaniko/pkg/constants"
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
||||||
|
|
@ -47,14 +46,15 @@ func (t *Tar) UnpackTarFromBuildContext() (string, error) {
|
||||||
logrus.Infof("To simulate EOF and exit, press 'Ctrl+D'")
|
logrus.Infof("To simulate EOF and exit, press 'Ctrl+D'")
|
||||||
// if launched through docker in interactive mode and without piped data
|
// if launched through docker in interactive mode and without piped data
|
||||||
// process will be stuck here until EOF is sent
|
// process will be stuck here until EOF is sent
|
||||||
data, err := util.GetInputFrom(os.Stdin)
|
gzr, err := gzip.NewReader(os.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "fail to get standard input")
|
return directory, err
|
||||||
}
|
|
||||||
t.context = filepath.Join(directory, constants.ContextTar)
|
|
||||||
if err := ioutil.WriteFile(t.context, data, 0644); err != nil {
|
|
||||||
return "", errors.Wrap(err, "fail to redirect standard input into compressed tar file")
|
|
||||||
}
|
}
|
||||||
|
defer gzr.Close()
|
||||||
|
_, err = util.UnTar(gzr, directory)
|
||||||
|
|
||||||
|
return directory, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return directory, util.UnpackCompressedTar(t.context, directory)
|
return directory, util.UnpackCompressedTar(t.context, directory)
|
||||||
|
|
|
||||||
|
|
@ -261,8 +261,8 @@ func childDirInIgnoreList(path string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// unTar returns a list of files that have been extracted from the tar archive at r to the path at dest
|
// UnTar returns a list of files that have been extracted from the tar archive at r to the path at dest
|
||||||
func unTar(r io.Reader, dest string) ([]string, error) {
|
func UnTar(r io.Reader, dest string) ([]string, error) {
|
||||||
var extractedFiles []string
|
var extractedFiles []string
|
||||||
tr := tar.NewReader(r)
|
tr := tar.NewReader(r)
|
||||||
for {
|
for {
|
||||||
|
|
|
||||||
|
|
@ -626,7 +626,7 @@ func createUncompressedTar(fileContents map[string]string, tarFileName, testDir
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_unTar(t *testing.T) {
|
func Test_UnTar(t *testing.T) {
|
||||||
tcs := []struct {
|
tcs := []struct {
|
||||||
name string
|
name string
|
||||||
setupTarContents map[string]string
|
setupTarContents map[string]string
|
||||||
|
|
@ -671,7 +671,7 @@ func Test_unTar(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fileList, err := unTar(file, tc.destination)
|
fileList, err := UnTar(file, tc.destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -218,7 +218,7 @@ func UnpackLocalTarArchive(path, dest string) ([]string, error) {
|
||||||
return nil, UnpackCompressedTar(path, dest)
|
return nil, UnpackCompressedTar(path, dest)
|
||||||
} else if compressionLevel == archive.Bzip2 {
|
} else if compressionLevel == archive.Bzip2 {
|
||||||
bzr := bzip2.NewReader(file)
|
bzr := bzip2.NewReader(file)
|
||||||
return unTar(bzr, dest)
|
return UnTar(bzr, dest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if fileIsUncompressedTar(path) {
|
if fileIsUncompressedTar(path) {
|
||||||
|
|
@ -227,7 +227,7 @@ func UnpackLocalTarArchive(path, dest string) ([]string, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
return unTar(file, dest)
|
return UnTar(file, dest)
|
||||||
}
|
}
|
||||||
return nil, errors.New("path does not lead to local tar archive")
|
return nil, errors.New("path does not lead to local tar archive")
|
||||||
}
|
}
|
||||||
|
|
@ -286,6 +286,6 @@ func UnpackCompressedTar(path, dir string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer gzr.Close()
|
defer gzr.Close()
|
||||||
_, err = unTar(gzr, dir)
|
_, err = UnTar(gzr, dir)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue