Skip to content

Commit

Permalink
(Closed #15) Added distribute flag
Browse files Browse the repository at this point in the history
  • Loading branch information
hahwul committed Feb 9, 2021
1 parent 4c594db commit d6d7411
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
prefixOption := flag.String("prefix", "", "Prefix string")
suffixOption := flag.String("suffix", "", "Suffix string")
rmnlOption := flag.Bool("rmnl", false, "Remove newline(\\r\\n)")
distributeOption := flag.Bool("distribute", false, "Distribution to files")
flag.Parse()

// Show version
Expand Down Expand Up @@ -47,6 +48,7 @@ func main() {
Prefix: *prefixOption,
Suffix: *suffixOption,
RemoveNewLine: *rmnlOption,
Distribute: *distributeOption,
}

// Running gee app
Expand Down
18 changes: 17 additions & 1 deletion pkg/gee/file.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package gee

import "os"
import (
"os"

model "github.com/hahwul/gee/pkg/model"
printing "github.com/hahwul/gee/pkg/printing"
)

// ClosedFiles is closing list of files
func ClosedFiles(files []*os.File) {
for _, v := range files {
v.Close()
}
}

// WriteFile is write to file
func WriteFile(k *os.File, line string, options model.Options) {
if options.RemoveNewLine {
_, err := k.WriteString(line)
printing.ErrPrint(err)
} else {
_, err := k.WriteString(line + "\r\n")
printing.ErrPrint(err)
}
}
21 changes: 12 additions & 9 deletions pkg/gee/gee.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
// "sync"

model "github.com/hahwul/gee/pkg/model"
printing "github.com/hahwul/gee/pkg/printing"
)

// Gee is running gee
Expand All @@ -17,6 +18,7 @@ func Gee(options model.Options) {
var files = []*os.File{}
stdLine := 1
stdPointer := 1
distributePointer := 0

if options.Append {
mode = os.O_APPEND | os.O_CREATE | os.O_WRONLY
Expand Down Expand Up @@ -55,23 +57,24 @@ func Gee(options model.Options) {
for _, filename := range options.Files {
f, err := os.OpenFile(filename+"_"+strconv.Itoa(stdPointer), mode, 0644)
if err != nil {

printing.ErrPrint(err)
} else {
files = append(files, f)
}
}
stdLine = 1
stdPointer = stdPointer + 1
}
for _, k := range files {
if options.RemoveNewLine {
_, err := k.WriteString(line)
if err != nil {
}
if options.Distribute && (len(files) > 0) {
if distributePointer < len(files) {
WriteFile(files[distributePointer], line, options)
distributePointer = distributePointer + 1
} else {
_, err := k.WriteString(line + "\r\n")
if err != nil {
}
distributePointer = 0
}
} else {
for _, k := range files {
WriteFile(k, line, options)
}
}
stdLine = stdLine + 1
Expand Down
11 changes: 11 additions & 0 deletions pkg/printing/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package printing

// ErrPanic is panic error
func ErrPanic(e error) {

}

// ErrPrint is print error
func ErrPrint(e error) {

}

0 comments on commit d6d7411

Please sign in to comment.