Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 0 |
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 | // JoinFile joins seperate chunks of file and forms the final downloaded artifact |
||
12 | func JoinFile(files []string, out string) error { |
||
13 | //sort with file name or we will join files with wrong order |
||
14 | sort.Strings(files) |
||
15 | var bar *pb.ProgressBar |
||
16 | |||
17 | if DisplayProgressBar() { |
||
18 | Printf("Start joining \n") |
||
19 | bar = pb.StartNew(len(files)).Prefix(color.CyanString("Joining")) |
||
20 | } |
||
21 | |||
22 | outf, err := os.OpenFile(out, os.O_CREATE|os.O_WRONLY, 0600) |
||
23 | defer outf.Close() |
||
24 | if err != nil { |
||
25 | return err |
||
26 | } |
||
27 | |||
28 | for _, f := range files { |
||
29 | if err = copy(f, outf); err != nil { |
||
30 | return err |
||
31 | } |
||
32 | if DisplayProgressBar() { |
||
33 | bar.Increment() |
||
34 | } |
||
35 | } |
||
36 | |||
37 | if DisplayProgressBar() { |
||
38 | bar.Finish() |
||
39 | } |
||
40 | |||
41 | return nil |
||
42 | } |
||
43 | |||
44 | //this function split just to use defer |
||
45 | func copy(from string, to io.Writer) error { |
||
46 | f, err := os.OpenFile(from, os.O_RDONLY, 0600) |
||
47 | defer f.Close() |
||
48 | if err != nil { |
||
49 | return err |
||
50 | } |
||
51 | io.Copy(to, f) |
||
52 | return nil |
||
53 | } |
||
54 |