This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker.go
61 lines (52 loc) · 1.66 KB
/
packer.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
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"github.com/metakeule/fmtdate"
"log"
"os"
"os/exec"
"time"
)
func MakePack(files_path string, rar bool, local bool) (pack_name string) {
if files_path != "" {
packerScriptCreator(files_path, rar, local)
}
_, err := os.Open("packer")
if err == nil {
cmd := exec.Command("/bin/sh", "packer")
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run(packer) failed with %s\n", err)
}
} else {
log.Fatalf("packer script can't be found and you didn't get parameters to create new one!")
}
dt := time.Now()
//fmtdate package make normal date form that is tha same with bash date form
if !rar {
pack_name = fmt.Sprintf("%v.7z", fmtdate.FormatDate(dt))
} else {
pack_name = fmt.Sprintf("%v.rar", fmtdate.FormatDate(dt))
}
return pack_name
}
func packerScriptCreator(files_path string, rar bool, local bool) {
packer_script, err := os.Create("packer")
if err != nil {
log.Fatalf("File can't be created; %s\n", err)
}
defer packer_script.Close()
//pack to .tar archive and than to .7z or .rar archive. Why? Go to Google and read about .tar format
if !rar {
_, err = packer_script.WriteString(fmt.Sprintf("#!/bin/bash\nyear=$(date +'%%Y')\nmonth=$(date +'%%m')\nday=$(date +'%%d')\ntar -cf $year-$month-$day.tar %v\n7z a $year-$month-$day.7z $year-$month-$day.tar", files_path))
if err != nil {
log.Fatalf("File can't be writed; %s\n", err)
}
} else {
_, err = packer_script.WriteString(fmt.Sprintf("#!/bin/bash\nyear=$(date +'%%Y')\nmonth=$(date +'%%m')\nday=$(date +'%%d')\nrar a -r $year-$month-$day.rar %v", files_path))
if err != nil {
log.Fatalf("File can't be writed; %s\n", err)
}
}
packer_script.Sync()
}