Skip to content

Commit

Permalink
Added apply file parser
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Jan 8, 2025
1 parent ada4dd1 commit f55d002
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
66 changes: 66 additions & 0 deletions internal/server/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0

package server

import (
"bufio"
"fmt"
"io"
"os"
"strings"
)

const FILE_SIZE_LIMIT = 50 * 1024 * 1024

func readFileCommands(fileName string) ([]string, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
reader := bufio.NewReader(file)
return readCommands(reader)
}

func readCommands(r io.Reader) ([]string, error) {
ret := []string{}
size := 0

command := strings.Builder{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
size += len(line)
if size > FILE_SIZE_LIMIT {
return nil, fmt.Errorf("file size exceeds limit of %d bytes", FILE_SIZE_LIMIT)
}

line = strings.TrimSpace(line)
if len(line) > 0 && line[0] == '#' {
continue
}

if !strings.HasSuffix(line, "\\") {
command.WriteString(" ")
command.WriteString(line)

commandStr := strings.TrimSpace(command.String())
if commandStr != "" {
ret = append(ret, commandStr)
}
command.Reset()
} else {
command.WriteString(" ")
command.WriteString(line[:len(line)-1])
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
commandStr := strings.TrimSpace(command.String())
if commandStr != "" {
ret = append(ret, commandStr)
}
return ret, nil
}
82 changes: 82 additions & 0 deletions internal/server/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0

package server

import (
"reflect"
"strings"
"testing"
)

func TestReadCommands(t *testing.T) {
tests := []struct {
name string
commands string
want []string
}{
{
name: "empty",
commands: "",
want: []string{},
},
{
name: "simple",
commands: "echo hello\n",
want: []string{"echo hello"},
},
{
name: "comment",
commands: "# comment\n",
want: []string{},
},
{
name: "multi-line",
commands: "echo hello\r\nworld\n",
want: []string{"echo hello", "world"},
},
{
name: "multi-line with comment",
commands: "echo hello\n# comment\nworld\n",
want: []string{"echo hello", "world"},
},
{
name: "multi-line with comment and continuation1",
commands: "echo hello\\\n# comment\nworld\\abc",
want: []string{"echo hello world\\abc"},
},
{
name: "multi-line with comment and continuation2",
commands: `echo hello
# comment
world`,
want: []string{"echo hello", "world"},
},
{
name: "multi-line with comment and continuation3",
commands: "echo hello\\\n\n\nworld\nworld\n",
want: []string{"echo hello", "world", "world"},
},
{
name: "multi-line with comment and continuation4",
commands: "echo hello\\\nworld\nworld\n",
want: []string{"echo hello world", "world"},
},
{
name: "multi-line with comment and continuation5",
commands: "echo hello\\\nworld again\\\nworld\n# comment\n",
want: []string{"echo hello world again world"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readCommands(strings.NewReader(tt.commands))
if err != nil {
t.Errorf("readCommands() error = %+#v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("readCommands() = %+#v, want %+#v", got, tt.want)
}
})
}
}

0 comments on commit f55d002

Please sign in to comment.