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