Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
frantjc committed Oct 30, 2022
1 parent 0a72763 commit 9a28105
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml → .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: CI
on:
push:
branches:
Expand All @@ -20,4 +21,3 @@ jobs:
push: true
tags: |
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:${{ github.sha }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
# macOS
.DS_Store

# dev
bin/*
!bin/.gitkeep
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
ARG base_image=scratch
ARG build_image=golang:1.18-alpine3.15
ARG build_image=golang:1.19-alpine3.15

FROM ${base_image} AS base_image

FROM ${build_image} AS build_image
ENV CGO_ENABLED=0

FROM build_image AS build
WORKDIR $GOPATH/src/github.com/frantjc/dockerfile-addendum
COPY go.mod go.sum ./
RUN go mod download

FROM build_image AS build
COPY . .
ARG version=0.0.0
ARG prerelease=
Expand Down
38 changes: 19 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
BIN ?= /usr/local/bin

GO ?= go
GIT ?= git
DOCKER ?= docker
GO = go
GIT = git
DOCKER = docker
INSTALL = sudo install

VERSION ?= 0.0.0
PRERELEASE ?= alpha0
Expand All @@ -19,33 +18,34 @@ IMAGE ?= $(REGISTRY)/$(REPOSITORY):$(TAG)

BUILD_ARGS ?= --build-arg version=$(VERSION) --build-arg prerelease=$(PRERELEASE)

INSTALL ?= sudo install
BIN ?= /usr/local/bin

.DEFAULT: install

install: binaries
@$(INSTALL) $(CURDIR)/bin/addendum $(BIN)
install: build
@$(INSTALL) ./bin/addendum $(BIN)

bin bins binaries: addendum

addendum:
@$(GO) build -ldflags "-s -w -X $(MODULE).Version=$(VERSION) -X $(MODULE).Prerelease=$(PRERELEASE)" -o $(CURDIR)/bin $(CURDIR)/cmd/$@
build:
@$(GO) $@ \
-ldflags "\
-s -w \
-X $(MODULE).Version=$(VERSION) \
-X $(MODULE).Prerelease=$(PRERELEASE)\
" \
-o ./bin \
./cmd/addendum

image img:
@$(DOCKER) build -t $(IMAGE) $(BUILD_ARGS) .

fmt vet test:
@$(GO) $@ ./...

tidy vendor verify:
download vendor verify:
@$(GO) mod $@

clean:
@rm -rf bin/*

.PHONY: \
install bin bins binaries addendum \
install \
image img \
fmt vet test \
tidy vendor verify \
clean
vendor verify
120 changes: 14 additions & 106 deletions cmd/addendum/main.go
Original file line number Diff line number Diff line change
@@ -1,121 +1,29 @@
package main

import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"os/signal"
"syscall"

"github.com/frantjc/dockerfile-addendum"
"github.com/spf13/cobra"
"github.com/frantjc/dockerfile-addendum/pkg/command"
)

var (
rootCmd = &cobra.Command{
Use: "addendum",
Version: addendum.Semver(),
RunE: run,
Args: cobra.ExactArgs(1),
}
rm bool
out string
gz bool
)

func init() {
rootCmd.SetVersionTemplate(
fmt.Sprintf("{{ .Name }}{{ .Version }} %s\n", runtime.Version()),
)
rootCmd.Flags().BoolVar(&rm, "rm", false, "Remove the tarball after extracting its contents")
rootCmd.Flags().StringVarP(&out, "out", "o", ".", "Where to extract the tarball's contents to")
rootCmd.Flags().BoolVar(&gz, "gz", false, "Force assuming the tarball is gzipped")
}

func run(cmd *cobra.Command, args []string) error {
func main() {
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)
ctx, cancel = context.WithCancel(context.Background())
sigC = make(chan os.Signal, 1)
err error
)
switch {
case err != nil:
// tarball doesn't exist, so there's nothing to do
return nil
case fi.IsDir():
return fmt.Errorf("directory %s is not a tar archive", fi.Name())
}

f, err := os.Open(path)
if err != nil {
return err
}
signal.Notify(sigC, syscall.SIGINT, syscall.SIGTERM)

if compressed {
r, err = gzip.NewReader(f)
} 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("unable to determine path for tar header %s", header.Name)
}
go func() {
<-sigC
cancel()
}()

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("unable to create directory %s", fullpath)
}
}
case tar.TypeReg:
o, err := os.Create(fullpath)
if err != nil {
return fmt.Errorf("unable to create file %s", fullpath)
}
defer o.Close()

if _, err := io.CopyN(o, tarball, header.Size); err != nil {
return fmt.Errorf("unable to write to file %s", fullpath)
}
default:
return fmt.Errorf("unable to handle tar header type %b", header.Typeflag)
}
}
}

func main() {
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
if err = command.NewRoot().ExecuteContext(ctx); err != nil {
os.Stderr.WriteString(err.Error())
os.Exit(1)
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
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
)
6 changes: 6 additions & 0 deletions go.sum
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=
113 changes: 113 additions & 0 deletions pkg/command/root.go
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
}

0 comments on commit 9a28105

Please sign in to comment.