Skip to content

Commit

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

package step

import (
"bufio"
"fmt"
"strings"

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

type DryRun struct{}

func (DryRun) Setup(script string) error {
return printLines(script)
}

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

func (DryRun) SymlinkFile(dst service.FilePath, src string) error {
return printSourceDest(dst, src)
}

func (DryRun) CopyFile(dst service.FilePath, src string) error {
return printSourceDest(dst, src)
}

func (DryRun) Finalize(script string) error {
return printLines(script)
}

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

func printLines(str string) error {
lines := bufio.NewScanner(strings.NewReader(str))
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
}

func printSourceDest(dst service.FilePath, src string) error {
buf := strings.Builder{}
fmt.Fprintf(&buf, "%s → %s", src, dst.Path)
if dst.Mode != 0 {
fmt.Fprintf(&buf, " With permission %o", dst.Mode)
}
return printLine(buf.String())
}
14 changes: 14 additions & 0 deletions installer/step/step.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: Fabio Forni <[email protected]>
// SPDX-License-Identifier: MPL-2.0

package step

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

type Performer interface {
Setup(script string) error
InstallPackages(fullCmd []string) error
SymlinkFile(dst service.FilePath, src string) error
CopyFile(dst service.FilePath, src string) error
Finalize(script string) error
}

0 comments on commit db96813

Please sign in to comment.