toolbox: expand archive/unarchive starting tilde

Expand archive sourceDir(s) and unarchive DestDir
This commit is contained in:
Simone Gotti 2019-04-13 14:56:35 +02:00
parent 3928851c10
commit 4c12b03544
4 changed files with 21 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import (
"log" "log"
"os" "os"
"github.com/mitchellh/go-homedir"
"github.com/sorintlab/agola/internal/toolbox/archive" "github.com/sorintlab/agola/internal/toolbox/archive"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
@ -59,6 +60,16 @@ func archiveRun(cmd *cobra.Command, args []string) {
defer out.Close() 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 { if err := archive.CreateTar(a.ArchiveInfos, out); err != nil {
log.Fatalf("create tar error: %v", err) log.Fatalf("create tar error: %v", err)
} }

View File

@ -20,6 +20,7 @@ import (
"log" "log"
"os" "os"
"github.com/mitchellh/go-homedir"
"github.com/sorintlab/agola/internal/toolbox/unarchive" "github.com/sorintlab/agola/internal/toolbox/unarchive"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -52,9 +53,15 @@ func init() {
func unarchiveRun(cmd *cobra.Command, args []string) { func unarchiveRun(cmd *cobra.Command, args []string) {
flag.Parse() 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) 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) log.Fatalf("untar error: %v", err)
} }
} }

2
go.mod
View File

@ -30,7 +30,7 @@ require (
github.com/mattn/go-sqlite3 v1.10.0 github.com/mattn/go-sqlite3 v1.10.0
github.com/minio/minio-go v6.0.14+incompatible github.com/minio/minio-go v6.0.14+incompatible
github.com/mitchellh/copystructure v1.0.0 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/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.1 // indirect github.com/opencontainers/runc v0.1.1 // indirect

1
go.sum
View File

@ -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/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 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= 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/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 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=