main.JoinFile   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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