-
Notifications
You must be signed in to change notification settings - Fork 68
/
joiner.go
52 lines (44 loc) · 915 Bytes
/
joiner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"gopkg.in/cheggaaa/pb.v1"
"github.com/fatih/color"
"io"
"os"
"sort"
)
func JoinFile(files []string, out string) error {
//sort with file name or we will join files with wrong order
sort.Strings(files)
var bar *pb.ProgressBar
if DisplayProgressBar() {
Printf("Start joining \n")
bar = pb.StartNew(len(files)).Prefix(color.CyanString("Joining"))
}
outf, err := os.OpenFile(out, os.O_CREATE|os.O_WRONLY, 0600)
defer outf.Close()
if err != nil {
return err
}
for _, f := range files {
if err = copy(f, outf); err != nil {
return err
}
if DisplayProgressBar() {
bar.Increment()
}
}
if DisplayProgressBar() {
bar.Finish()
}
return nil
}
//this function split just to use defer
func copy(from string, to io.Writer) error {
f, err := os.OpenFile(from, os.O_RDONLY, 0600)
defer f.Close()
if err != nil {
return err
}
io.Copy(to, f)
return nil
}