From f7d930b58899b7fd1995985cf85fbef513aa753f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 9 Nov 2018 22:37:05 +0100 Subject: [PATCH] update 'buildfile' using 'instruction' --- internal/buildfile/errors.go | 4 -- internal/buildfile/instruction.go | 63 ------------------------------- internal/buildfile/reader.go | 16 ++++---- internal/buildfile/reader_test.go | 35 ++++++++--------- 4 files changed, 27 insertions(+), 91 deletions(-) delete mode 100644 internal/buildfile/instruction.go diff --git a/internal/buildfile/errors.go b/internal/buildfile/errors.go index d09374a..d00ffdb 100644 --- a/internal/buildfile/errors.go +++ b/internal/buildfile/errors.go @@ -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 diff --git a/internal/buildfile/instruction.go b/internal/buildfile/instruction.go deleted file mode 100644 index 46b1d68..0000000 --- a/internal/buildfile/instruction.go +++ /dev/null @@ -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 -} diff --git a/internal/buildfile/reader.go b/internal/buildfile/reader.go index 828bba5..2066758 100644 --- a/internal/buildfile/reader.go +++ b/internal/buildfile/reader.go @@ -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) } diff --git a/internal/buildfile/reader_test.go b/internal/buildfile/reader_test.go index e388710..3055977 100644 --- a/internal/buildfile/reader_test.go +++ b/internal/buildfile/reader_test.go @@ -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 {