Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Adis Durakovic committed Mar 8, 2024
1 parent 69bb732 commit 339c121
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,8 @@ func NewRestic(errb *bytes.Buffer, outb *bytes.Buffer, settings *Settings) *Rest
return r
}

func (r *Restic) core(
repository Repository,
cmd []string,
envs []string,
ctx *context.Context,
cancel *context.CancelFunc,
ch *chan string,
) (string, error) {

cmds := []string{"-r", repository.Path, "--json"}
cmds = append(cmds, cmd...)
var sout bytes.Buffer
var serr bytes.Buffer
var c *exec.Cmd

if ctx != nil {
c = exec.CommandContext(*ctx, "/usr/bin/restic", cmds...)
if cancel != nil {

defer (*cancel)()
}
} else {
c = exec.Command("/usr/bin/restic", cmds...)
}
func (r *Restic) PipeOutErr(c *exec.Cmd, sout *bytes.Buffer, serr *bytes.Buffer, ch *chan string) {
stdout, err := c.StdoutPipe()
stderr, err := c.StderrPipe()

if err == nil {
go func() {
scanner := bufio.NewScanner(stdout)
Expand All @@ -68,6 +43,13 @@ func (r *Restic) core(
sout.WriteString(scanner.Text())
}
}()

}

stderr, err := c.StderrPipe()

if err == nil {

go func() {
scanner := bufio.NewScanner(stderr)
scanner.Split(bufio.ScanLines)
Expand All @@ -82,7 +64,9 @@ func (r *Restic) core(
}
}()
}
}

func (r *Restic) getEnvs(repository Repository, envs []string) []string {
envs = append(
envs,
[]string{"RESTIC_PASSWORD=" + repository.Password, "RESTIC_PROGRESS_FPS=5"}...)
Expand All @@ -94,7 +78,6 @@ func (r *Restic) core(
"AWS_SECRET_ACCESS_KEY=" + repository.Options.S3Secret,
}...)
}

if repository.Type == "azure" {
envs = append(
envs,
Expand All @@ -113,15 +96,46 @@ func (r *Restic) core(
"GOOGLE_APPLICATION_CREDENTIALS=" + repository.Options.GoogleApplicationCredentials,
}...)
}
return envs

}

func (r *Restic) core(
repository Repository,
cmd []string,
envs []string,
ctx *context.Context,
cancel *context.CancelFunc,
ch *chan string,
) (string, error) {

cmds := []string{"-r", repository.Path, "--json"}
cmds = append(cmds, cmd...)
var sout bytes.Buffer
var serr bytes.Buffer
var c *exec.Cmd

if ctx != nil {
c = exec.CommandContext(*ctx, "/usr/bin/restic", cmds...)
if cancel != nil {

defer (*cancel)()
}
} else {
c = exec.Command("/usr/bin/restic", cmds...)
}

r.PipeOutErr(c, &sout, &serr, ch)

envs = r.getEnvs(repository, envs)
log.Info("core", "repo", repository.Path, "cmd", cmd, "envs", envs)

c.Env = append(
os.Environ(),
envs...,
)

err = c.Start()
err := c.Start()
if err != nil {
log.Error("executing restic command", "err", err)
}
Expand Down

0 comments on commit 339c121

Please sign in to comment.