From a4b5750cfb674ddc331fec2c92c2e01827884c13 Mon Sep 17 00:00:00 2001 From: Andrea Manzini Date: Thu, 2 Feb 2023 12:19:31 +0100 Subject: [PATCH] add test ; update TODO and license --- TODO.md | 7 ++++--- containertools.nimble | 2 +- src/containertools/instruction.nim | 11 +++++++++++ tests/test_parsing.nim | 5 ++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 5e3dc88..e168bc3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,5 @@ ## Todo -- [ ] add build feature that permits to run our image - - remember to use -p for any EXPOSEd port - [ ] add examples and documentation - [ ] define syntax validation rules and error reporting - some examples: https://github.com/marquesmps/dockerfile_validator/blob/develop/src/rule_files/default_rules.yaml @@ -11,7 +9,10 @@ ## In progress -- [ ] publish package on nimble https://github.com/nim-lang/nimble#creating-packages +- [ ] add build feature that permits to run our image + - remember to use -p for any EXPOSEd port +- [x] publish package on nimble https://github.com/nim-lang/nimble#creating-packages + - PR done on https://github.com/nim-lang/packages, waiting for merge ## Done ✓ diff --git a/containertools.nimble b/containertools.nimble index 3477e8b..9a168d5 100644 --- a/containertools.nimble +++ b/containertools.nimble @@ -3,7 +3,7 @@ version = "0.1.0" author = "Andrea Manzini" description = "A library to handle container file" -license = "GPL-2.0-only" +license = "GPL-3.0" srcDir = "src" diff --git a/src/containertools/instruction.nim b/src/containertools/instruction.nim index 5e5387a..3a44c59 100644 --- a/src/containertools/instruction.nim +++ b/src/containertools/instruction.nim @@ -52,6 +52,14 @@ proc `$`*(self: Instruction): string = proc unquote(s: string): seq[string] = result = s.strip[1..^2].split(',').mapIt(it.strip[1..^2]) +proc isNumeric(x: string): bool = + try: + discard parseInt(x) + result = true + except ValueError: + result = false + + # parse a string into a Instruction proc parse*(str: string): Instruction = let tokens = str.strip().split(maxsplit = 1) @@ -62,6 +70,9 @@ proc parse*(str: string): Instruction = if (parsed_instr == CMD or parsed_instr == RUN or parsed_instr == ENTRYPOINT) and arg.startsWith('['): return Instruction(cmd: parsed_instr, kind: Ak_array, array_val: unquote(arg)) + if parsed_instr == EXPOSE and arg.isNumeric: + return Instruction(cmd: EXPOSE, kind: Ak_int, int_val: uint16( + arg.parseInt)) # TODO: can overflow result = Instruction(cmd: parsed_instr, kind: Ak_string, str_val: arg) diff --git a/tests/test_parsing.nim b/tests/test_parsing.nim index 0227976..98719ac 100644 --- a/tests/test_parsing.nim +++ b/tests/test_parsing.nim @@ -1,6 +1,7 @@ import unittest2 import containertools + suite "parse single line": test "can parse some instruction": let testTable = [ @@ -13,7 +14,9 @@ suite "parse single line": ("COPY start.sh start.sh", Instruction(cmd: BuildInstruction.COPY, kind: Ak_string, str_val: "start.sh start.sh")), ("RUN rm /usr/sbin/policy-rc.d", Instruction(cmd: BuildInstruction.RUN, - kind: Ak_string, str_val: "rm /usr/sbin/policy-rc.d")) + kind: Ak_string, str_val: "rm /usr/sbin/policy-rc.d")), + ("EXPOSE 8080", Instruction(cmd: BuildInstruction.EXPOSE, kind: Ak_int, + int_val: 8080)) ] for items in testTable: check: items[0].parse == items[1]