-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 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
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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |