60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package create
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"errors"
|
|
"os"
|
|
"git.jcg.re/jcgruenhage/borgocli/run/config"
|
|
)
|
|
|
|
func Run(config config.Config) {
|
|
var parameters []string
|
|
parameters = append(parameters, "create")
|
|
|
|
match, err := regexp.MatchString("[0-9]+,[0-9]+,[0-9]+,[0-9]+", config.Data.ChunkerParameters)
|
|
check(err)
|
|
|
|
if match {
|
|
parameters = append(parameters, fmt.Sprintf("--chunker-params %s", config.Data.ChunkerParameters))
|
|
} else if len(config.Data.ChunkerParameters) != 0 {
|
|
panic(errors.New("The chunker parameters are malformed."))
|
|
}
|
|
|
|
match, err = regexp.MatchString("(none)|(lz4)|(zlib(,[0-9])?)|(lzma(,[0-9])?)", config.Data.Compression)
|
|
check(err)
|
|
|
|
if match {
|
|
parameters = append(parameters, fmt.Sprintf("--compression %s", config.Data.Compression))
|
|
} else if len(config.Data.ChunkerParameters) != 0 {
|
|
panic(errors.New("The compression parameter is malformed"))
|
|
}
|
|
|
|
if len(config.Data.Excludes) != 0 {
|
|
for i := 0; i < len(config.Data.Excludes); i++ {
|
|
parameters = append(parameters, fmt.Sprintf("--exclude %s", config.Data.Excludes[i]))
|
|
}
|
|
}
|
|
|
|
parameters = append(parameters, fmt.Sprintf("%s::%s-{now}", config.Data.Repository, config.Data.Hostname))
|
|
parameters = append(parameters, config.Data.SourceDirectories...)
|
|
|
|
if len(config.Data.Passphrase) != 0 {
|
|
os.Setenv("BORG_PASSPHRASE", config.Data.Passphrase)
|
|
}
|
|
|
|
cmd := exec.Command("borg", parameters...)
|
|
|
|
fmt.Print(cmd.Path, cmd.Args)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
func check(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|