Skip to content

Commit

Permalink
add truncate directive
Browse files Browse the repository at this point in the history
  • Loading branch information
wreulicke committed Dec 11, 2024
1 parent 7028816 commit 319df68
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
commentDirectiveCutStart = "patchr:cut-start"
// end of cut block.
commentDirectiveCutEnd = "patchr:cut-end"
// truncate to the end of file.
commentDirectiveTruncate = "patchr:truncate"
)

var commentDirectives = []patchCommentDirective{
Expand All @@ -43,6 +45,7 @@ var commentDirectives = []patchCommentDirective{
commentDirectiveTemplateEnd,
commentDirectiveCutStart,
commentDirectiveCutEnd,
commentDirectiveTruncate,
}

type Patcher struct {
Expand Down Expand Up @@ -107,6 +110,8 @@ func (p *Patcher) visit(line string, dst *bufio.Writer, scanner *bufio.Scanner,
return p.visitCutStart(line, dst, scanner, data)
case commentDirectiveCutEnd:
return errors.New("unexpected end directive")
case commentDirectiveTruncate:
return p.visitTruncate(line, dst, scanner, data)
}
}
}
Expand Down Expand Up @@ -204,6 +209,18 @@ func (p *Patcher) visitCutStart(line string, dst *bufio.Writer, scanner *bufio.S
return nil
}

func (p *Patcher) visitTruncate(_ string, _ *bufio.Writer, scanner *bufio.Scanner, _ any) error {
for scanner.Scan() {
// skip all lines
}

if err := scanner.Err(); err != nil {
return fmt.Errorf("unexpected scan error: %w", err)
}

return nil
}

func buildDirective(prefix string, directive patchCommentDirective) string {
return prefix + " " + string(directive)
}
Expand Down

0 comments on commit 319df68

Please sign in to comment.