Skip to content

Commit

Permalink
stepwriter: Add DryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Nov 23, 2023
1 parent e711a0a commit a6a64c6
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions installer/stepwriter/dryrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: Fabio Forni <[email protected]>
// SPDX-License-Identifier: MPL-2.0

package stepwriter

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

"github.com/livingsilver94/backee/installer"
"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, src installer.FileCopy) 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:")
err := printLine(buf.String())
if err != nil {
return err
}
_, err = src.WriteTo(os.Stdout)
return err
}

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
}

0 comments on commit a6a64c6

Please sign in to comment.