Skip to content

Commit

Permalink
feat(#16): Make output less by default (#19)
Browse files Browse the repository at this point in the history
* feat(#16): Make output level info by default

* chore: Consolidate sort to zip creation
  • Loading branch information
timo-reymann authored May 23, 2023
1 parent 15e9345 commit 7888ccb
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 14 deletions.
7 changes: 2 additions & 5 deletions pkg/features/fileset/directories.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/timo-reymann/deterministic-zip/pkg/file"
"os"
"path/filepath"
"sort"
)

// Directories adds all children folders recursively
Expand All @@ -25,7 +24,6 @@ func (r Directories) IsEnabled(c *cli.Configuration) bool {
// Execute and read all directories recursively and add them back to source files
func (r Directories) Execute(c *cli.Configuration) error {
files := make(map[string]string, 0)
sort.Strings(c.SourceFiles)

for _, f := range c.SourceFiles {
isDir, err := file.IsDir(f)
Expand All @@ -43,7 +41,7 @@ func (r Directories) Execute(c *cli.Configuration) error {
includeParentDirs(&files, f)
}

c.SourceFiles = sortedKeySlice(&files)
c.SourceFiles = keySlice(&files)

return nil
}
Expand All @@ -60,11 +58,10 @@ func includeParentDirs(m *map[string]string, path string) {
}
}

func sortedKeySlice(m *map[string]string) []string {
func keySlice(m *map[string]string) []string {
keys := make([]string, 0, len(*m))
for k := range *m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
3 changes: 3 additions & 0 deletions pkg/features/fileset/directories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fileset
import (
"github.com/timo-reymann/deterministic-zip/pkg/cli"
"reflect"
"sort"
"testing"
)

Expand Down Expand Up @@ -85,6 +86,8 @@ func TestDirectories_Execute(t *testing.T) {
}
}

// Sort before test
sort.Strings(c.SourceFiles)
if !reflect.DeepEqual(tc.sourcesAfter, c.SourceFiles) {
t.Fatalf("Expected %v, but got %v", tc.sourcesAfter, c.SourceFiles)
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/features/fileset/recursive.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/timo-reymann/deterministic-zip/pkg/features/conditions"
"github.com/timo-reymann/deterministic-zip/pkg/file"
"github.com/timo-reymann/deterministic-zip/pkg/output"
"sort"
)

// Recursive adds all children folders recursively
Expand Down Expand Up @@ -47,8 +46,6 @@ func (r Recursive) Execute(c *cli.Configuration) error {
}
}

sort.Strings(files)

c.SourceFiles = files

return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/features/output/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestLogFile_DebugName(t *testing.T) {

func TestLogFile_Execute(t *testing.T) {
lf := LogFile{}
output.SetLevel(output.LevelInfo)
err := lf.Execute(&cli.Configuration{LogFilePath: "/tmp/log"})
if err != nil {
t.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion pkg/features/output/verbose.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ func (v Verbose) IsEnabled(c *cli.Configuration) bool {
// Execute and set level to Debug
func (v Verbose) Execute(c *cli.Configuration) error {
output.SetLevel(output.LevelDebug)
output.Debug("Enable verbose mode")
return nil
}
4 changes: 2 additions & 2 deletions pkg/output/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const LevelInfo = 0
// LevelDebug shows verbose output
const LevelDebug = 1

var level = LevelInfo
var level = LevelSilence

var logFile = ""

Expand Down Expand Up @@ -75,5 +75,5 @@ func Debug(out string) {

// Debugf same as Debug but with format string
func Debugf(out string, vars ...interface{}) {
outputf(LevelDebug, out, vars...)
outputf(LevelDebug, "debug: "+out, vars...)
}
5 changes: 2 additions & 3 deletions pkg/zip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ func createFileName(input string) string {
func Create(c *cli.Configuration, compression uint16) error {
finalName := createFileName(c.ZipFile)

sort.Strings(c.SourceFiles)

newZipFile, err := os.OpenFile(finalName, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
Expand All @@ -42,6 +40,7 @@ func Create(c *cli.Configuration, compression uint16) error {
zipWriter := zip.NewWriter(newZipFile)
registerCompressors(zipWriter)

sort.Strings(c.SourceFiles)
for _, srcFile := range c.SourceFiles {
if err := appendFile(srcFile, zipWriter, compression, conditions.OnFlag(c.Directories)); err != nil {
return err
Expand All @@ -61,7 +60,7 @@ func appendFile(srcFile string, zipWriter *zip.Writer, compression uint16, inclu
output.Infof("Adding %s", srcFile)

f, err := os.Open(srcFile)

// Ensure the open file is always closed, ignoring any errors during the close
defer func(f *os.File) {
if f != nil {
Expand Down

0 comments on commit 7888ccb

Please sign in to comment.