Skip to content

Commit

Permalink
installer: Add the DryRun StepWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Nov 21, 2023
1 parent 2c0fd7e commit fccee42
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
14 changes: 5 additions & 9 deletions installer/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"

"github.com/livingsilver94/backee/installer/stepwriter"
"github.com/livingsilver94/backee/repo"
"github.com/livingsilver94/backee/service"
)
Expand All @@ -16,7 +17,7 @@ type StepWriter interface {
Setup(script string) error
InstallPackages(fullCmd []string) error
SymlinkFile(dst service.FilePath, src string) error
CopyFile(dst service.FilePath, src string) error
CopyFile(dst service.FilePath, src stepwriter.FileContent) error
Finalize(script string) error
}

Expand Down Expand Up @@ -84,23 +85,18 @@ func (s Steps) CopyFiles(repo repo.Repo, vars repo.Variables) error {
}
s.log.Info("Copying files")

lnDir, err := repo.DataDir(s.srv.Name)
_, err := repo.DataDir(s.srv.Name)
if err != nil {
return err
}
tmpl := NewTemplate(s.srv.Name, vars)
dest := &strings.Builder{}
for srcFile, dstFile := range s.srv.Copies {
for _, dstFile := range s.srv.Copies {
err := tmpl.ReplaceString(dstFile.Path, dest)
if err != nil {
return err
}
err = s.wri.CopyFile(
service.FilePath{Path: dest.String(), Mode: dstFile.Mode},
filepath.Join(lnDir, srcFile))
if err != nil {
return err
}
// TODO: read file.
dest.Reset()
}
return nil
Expand Down
79 changes: 79 additions & 0 deletions installer/stepwriter/dryrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: Fabio Forni <[email protected]>
// SPDX-License-Identifier: MPL-2.0

package stepwriter

import (
"bufio"
"fmt"
"io"
"strings"

"github.com/livingsilver94/backee/service"
)

var (
buf = &strings.Builder{}
)

type DryRun struct{}

func (DryRun) Setup(script string) error {
return printMultiline(strings.NewReader(script))
}

func (DryRun) InstallPackages(fullCmd []string) error {
return printf("Will run %q", strings.Join(fullCmd, " "))
}

func (DryRun) SymlinkFile(dst service.FilePath, src string) error {
defer buf.Reset()
fmt.Fprintf(buf, "%s → %s", src, dst.Path)
if dst.Mode != 0 {
fmt.Fprintf(buf, " with permission %o", dst.Mode)
}
return printLine(buf.String())
}

func (DryRun) CopyFile(dst service.FilePath, file FileContent) error {
defer buf.Reset()
fmt.Fprintf(buf, "Will write %s", dst.Path)
if dst.Mode != 0 {
fmt.Fprintf(buf, " with permission %o", dst.Mode)
}
buf.WriteString(" with the following content:")
if file.Binary {
buf.WriteString(" *binary*")
return printLine(buf.String())
}
err := printLine(buf.String())
if err != nil {
return err
}
return printMultiline(file.Content)
}

func (DryRun) Finalize(script string) error {
return printMultiline(strings.NewReader(script))
}

func printLine(str string) error {
_, err := fmt.Println("\t" + str)
return err
}

func printMultiline(read io.Reader) error {
lines := bufio.NewScanner(read)
for lines.Scan() {
err := printLine(lines.Text())
if err != nil {
return err
}
}
return nil
}

func printf(format string, args ...any) error {
_, err := fmt.Printf("\t"+format+"\n", args)
return err
}
11 changes: 11 additions & 0 deletions installer/stepwriter/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: Fabio Forni <[email protected]>
// SPDX-License-Identifier: MPL-2.0

package stepwriter

import "io"

type FileContent struct {
Binary bool
Content io.Reader
}

0 comments on commit fccee42

Please sign in to comment.