-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
installer: Add the DryRun StepWriter
- Loading branch information
1 parent
2c0fd7e
commit fccee42
Showing
3 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// SPDX-FileCopyrightText: Fabio Forni <development@redaril.me> | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-FileCopyrightText: Fabio Forni <development@redaril.me> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package stepwriter | ||
|
||
import "io" | ||
|
||
type FileContent struct { | ||
Binary bool | ||
Content io.Reader | ||
} |