Skip to content

Commit

Permalink
tetragon-rthooks: move code to apply changes
Browse files Browse the repository at this point in the history
Move applyChanges used to patch the containerd configuration file to its
own file. Useful for next commit.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Dec 6, 2024
1 parent f8a58fd commit c55f4a8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 68 deletions.
80 changes: 80 additions & 0 deletions contrib/tetragon-rthooks/cmd/setup/addline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package main

import (
"bufio"
"os"
"strings"

"github.com/pelletier/go-toml"
)

// special line number to append at the end of the file
const appendAtEndLine = -10

type addLine struct {
pos toml.Position
line string
replaceLine bool
}

func applyChanges(fnameIn, fnameOut string, changes []addLine) error {
fIn, err := os.Open(fnameIn)
if err != nil {
return err
}
defer fIn.Close()
cr := "\n"
if usesCR(fIn) {
cr = "\r\n"
}

fOut, err := os.Create(fnameOut)
if err != nil {
return err
}
defer fOut.Close()

inLine := 0
inSc := bufio.NewScanner(fIn)
out := bufio.NewWriter(fOut)
defer out.Flush()
for inSc.Scan() {
inLine++
lines := []string{}
replaceLine := false
for i := range changes {
ch := &changes[i]
if ch.pos.Line == inLine {
line := strings.Repeat(" ", ch.pos.Col-1) + ch.line + cr
lines = append(lines, line)
if ch.replaceLine {
replaceLine = true
}
}
}
if !replaceLine {
out.WriteString(inSc.Text())
out.WriteString(cr)
}
for _, line := range lines {
out.WriteString(line)
}
}

for i := range changes {
ch := &changes[i]
if ch.pos.Line == appendAtEndLine {
indent := ""
if ch.pos.Col > 0 {
indent = strings.Repeat(" ", ch.pos.Col-1)
}
line := indent + ch.line + cr
out.WriteString(line)
}
}

return nil
}
68 changes: 0 additions & 68 deletions contrib/tetragon-rthooks/cmd/setup/patch-containerd-conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"log/slog"
"os"
"strings"

srvconf "github.com/containerd/containerd/services/server/config"
"github.com/pelletier/go-toml"
Expand All @@ -18,14 +17,6 @@ import (
// solution would be to modify the config object and just marshall it instead of just doing text
// replacements. TBD.

const appendAtEndLine = -10

type addLine struct {
pos toml.Position
line string
replaceLine bool
}

type addOCIHookState struct {
cnf *addOCIHookCmd
// poor man's patch
Expand Down Expand Up @@ -141,65 +132,6 @@ func usesCR(f *os.File) bool {
return false
}

func applyChanges(fnameIn, fnameOut string, changes []addLine) error {
fIn, err := os.Open(fnameIn)
if err != nil {
return err
}
defer fIn.Close()
cr := "\n"
if usesCR(fIn) {
cr = "\r\n"
}

fOut, err := os.Create(fnameOut)
if err != nil {
return err
}
defer fOut.Close()

inLine := 0
inSc := bufio.NewScanner(fIn)
out := bufio.NewWriter(fOut)
defer out.Flush()
for inSc.Scan() {
inLine++
lines := []string{}
replaceLine := false
for i := range changes {
ch := &changes[i]
if ch.pos.Line == inLine {
line := strings.Repeat(" ", ch.pos.Col-1) + ch.line + cr
lines = append(lines, line)
if ch.replaceLine {
replaceLine = true
}
}
}
if !replaceLine {
out.WriteString(inSc.Text())
out.WriteString(cr)
}
for _, line := range lines {
out.WriteString(line)
}
}

for i := range changes {
ch := &changes[i]
if ch.pos.Line == appendAtEndLine {
indent := ""
if ch.pos.Col > 0 {
indent = strings.Repeat(" ", ch.pos.Col-1)
}
line := indent + ch.line + cr
out.WriteString(line)
}
}

return nil
}

func (c *addOCIHookCmd) Run(log *slog.Logger) error {
changes, err := addOciHook(log, c)
if err != nil {
Expand Down

0 comments on commit c55f4a8

Please sign in to comment.