From 4c12b035446bf2b673442c06473be6ac9b364ba4 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Sat, 13 Apr 2019 14:56:35 +0200 Subject: [PATCH] toolbox: expand archive/unarchive starting tilde Expand archive sourceDir(s) and unarchive DestDir --- cmd/toolbox/cmd/archive.go | 11 +++++++++++ cmd/toolbox/cmd/unarchive.go | 9 ++++++++- go.mod | 2 +- go.sum | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/toolbox/cmd/archive.go b/cmd/toolbox/cmd/archive.go index 8cb0d92..b304633 100644 --- a/cmd/toolbox/cmd/archive.go +++ b/cmd/toolbox/cmd/archive.go @@ -20,6 +20,7 @@ import ( "log" "os" + "github.com/mitchellh/go-homedir" "github.com/sorintlab/agola/internal/toolbox/archive" "github.com/sorintlab/agola/internal/util" @@ -59,6 +60,16 @@ func archiveRun(cmd *cobra.Command, args []string) { defer out.Close() } + // expand ~ in archiveinfos SourceDir + for i := range a.ArchiveInfos { + exp, err := homedir.Expand(a.ArchiveInfos[i].SourceDir) + if err != nil { + log.Fatalf("failed to expand dir %q: %v", a.ArchiveInfos[i].SourceDir, err) + } + + a.ArchiveInfos[i].SourceDir = exp + } + if err := archive.CreateTar(a.ArchiveInfos, out); err != nil { log.Fatalf("create tar error: %v", err) } diff --git a/cmd/toolbox/cmd/unarchive.go b/cmd/toolbox/cmd/unarchive.go index cde841e..480f113 100644 --- a/cmd/toolbox/cmd/unarchive.go +++ b/cmd/toolbox/cmd/unarchive.go @@ -20,6 +20,7 @@ import ( "log" "os" + "github.com/mitchellh/go-homedir" "github.com/sorintlab/agola/internal/toolbox/unarchive" "github.com/spf13/cobra" @@ -52,9 +53,15 @@ func init() { func unarchiveRun(cmd *cobra.Command, args []string) { flag.Parse() + // expand ~ in destdir + destDir, err := homedir.Expand(unarchiveOpts.destDir) + if err != nil { + log.Fatalf("failed to expand dir %q: %v", unarchiveOpts.destDir, err) + } + br := bufio.NewReader(os.Stdin) - if err := unarchive.Unarchive(br, unarchiveOpts.destDir, unarchiveOpts.overwrite, unarchiveOpts.removeDestDir); err != nil { + if err := unarchive.Unarchive(br, destDir, unarchiveOpts.overwrite, unarchiveOpts.removeDestDir); err != nil { log.Fatalf("untar error: %v", err) } } diff --git a/go.mod b/go.mod index 611dbdf..f2ce41d 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( github.com/mattn/go-sqlite3 v1.10.0 github.com/minio/minio-go v6.0.14+incompatible github.com/mitchellh/copystructure v1.0.0 - github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mitchellh/go-homedir v1.1.0 github.com/opencontainers/go-digest v1.0.0-rc1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/runc v0.1.1 // indirect diff --git a/go.sum b/go.sum index 6009d21..d11497a 100644 --- a/go.sum +++ b/go.sum @@ -124,6 +124,7 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/nwaples/rardecode v1.0.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=