-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
176 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,5 @@ | |
# macOS | ||
.DS_Store | ||
|
||
# dev | ||
bin/* | ||
!bin/.gitkeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
module github.com/frantjc/dockerfile-addendum | ||
|
||
go 1.18 | ||
go 1.19 | ||
|
||
require github.com/spf13/cobra v1.4.0 | ||
require github.com/spf13/cobra v1.6.1 | ||
|
||
require ( | ||
github.com/inconshreveable/mousetrap v1.0.0 // indirect | ||
github.com/inconshreveable/mousetrap v1.0.1 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= | ||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= | ||
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= | ||
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= | ||
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= | ||
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= | ||
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
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.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package command | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/frantjc/dockerfile-addendum" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewRoot() *cobra.Command { | ||
var ( | ||
gz, rm bool | ||
out string | ||
cmd = &cobra.Command{ | ||
Use: "addendum", | ||
Version: addendum.Semver(), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var ( | ||
path = args[0] | ||
ext = filepath.Ext(path) | ||
compressed = gz || strings.EqualFold(ext, ".tgz") || strings.EqualFold(ext, ".gz") || strings.EqualFold(ext, ".tar.gz") | ||
r io.Reader | ||
fi, err = os.Stat(path) | ||
) | ||
switch { | ||
case err != nil: | ||
// tarball doesn't exist, so there's nothing to do | ||
return nil | ||
case fi.IsDir(): | ||
return fmt.Errorf("not a tar archive: %s", fi.Name()) | ||
} | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if compressed { | ||
if r, err = gzip.NewReader(f); err != nil { | ||
return err | ||
} | ||
} else { | ||
r = f | ||
} | ||
|
||
tarball := tar.NewReader(r) | ||
|
||
for { | ||
header, err := tarball.Next() | ||
switch { | ||
case err == io.EOF: | ||
if rm { | ||
return os.Remove(path) | ||
} | ||
|
||
return nil | ||
case err != nil: | ||
return err | ||
} | ||
|
||
fullpath, err := filepath.Abs( | ||
filepath.Join(out, header.Name), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("determine path for tar header: %s", header.Name) | ||
} | ||
|
||
switch header.Typeflag { | ||
case tar.TypeDir: | ||
di, err := os.Stat(fullpath) | ||
switch { | ||
case err == nil && !di.IsDir(): | ||
return fmt.Errorf("not a directory: %s", fullpath) | ||
case err == nil && di.IsDir(): | ||
// nothing to do | ||
default: | ||
if err := os.Mkdir(fullpath, header.FileInfo().Mode().Perm()); err != nil { | ||
return fmt.Errorf("create directory: %s", fullpath) | ||
} | ||
} | ||
case tar.TypeReg: | ||
o, err := os.Create(fullpath) | ||
if err != nil { | ||
return fmt.Errorf("create file: %s", fullpath) | ||
} | ||
defer o.Close() | ||
|
||
if _, err := io.CopyN(o, tarball, header.Size); err != nil { | ||
return fmt.Errorf("write to file: %s", fullpath) | ||
} | ||
default: | ||
return fmt.Errorf("handle tar header type: %b", header.Typeflag) | ||
} | ||
} | ||
}, | ||
Args: cobra.ExactArgs(1), | ||
} | ||
) | ||
|
||
cmd.SetVersionTemplate("{{ .Name }}{{ .Version }} " + runtime.Version() + "\n") | ||
cmd.Flags().BoolVar(&rm, "rm", false, "Remove the tarball after extracting its contents") | ||
cmd.Flags().StringVarP(&out, "out", "o", ".", "Where to extract the tarball's contents to") | ||
cmd.Flags().BoolVar(&gz, "gz", false, "Force assuming the tarball is gzipped") | ||
|
||
return cmd | ||
} |
Oops, something went wrong.