update 'buildfile' using 'instruction'
This commit is contained in:
parent
1577808844
commit
f7d930b588
|
@ -1,13 +1,9 @@
|
|||
package buildfile
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var ErrInvalidSyntax = errors.New("invalid instruction format")
|
||||
var ErrUnknownInstruction = errors.New("unknown instruction")
|
||||
|
||||
type LineError struct {
|
||||
Line int
|
||||
Err error
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
package buildfile
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type instruction_type byte
|
||||
|
||||
const (
|
||||
// INSTALL one or more package(s)
|
||||
INSTALL instruction_type = iota
|
||||
// UPDATE all installed packages candidates
|
||||
UPDATE
|
||||
// REMOVE one or more package(s)
|
||||
REMOVE
|
||||
// SERVICE management (enable, disable, start, stop, restart, reload)
|
||||
SERVICE
|
||||
// CONF edits configuration files
|
||||
CONF
|
||||
)
|
||||
|
||||
// instruction is self-explanatory
|
||||
type instruction struct {
|
||||
Type instruction_type
|
||||
// Sub command instruction-type-specific
|
||||
Sub []string
|
||||
Args string
|
||||
}
|
||||
|
||||
// createInstruction from a raw line
|
||||
func createInstruction(raw string) (*instruction, error) {
|
||||
|
||||
// 1. fail if invalid base syntax 'cmd args...'
|
||||
// where command is 3 letters
|
||||
if len(raw) < 5 || len(strings.Trim(raw[0:3], " ")) < 3 || raw[1] == ' ' || raw[3] != ' ' {
|
||||
return nil, ErrInvalidSyntax
|
||||
}
|
||||
|
||||
// 2. prepare instruction
|
||||
inst := &instruction{
|
||||
Type: INSTALL,
|
||||
Sub: make([]string, 0),
|
||||
Args: raw[4:],
|
||||
}
|
||||
|
||||
// 3. Extract instruction type
|
||||
switch raw[0:3] {
|
||||
case "ins":
|
||||
inst.Type = INSTALL
|
||||
case "upd":
|
||||
inst.Type = UPDATE
|
||||
case "del":
|
||||
inst.Type = REMOVE
|
||||
case "ser":
|
||||
inst.Type = SERVICE
|
||||
case "cnf":
|
||||
inst.Type = CONF
|
||||
default:
|
||||
return nil, ErrUnknownInstruction
|
||||
}
|
||||
|
||||
return inst, nil
|
||||
}
|
|
@ -2,15 +2,15 @@ package buildfile
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Reader is the buildfile reader
|
||||
type Reader struct {
|
||||
// linux distribution to run on
|
||||
distribution string
|
||||
Content []*instruction
|
||||
Content []instruction.T
|
||||
}
|
||||
|
||||
// NewReader creates a new reader for the specified build file and linux distribution
|
||||
|
@ -18,27 +18,29 @@ func NewReader(distro string, buildfile io.Reader) (*Reader, error) {
|
|||
|
||||
r := &Reader{
|
||||
distribution: distro,
|
||||
Content: make([]*instruction, 0),
|
||||
Content: make([]instruction.T, 0),
|
||||
}
|
||||
|
||||
// read each line
|
||||
// add each line as instruction
|
||||
l, reader := 0, bufio.NewReader(buildfile)
|
||||
for {
|
||||
l++
|
||||
|
||||
// read line until end
|
||||
line, err := reader.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, LineError{l, err}
|
||||
}
|
||||
line = strings.Trim(line, " \t\n")
|
||||
|
||||
// get and add instruction to list
|
||||
inst, err := createInstruction(line)
|
||||
// turn into instruction
|
||||
inst, err := instruction.Parse(line)
|
||||
if err != nil {
|
||||
return nil, LineError{l, err}
|
||||
}
|
||||
|
||||
// add to list
|
||||
r.Content = append(r.Content, inst)
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package buildfile
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -11,26 +12,26 @@ func TestInstructionSyntax(t *testing.T) {
|
|||
Line int
|
||||
Err error
|
||||
}{
|
||||
{"ins args\ncnf args\n", 0, nil},
|
||||
{" ins args\ncnf args\n", 0, nil},
|
||||
{"\tins args\ncnf args\n", 0, nil},
|
||||
{" \t ins args\ncnf args\n", 0, nil},
|
||||
{" \t ins args\ncnf args\n", 0, nil},
|
||||
{"ins args\ndel args\n", 0, nil},
|
||||
{" ins args\ndel args\n", 0, nil},
|
||||
{"\tins args\ndel args\n", 0, nil},
|
||||
{" \t ins args\ndel args\n", 0, nil},
|
||||
{" \t ins args\ndel args\n", 0, nil},
|
||||
|
||||
{"cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
||||
{"ins args\ncmd args\n", 2, ErrUnknownInstruction},
|
||||
{" cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
||||
{"\tcmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
||||
{" \t cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
||||
{" \t cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
||||
{"cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
|
||||
{"ins args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
|
||||
{" cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
|
||||
{"\tcmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
|
||||
{" \t cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
|
||||
{" \t cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
|
||||
|
||||
{" md args\ncmd args\n", 1, ErrInvalidSyntax},
|
||||
{"c d args\ncmd args\n", 1, ErrInvalidSyntax},
|
||||
{"cm args\ncmd args\n", 1, ErrInvalidSyntax},
|
||||
{" md args\ncmd args\n", 1, instruction.ErrInvalidSyntax},
|
||||
{"c d args\ncmd args\n", 1, instruction.ErrInvalidSyntax},
|
||||
{"cm args\ncmd args\n", 1, instruction.ErrInvalidSyntax},
|
||||
|
||||
{"ins args\n md args\n", 2, ErrInvalidSyntax},
|
||||
{"ins args\nc d args\n", 2, ErrInvalidSyntax},
|
||||
{"ins args\ncm args\n", 2, ErrInvalidSyntax},
|
||||
{"ins args\n md args\n", 2, instruction.ErrInvalidSyntax},
|
||||
{"ins args\nc d args\n", 2, instruction.ErrInvalidSyntax},
|
||||
{"ins args\ncm args\n", 2, instruction.ErrInvalidSyntax},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
|
Loading…
Reference in New Issue