Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2586386cc3 | |||
| cb53596c46 | |||
| 88ef93e9af | |||
| 610ba20276 | |||
| 1eda2fcc82 |
247
filesystem.go
Normal file
247
filesystem.go
Normal file
@@ -0,0 +1,247 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var filesystem FileSystem = NewRealFileSystem()
|
||||
|
||||
// FileSystem abstracts filesystem operations so we can swap implementations
|
||||
type FileSystem interface {
|
||||
Remove(path string) error
|
||||
RemoveAll(path string) error
|
||||
MkdirAll(path string, perm os.FileMode) error
|
||||
Symlink(source, target string) error
|
||||
Link(source, target string) error
|
||||
RecordLinkAttempt(kind string, source, target string, err error, dryRun bool)
|
||||
SummaryLines() []string
|
||||
SummaryRecords() []operationRecord
|
||||
IsDryRun() bool
|
||||
}
|
||||
|
||||
const (
|
||||
opRemove = "remove"
|
||||
opRemoveAll = "remove_all"
|
||||
opMkdirAll = "mkdir_all"
|
||||
opSymlink = "symlink"
|
||||
opHardlink = "hardlink"
|
||||
)
|
||||
|
||||
type operationRecord struct {
|
||||
kind string
|
||||
source string
|
||||
target string
|
||||
err error
|
||||
dryRun bool
|
||||
}
|
||||
|
||||
func (r operationRecord) summaryLine() (string, bool) {
|
||||
detail := r.detail()
|
||||
if detail != "" {
|
||||
return fmt.Sprintf("%s %s → %s (%s) %s",
|
||||
r.kindLabel(),
|
||||
r.formattedSource(),
|
||||
r.formattedTarget(),
|
||||
r.resultLabel(),
|
||||
detail), true
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s %s → %s (%s)",
|
||||
r.kindLabel(),
|
||||
r.formattedSource(),
|
||||
r.formattedTarget(),
|
||||
r.resultLabel()), true
|
||||
}
|
||||
|
||||
func (r operationRecord) kindLabel() string {
|
||||
switch r.kind {
|
||||
case opSymlink:
|
||||
return "Symlink"
|
||||
case opHardlink:
|
||||
return "Hardlink"
|
||||
case opRemove:
|
||||
return "Remove"
|
||||
case opRemoveAll:
|
||||
return "RemoveAll"
|
||||
case opMkdirAll:
|
||||
return "MkdirAll"
|
||||
default:
|
||||
return r.kind
|
||||
}
|
||||
}
|
||||
|
||||
func (r operationRecord) resultLabel() string {
|
||||
if r.err != nil {
|
||||
return fmt.Sprintf("%sFAIL%s", BRed, Reset)
|
||||
}
|
||||
if r.dryRun {
|
||||
return fmt.Sprintf("%sDRY-RUN%s", BCyan, Reset)
|
||||
}
|
||||
return fmt.Sprintf("%sOK%s", BGreen, Reset)
|
||||
}
|
||||
|
||||
func (r operationRecord) formattedSource() string {
|
||||
if r.source == "" {
|
||||
return "-"
|
||||
}
|
||||
return FormatSourcePath(r.source)
|
||||
}
|
||||
|
||||
func (r operationRecord) formattedTarget() string {
|
||||
if r.target == "" {
|
||||
return "-"
|
||||
}
|
||||
return FormatTargetPath(r.target)
|
||||
}
|
||||
|
||||
func (r operationRecord) detail() string {
|
||||
if r.err != nil {
|
||||
return r.err.Error()
|
||||
}
|
||||
if r.dryRun {
|
||||
return "dry-run"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type baseFileSystem struct {
|
||||
mu sync.Mutex
|
||||
operations []operationRecord
|
||||
}
|
||||
|
||||
func (b *baseFileSystem) addOperation(kind, source, target string, err error, dryRun bool) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.operations = append(b.operations, operationRecord{
|
||||
kind: kind,
|
||||
source: source,
|
||||
target: target,
|
||||
err: err,
|
||||
dryRun: dryRun,
|
||||
})
|
||||
}
|
||||
|
||||
func (b *baseFileSystem) snapshot() []operationRecord {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
ops := make([]operationRecord, len(b.operations))
|
||||
copy(ops, b.operations)
|
||||
return ops
|
||||
}
|
||||
|
||||
func summarizeOperations(records []operationRecord) []string {
|
||||
var lines []string
|
||||
for _, record := range records {
|
||||
if line, ok := record.summaryLine(); ok {
|
||||
lines = append(lines, line)
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
type realFileSystem struct {
|
||||
baseFileSystem
|
||||
}
|
||||
|
||||
// NewRealFileSystem returns a filesystem implementation that writes to disk
|
||||
func NewRealFileSystem() FileSystem {
|
||||
return &realFileSystem{}
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) Remove(path string) error {
|
||||
err := os.Remove(path)
|
||||
fs.addOperation(opRemove, "", path, err, false)
|
||||
return err
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) RemoveAll(path string) error {
|
||||
err := os.RemoveAll(path)
|
||||
fs.addOperation(opRemoveAll, "", path, err, false)
|
||||
return err
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) MkdirAll(path string, perm os.FileMode) error {
|
||||
err := os.MkdirAll(path, perm)
|
||||
fs.addOperation(opMkdirAll, "", path, err, false)
|
||||
return err
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) Symlink(source, target string) error {
|
||||
err := os.Symlink(source, target)
|
||||
fs.RecordLinkAttempt(opSymlink, source, target, err, false)
|
||||
return err
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) Link(source, target string) error {
|
||||
err := os.Link(source, target)
|
||||
fs.RecordLinkAttempt(opHardlink, source, target, err, false)
|
||||
return err
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) RecordLinkAttempt(kind string, source, target string, err error, dryRun bool) {
|
||||
fs.addOperation(kind, source, target, err, dryRun)
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) SummaryLines() []string {
|
||||
return summarizeOperations(fs.snapshot())
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) SummaryRecords() []operationRecord {
|
||||
return fs.snapshot()
|
||||
}
|
||||
|
||||
func (fs *realFileSystem) IsDryRun() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type dryRunFileSystem struct {
|
||||
baseFileSystem
|
||||
}
|
||||
|
||||
// NewDryRunFileSystem returns a filesystem implementation that only records operations
|
||||
func NewDryRunFileSystem() FileSystem {
|
||||
return &dryRunFileSystem{}
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) Remove(path string) error {
|
||||
fs.addOperation(opRemove, "", path, nil, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) RemoveAll(path string) error {
|
||||
fs.addOperation(opRemoveAll, "", path, nil, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) MkdirAll(path string, perm os.FileMode) error {
|
||||
fs.addOperation(opMkdirAll, "", path, nil, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) Symlink(source, target string) error {
|
||||
fs.RecordLinkAttempt(opSymlink, source, target, nil, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) Link(source, target string) error {
|
||||
fs.RecordLinkAttempt(opHardlink, source, target, nil, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) RecordLinkAttempt(kind string, source, target string, err error, dryRun bool) {
|
||||
fs.addOperation(kind, source, target, err, dryRun)
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) SummaryLines() []string {
|
||||
return summarizeOperations(fs.snapshot())
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) SummaryRecords() []operationRecord {
|
||||
return fs.snapshot()
|
||||
}
|
||||
|
||||
func (fs *dryRunFileSystem) IsDryRun() bool {
|
||||
return true
|
||||
}
|
||||
19
filesystem_test.go
Normal file
19
filesystem_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSummaryLineMarksFailuresInRed(t *testing.T) {
|
||||
fs := NewRealFileSystem()
|
||||
|
||||
fs.RecordLinkAttempt(opSymlink, "/tmp/source", "/tmp/target", fmt.Errorf("boom"), false)
|
||||
|
||||
lines := fs.SummaryLines()
|
||||
assert.Equal(t, 1, len(lines))
|
||||
assert.Contains(t, lines[0], BRed+"FAIL"+Reset)
|
||||
assert.Contains(t, lines[0], "boom")
|
||||
}
|
||||
@@ -76,12 +76,17 @@ func (instruction *LinkInstruction) Undo() {
|
||||
|
||||
if isSymlink {
|
||||
LogInfo("Removing symlink at %s", FormatTargetPath(instruction.Target))
|
||||
err = os.Remove(instruction.Target)
|
||||
err = filesystem.Remove(instruction.Target)
|
||||
if err != nil {
|
||||
LogError("could not remove symlink at %s; err: %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
}
|
||||
} else {
|
||||
if filesystem.IsDryRun() {
|
||||
LogInfo("[DRY-RUN] Would remove symlink at %s", FormatTargetPath(instruction.Target))
|
||||
} else {
|
||||
LogSuccess("Removed symlink at %s", FormatTargetPath(instruction.Target))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LogInfo("%s is not a symlink, skipping", FormatTargetPath(instruction.Target))
|
||||
}
|
||||
@@ -172,19 +177,28 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
||||
return
|
||||
}
|
||||
|
||||
linkKind := opSymlink
|
||||
if instruction.Hard {
|
||||
linkKind = opHardlink
|
||||
}
|
||||
reportErr := func(err error) {
|
||||
filesystem.RecordLinkAttempt(linkKind, instruction.Source, instruction.Target, err, filesystem.IsDryRun())
|
||||
status <- err
|
||||
}
|
||||
|
||||
if !FileExists(instruction.Source) {
|
||||
status <- fmt.Errorf("instruction source %s does not exist", FormatSourcePath(instruction.Source))
|
||||
reportErr(fmt.Errorf("instruction source %s does not exist", FormatSourcePath(instruction.Source)))
|
||||
return
|
||||
}
|
||||
|
||||
if instruction.Files {
|
||||
info, err := os.Stat(instruction.Source)
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("could not stat source %s; err: %v", FormatSourcePath(instruction.Source), err)
|
||||
reportErr(fmt.Errorf("could not stat source %s; err: %v", FormatSourcePath(instruction.Source), err))
|
||||
return
|
||||
}
|
||||
if info.IsDir() {
|
||||
status <- fmt.Errorf("source %s is a directory but files=true", FormatSourcePath(instruction.Source))
|
||||
reportErr(fmt.Errorf("source %s is a directory but files=true", FormatSourcePath(instruction.Source)))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -202,31 +216,31 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
||||
if FileExists(instruction.Target) {
|
||||
if instruction.Files {
|
||||
if info, err := os.Stat(instruction.Target); err == nil && info.IsDir() {
|
||||
status <- fmt.Errorf("target %s is a directory but files=true", FormatTargetPath(instruction.Target))
|
||||
reportErr(fmt.Errorf("target %s is a directory but files=true", FormatTargetPath(instruction.Target)))
|
||||
return
|
||||
}
|
||||
}
|
||||
if instruction.Force {
|
||||
isSymlink, err := IsSymlink(instruction.Target)
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("could not determine whether %s is a sym link or not, stopping; err: %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
reportErr(fmt.Errorf("could not determine whether %s is a sym link or not, stopping; err: %v",
|
||||
FormatTargetPath(instruction.Target), err))
|
||||
return
|
||||
}
|
||||
|
||||
if instruction.Hard {
|
||||
info, err := os.Stat(instruction.Target)
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("could not stat %s, stopping; err: %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
reportErr(fmt.Errorf("could not stat %s, stopping; err: %v",
|
||||
FormatTargetPath(instruction.Target), err))
|
||||
return
|
||||
}
|
||||
if info.Mode().IsRegular() && info.Name() == filepath.Base(instruction.Source) {
|
||||
LogTarget("Overwriting existing file %s", instruction.Target)
|
||||
err := os.Remove(instruction.Target)
|
||||
err := filesystem.Remove(instruction.Target)
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("could not remove existing file %s; err: %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
reportErr(fmt.Errorf("could not remove existing file %s; err: %v",
|
||||
FormatTargetPath(instruction.Target), err))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -234,59 +248,73 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
||||
|
||||
if isSymlink {
|
||||
LogTarget("Removing symlink at %s", instruction.Target)
|
||||
err = os.Remove(instruction.Target)
|
||||
err = filesystem.Remove(instruction.Target)
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("failed deleting %s due to %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
reportErr(fmt.Errorf("failed deleting %s due to %v",
|
||||
FormatTargetPath(instruction.Target), err))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if !instruction.Delete {
|
||||
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s",
|
||||
FormatTargetPath(instruction.Target))
|
||||
reportErr(fmt.Errorf("refusing to delte actual (non symlink) file %s",
|
||||
FormatTargetPath(instruction.Target)))
|
||||
return
|
||||
}
|
||||
LogImportant("Deleting (!!!) %s", instruction.Target)
|
||||
err = os.RemoveAll(instruction.Target)
|
||||
err = filesystem.RemoveAll(instruction.Target)
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("failed deleting %s due to %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
reportErr(fmt.Errorf("failed deleting %s due to %v",
|
||||
FormatTargetPath(instruction.Target), err))
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
status <- fmt.Errorf("target %s exists - handle manually or set the 'forced' flag (3rd field)",
|
||||
FormatTargetPath(instruction.Target))
|
||||
reportErr(fmt.Errorf("target %s exists - handle manually or set the 'forced' flag (3rd field)",
|
||||
FormatTargetPath(instruction.Target)))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
targetDir := filepath.Dir(instruction.Target)
|
||||
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(targetDir, 0755)
|
||||
err = filesystem.MkdirAll(targetDir, 0755)
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("failed creating directory %s due to %v",
|
||||
FormatTargetPath(targetDir), err)
|
||||
reportErr(fmt.Errorf("failed creating directory %s due to %v",
|
||||
FormatTargetPath(targetDir), err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
linkType := "symlink"
|
||||
if instruction.Hard {
|
||||
linkType = "hardlink"
|
||||
}
|
||||
|
||||
var err error
|
||||
if instruction.Hard {
|
||||
err = os.Link(instruction.Source, instruction.Target)
|
||||
err = filesystem.Link(instruction.Source, instruction.Target)
|
||||
} else {
|
||||
err = os.Symlink(instruction.Source, instruction.Target)
|
||||
err = filesystem.Symlink(instruction.Source, instruction.Target)
|
||||
}
|
||||
if err != nil {
|
||||
status <- fmt.Errorf("failed creating symlink between %s and %s with error %v",
|
||||
status <- fmt.Errorf("failed creating %s between %s and %s with error %v",
|
||||
linkType,
|
||||
FormatSourcePath(instruction.Source),
|
||||
FormatTargetPath(instruction.Target),
|
||||
err)
|
||||
return
|
||||
}
|
||||
LogSuccess("Created symlink between %s and %s",
|
||||
if filesystem.IsDryRun() {
|
||||
LogInfo("[DRY-RUN] Would create %s between %s and %s",
|
||||
linkType,
|
||||
FormatSourcePath(instruction.Source),
|
||||
FormatTargetPath(instruction.Target))
|
||||
} else {
|
||||
LogSuccess("Created %s between %s and %s",
|
||||
linkType,
|
||||
FormatSourcePath(instruction.Source),
|
||||
FormatTargetPath(instruction.Target))
|
||||
}
|
||||
|
||||
status <- nil
|
||||
}
|
||||
|
||||
@@ -394,6 +394,35 @@ func TestLinkInstruction_RunAsync(t *testing.T) {
|
||||
assert.Contains(t, err.Error(), "files=true")
|
||||
assert.True(t, FileExists(targetDir))
|
||||
})
|
||||
|
||||
t.Run("Dry run does not modify filesystem", func(t *testing.T) {
|
||||
originalFS := filesystem
|
||||
filesystem = NewDryRunFileSystem()
|
||||
defer func() {
|
||||
filesystem = originalFS
|
||||
}()
|
||||
|
||||
target := filepath.Join(testDir, "dryrun-link.txt")
|
||||
instruction := LinkInstruction{
|
||||
Source: srcFile,
|
||||
Target: target,
|
||||
Force: true,
|
||||
Delete: true,
|
||||
}
|
||||
|
||||
status := make(chan error)
|
||||
go instruction.RunAsync(status)
|
||||
|
||||
err := <-status
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, FileExists(target))
|
||||
|
||||
summary := filesystem.SummaryLines()
|
||||
assert.Equal(t, 1, len(summary))
|
||||
assert.Contains(t, summary[0], "DRY-RUN")
|
||||
assert.Contains(t, summary[0], FormatSourcePath(srcFile))
|
||||
assert.Contains(t, summary[0], FormatTargetPath(target))
|
||||
})
|
||||
}
|
||||
|
||||
func TestLinkInstruction_Undo(t *testing.T) {
|
||||
|
||||
140
main.go
140
main.go
@@ -3,11 +3,16 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
utils "git.site.quack-lab.dev/dave/cyutils"
|
||||
)
|
||||
|
||||
@@ -19,19 +24,36 @@ const ImportantColor = BRed
|
||||
const DefaultColor = Reset
|
||||
const PathColor = Green
|
||||
|
||||
var programName = os.Args[0]
|
||||
var undo = false
|
||||
var (
|
||||
programName = os.Args[0]
|
||||
undo = false
|
||||
version = "dev"
|
||||
)
|
||||
|
||||
func main() {
|
||||
recurse := flag.String("r", "", "recurse into directories")
|
||||
file := flag.String("f", "", "file to read instructions from")
|
||||
debug := flag.Bool("d", false, "debug")
|
||||
undoF := flag.Bool("u", false, "undo")
|
||||
versionFlag := flag.Bool("v", false, "print version and exit")
|
||||
dryRun := flag.Bool("n", false, "dry run (no filesystem changes)")
|
||||
flag.Parse()
|
||||
undo = *undoF
|
||||
|
||||
if *versionFlag {
|
||||
fmt.Println(getVersionString())
|
||||
return
|
||||
}
|
||||
|
||||
setupLogging(*debug)
|
||||
|
||||
if *dryRun {
|
||||
filesystem = NewDryRunFileSystem()
|
||||
LogInfo("Dry run mode enabled - no filesystem changes will be made")
|
||||
} else {
|
||||
filesystem = NewRealFileSystem()
|
||||
}
|
||||
|
||||
instructions := make(chan *LinkInstruction, 1000)
|
||||
status := make(chan error)
|
||||
|
||||
@@ -43,9 +65,11 @@ func main() {
|
||||
|
||||
if instructionsDone == 0 {
|
||||
LogInfo("No instructions were processed")
|
||||
printSummary(filesystem)
|
||||
os.Exit(1)
|
||||
}
|
||||
LogInfo("All done")
|
||||
printSummary(filesystem)
|
||||
}
|
||||
|
||||
// setupLogging configures logging based on debug flag
|
||||
@@ -162,6 +186,80 @@ func processInstructions(instructions chan *LinkInstruction) int32 {
|
||||
return instructionsDone
|
||||
}
|
||||
|
||||
func printSummary(fs FileSystem) {
|
||||
records := fs.SummaryRecords()
|
||||
if len(records) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
header := []string{"RESULT", "OPERATION", "SOURCE", "TARGET", "DETAIL"}
|
||||
widths := make([]int, len(header))
|
||||
for i, h := range header {
|
||||
widths[i] = len(h)
|
||||
}
|
||||
|
||||
type row struct {
|
||||
values []string
|
||||
err error
|
||||
}
|
||||
rows := make([]row, len(records))
|
||||
|
||||
for i, record := range records {
|
||||
values := []string{
|
||||
record.resultLabel(),
|
||||
record.kindLabel(),
|
||||
FormatSourcePath(record.source),
|
||||
FormatTargetPath(record.target),
|
||||
record.detail(),
|
||||
}
|
||||
rows[i] = row{values: values, err: record.err}
|
||||
|
||||
for j, val := range values {
|
||||
if l := visibleLength(val); l > widths[j] {
|
||||
widths[j] = l
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LogInfo("Summary:")
|
||||
var lineBuilder strings.Builder
|
||||
|
||||
writeSummaryRow(&lineBuilder, header, widths)
|
||||
for _, line := range strings.Split(strings.TrimRight(lineBuilder.String(), "\n"), "\n") {
|
||||
LogInfo("%s", line)
|
||||
}
|
||||
|
||||
for _, r := range rows {
|
||||
lineBuilder.Reset()
|
||||
writeSummaryRow(&lineBuilder, r.values, widths)
|
||||
line := strings.TrimRight(lineBuilder.String(), "\n")
|
||||
LogInfo("%s", line)
|
||||
}
|
||||
}
|
||||
|
||||
func writeSummaryRow(b *strings.Builder, cols []string, widths []int) {
|
||||
for i, val := range cols {
|
||||
if val == "" {
|
||||
val = "-"
|
||||
}
|
||||
b.WriteString(val)
|
||||
pad := widths[i] - visibleLength(val)
|
||||
if pad < 0 {
|
||||
pad = 0
|
||||
}
|
||||
if i < len(cols)-1 {
|
||||
b.WriteString(strings.Repeat(" ", pad+2))
|
||||
}
|
||||
}
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
|
||||
var ansiRegexp = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
||||
|
||||
func visibleLength(s string) int {
|
||||
return len(ansiRegexp.ReplaceAllString(s, ""))
|
||||
}
|
||||
|
||||
func IsPipeInput() bool {
|
||||
info, err := os.Stdin.Stat()
|
||||
if err != nil {
|
||||
@@ -226,6 +324,44 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
||||
}
|
||||
}
|
||||
|
||||
func getVersionString() string {
|
||||
if version != "" && version != "dev" {
|
||||
return version
|
||||
}
|
||||
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
if info.Main.Version != "" && info.Main.Version != "(devel)" {
|
||||
return info.Main.Version
|
||||
}
|
||||
|
||||
var revision, modified, vcsTime string
|
||||
for _, setting := range info.Settings {
|
||||
switch setting.Key {
|
||||
case "vcs.revision":
|
||||
revision = setting.Value
|
||||
case "vcs.modified":
|
||||
modified = setting.Value
|
||||
case "vcs.time":
|
||||
vcsTime = setting.Value
|
||||
}
|
||||
}
|
||||
if revision != "" {
|
||||
if len(revision) > 7 {
|
||||
revision = revision[:7]
|
||||
}
|
||||
if modified == "true" {
|
||||
revision += "-dirty"
|
||||
}
|
||||
if vcsTime != "" {
|
||||
revision += " (" + vcsTime + ")"
|
||||
}
|
||||
return revision
|
||||
}
|
||||
}
|
||||
|
||||
return "dev"
|
||||
}
|
||||
|
||||
func ReadFromFile(input string, output chan *LinkInstruction, status chan error, doclose bool) {
|
||||
if doclose {
|
||||
defer close(output)
|
||||
|
||||
Reference in New Issue
Block a user