Skip to content

Commit

Permalink
nested write
Browse files Browse the repository at this point in the history
  • Loading branch information
enr committed Feb 28, 2021
1 parent 78d7be0 commit a349266
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .sdlc/check
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ for dir in $(find "$LIB_DIR" -mindepth 1 -maxdepth 1 -type d); do
goimports -w .
diff <(goimports -d .) <(printf "")
echo "--- gocyclo"
diff <(gocyclo -over 10 .) <(printf "")
diff <(gocyclo -over 15 .) <(printf "")
cd "$dir"
echo "--- go test -cover ./..."
go test -mod vendor -cover ./...
Expand Down
10 changes: 1 addition & 9 deletions cmd/zipts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path"
"path/filepath"
"strings"
"time"

"github.com/enr/clui"
"github.com/enr/go-files/files"
Expand Down Expand Up @@ -91,7 +90,7 @@ func run(c runConfig, showHelp func()) int {
}
}
outputDirectory := c.OutputDir
suffix := timestamp()
suffix := core.Timestamp()
targetFilePath, err := resolveOutputPath(inputDirPath, suffix, outputDirectory)
if noop {
ui.Lifecyclef("Output file: %s", targetFilePath)
Expand Down Expand Up @@ -140,13 +139,6 @@ func main() {
app.Run(os.Args)
}

func timestamp() string {
const layout = "20060102150405"
t := time.Now()
ts := t.Local().Format(layout)
return ts
}

func resolveInputPath(arg string) (string, error) {
inputDirPath := strings.TrimSpace(arg)
if inputDirPath == "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/zipts/zipts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func getCurrentDirectory() string {

func TestRun(t *testing.T) {
runConfig := runConfig{
Args: []string{"notextists"},
Args: []string{"notexists"},
Noop: false,
}
showHelp := func() {
Expand All @@ -42,7 +42,7 @@ func TestRun(t *testing.T) {

func TestRunNoop(t *testing.T) {
runConfig := runConfig{
Args: []string{"notextists"},
Args: []string{"notexists"},
Noop: true,
}
showHelp := func() {
Expand Down
142 changes: 71 additions & 71 deletions cmd/zipw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ func main() {
runApp(os.Args)
}

type commandParams struct {
FileToAdd string `yaml:"file"`
InnerPath string `yaml:"inner"`
ZipPath string `yaml:"zip"`
}

type step struct {
path string
expanded string
Expand All @@ -73,83 +67,89 @@ func mainAction(c *cli.Context) error {
}
ui.Confidentialf("Adding file=%s to zip=%s in path inner=%s", params.FileToAdd, params.ZipPath, params.InnerPath)

if !files.Exists(params.FileToAdd) {
return exitErrorf(1, `Invalid path for the file to add: "%s". Exit`, params.FileToAdd)
zipWriter := core.NewZipWriter(ui)
err = zipWriter.Write(params)
if err != nil {
return exitErrorf(1, "Error processing %s: %s", params.ZipPath, err.Error())
}
/*
if !files.Exists(params.FileToAdd) {
return exitErrorf(1, `Invalid path for the file to add: "%s". Exit`, params.FileToAdd)
}
tmps := []string{}
defer func() {
for _, t := range tmps {
if t != "" {
ui.Confidentialf("Cleanup temporary dir: %s", t)
os.RemoveAll(t)
tmps := []string{}
defer func() {
for _, t := range tmps {
if t != "" {
ui.Confidentialf("Cleanup temporary dir: %s", t)
os.RemoveAll(t)
}
}
}
}()

var lastExpanded string
var lastPath string
steps := []step{}
tokens := []string{params.ZipPath}
tokens = append(tokens, strings.Split(params.InnerPath, `#`)...)
for i, v := range tokens {
e := step{}
e.destDir = lastExpanded
if i == len(tokens)-1 {
e.destination = lastPath
e.path = params.FileToAdd
e.innerPath = v
}()
var lastExpanded string
var lastPath string
steps := []step{}
tokens := []string{params.ZipPath}
tokens = append(tokens, strings.Split(params.InnerPath, `#`)...)
for i, v := range tokens {
e := step{}
e.destDir = lastExpanded
if i == len(tokens)-1 {
e.destination = lastPath
e.path = params.FileToAdd
e.innerPath = v
steps = append(steps, e)
break
} else if i == 0 {
e.path = params.ZipPath
e.destination = params.ZipPath
e.innerPath = filepath.Base(params.ZipPath)
e.destDir = filepath.Dir(params.ZipPath)
} else {
e.destination = lastPath
e.path = filepath.Join(lastExpanded, v)
e.innerPath = v
}
lastPath = e.path
lastExpanded, err = extractToTmp(e.path)
if err != nil {
ui.Errorf(`%s error extracting %s`, v, e.path)
break
}
tmps = append(tmps, lastExpanded)
ui.Confidentialf("Extracted %s to %s", e.path, lastExpanded)
e.expanded = lastExpanded
steps = append(steps, e)
break
} else if i == 0 {
e.path = params.ZipPath
e.destination = params.ZipPath
e.innerPath = filepath.Base(params.ZipPath)
e.destDir = filepath.Dir(params.ZipPath)
} else {
e.destination = lastPath
e.path = filepath.Join(lastExpanded, v)
e.innerPath = v
}
lastPath = e.path
lastExpanded, err = extractToTmp(e.path)
if err != nil {
ui.Errorf(`%s error extracting %s`, v, e.path)
break
return exitErrorf(1, "Error processing %s: %s", params.ZipPath, err.Error())
}
tmps = append(tmps, lastExpanded)
ui.Confidentialf("Extracted %s to %s", e.path, lastExpanded)
e.expanded = lastExpanded
steps = append(steps, e)
}
if err != nil {
return exitErrorf(1, "Error processing %s: %s", params.ZipPath, err.Error())
}
var s step
l := len(steps)
first := true
for i := l - 1; i > 0; i-- {
s = steps[i]
if first {
ui.Confidentialf("Copy %s in dir %s as %s", s.path, s.destDir, s.innerPath)
err = addFileToTmp(s.path, s.destDir, s.innerPath)
var s step
l := len(steps)
first := true
for i := l - 1; i > 0; i-- {
s = steps[i]
if first {
ui.Confidentialf("Copy %s in dir %s as %s", s.path, s.destDir, s.innerPath)
err = addFileToTmp(s.path, s.destDir, s.innerPath)
if err != nil {
ui.Errorf(`Error copying %s in dir %s as %s: %v`, s.path, s.destDir, s.innerPath, err)
break
}
first = false
}
err = zipTmp(s.destDir, s.destination)
if err != nil {
ui.Errorf(`Error copying %s in dir %s as %s: %v`, s.path, s.destDir, s.innerPath, err)
ui.Errorf(`Error zipping %s to %s: %v`, s.destDir, s.destination, err)
break
}
first = false
}
err = zipTmp(s.destDir, s.destination)
if err != nil {
ui.Errorf(`Error zipping %s to %s: %v`, s.destDir, s.destination, err)
break
return exitErrorf(1, "Error processing %s: %s", params.FileToAdd, err.Error())
}
}
if err != nil {
return exitErrorf(1, "Error processing %s: %s", params.FileToAdd, err.Error())
}

*/
return nil
}

Expand All @@ -158,8 +158,8 @@ func exitErrorf(exitCode int, template string, args ...interface{}) error {
return cli.NewExitError(fmt.Sprintf(template, args...), exitCode)
}

func loadParams(c *cli.Context) (commandParams, error) {
params := commandParams{}
func loadParams(c *cli.Context) (core.WriterRequest, error) {
params := core.WriterRequest{}

fileToAdd := c.String("file")
innerPath := c.String("inner")
Expand Down
4 changes: 3 additions & 1 deletion e2e/zipts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ specs:
working_dir: ..
exe: bin/zipts
args:
- "testdata/"
- "--out"
- "test/work"
- "test/data/01"
ext:
windows: .exe
expectations:
Expand Down
10 changes: 10 additions & 0 deletions lib/core/timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core

import "time"

func Timestamp() string {
const layout = "20060102150405"
t := time.Now()
ts := t.Local().Format(layout)
return ts
}
Loading

0 comments on commit a349266

Please sign in to comment.