Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cli/cmds/device-container.toit
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ install-container invocation/Invocation:
if seen-triggers.contains "interval":
ui.abort "Duplicate trigger 'interval'."
seen-triggers.add "interval"
duration := parse-duration parsed-trigger["interval"] --on-error=:
duration := parse-duration parsed-trigger["interval"] --if-error=:
ui.abort "Invalid interval '$parsed-trigger'. Use 20s, 5m10s, 12h or similar."
triggers.add (pod-specification.IntervalTrigger duration)
else if parsed-trigger is Map and (parsed-trigger.contains "gpio-low" or parsed-trigger.contains "gpio-high"):
// Add an entry to the seen triggers list, so we can ensure that it's not combined with 'none'.
seen-triggers.add "gpio"
on-high := parsed-trigger.contains "gpio-high"
pin-string := on-high ? parsed-trigger["gpio-high"] : parsed-trigger["gpio-low"]
pin := int.parse pin-string --on-error=:
pin := int.parse pin-string --if-error=:
ui.abort "Invalid pin '$pin-string'."

if seen-pins.contains pin:
Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmds/device.toit
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ set-max-offline invocation/Invocation:
ui := invocation.cli.ui

with-device invocation: | device/DeviceFleet fleet/FleetWithDevices |
max-offline-seconds := int.parse max-offline --on-error=:
max-offline-seconds := int.parse max-offline --if-error=:
// Assume it's a duration with units, like "5s".
duration := parse-duration max-offline --on-error=:
duration := parse-duration max-offline --if-error=:
ui.abort "Invalid max-offline duration: $max-offline."
duration.in-s

Expand Down
4 changes: 2 additions & 2 deletions src/cli/cmds/fleet.toit
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ group-add invocation/Invocation:
with-devices-fleet invocation: | fleet/FleetWithDevices |
pod-reference/PodReference? := null
if pod:
pod-reference = PodReference.parse pod --on-error=:
pod-reference = PodReference.parse pod --if-error=:
ui.abort "Invalid pod reference: $pod"
else if template:
pod-reference = fleet.pod-reference-for-group template
Expand Down Expand Up @@ -877,7 +877,7 @@ group-update invocation/Invocation:

pod-reference/PodReference? := null
if pod:
pod-reference = PodReference.parse pod --on-error=:
pod-reference = PodReference.parse pod --if-error=:
ui.abort "Invalid pod reference: $pod"
groups.do: | group/string |
if not fleet-file.group-pods.contains group:
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cmds/serial.toit
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ build-partitions_ partition-list/List --cli/Cli -> List:
ui.emit --error "No such file: $value."
ui.abort
else:
size := int.parse value --on-error=:
size := int.parse value --if-error=:
ui.abort "Partition '$type:$name' has illegal size: '$it'."
if size <= 0:
ui.abort "Partition '$type:$name' has illegal size: $size."
Expand Down
18 changes: 11 additions & 7 deletions src/cli/pod-registry.toit
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ class PodReference:

static parse str/string --allow-name-only/bool=false --cli/Cli -> PodReference:
return parse str --allow-name-only=allow-name-only
--on-error=(: cli.ui.abort it)
--if-error=(: cli.ui.abort it)

static parse str/string --allow-name-only/bool=false [--on-error] -> PodReference:
static parse str/string --allow-name-only/bool=false [--if-error] -> PodReference:
name/string? := null
revision/int? := null
tag/string? := null
hash-index := str.index-of "#"
at-index := str.index-of "@"
if hash-index >= 0 and at-index >= 0:
on-error.call "Cannot specify both revision and tag: '$str'."
if-error.call "Cannot specify both revision and tag: '$str'."
unreachable

if hash-index >= 0:
if revision:
on-error.call "Cannot specify the revision as option and in the name."
if-error.call "Cannot specify the revision as option and in the name."
unreachable
revision-string := str[hash-index + 1..]
revision = int.parse revision-string
--on-error=(: on-error.call "Invalid revision: '$revision-string'.")
--if-error=(: if-error.call "Invalid revision: '$revision-string'.")
name = str[..hash-index]
return PodReference --name=name --revision=revision

Expand All @@ -72,11 +74,13 @@ class PodReference:
if allow-name-only:
name = str
return PodReference --name=name
on-error.call "Invalid pod uuid: '$str'."
if-error.call "Invalid pod uuid: '$str'."
unreachable
return PodReference --id=id

if not allow-name-only:
on-error.call "Invalid pod reference: '$str'."
if-error.call "Invalid pod reference: '$str'."
unreachable

return PodReference --name=str

Expand Down
2 changes: 1 addition & 1 deletion src/cli/pod-specification.toit
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class JsonMap:
entry-string = entry-string.trim
if entry-string == "": return Duration.ZERO

return parse-duration entry-string --on-error=:
return parse-duration entry-string --if-error=:
format-error_ "$entry-type $key in $holder is not a valid duration: $entry"

get-optional-duration key/string --entry-type/string="Entry" -> Duration?:
Expand Down
24 changes: 12 additions & 12 deletions src/cli/utils/utils.toit
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,28 @@ read-base64-ubjson path/string -> any:
return ubjson.decode (base64.decode data)

read-file path/string --cli/Cli [block] -> any:
return read-file path block --on-error=: | exception |
return read-file path block --if-error=: | exception |
cli.ui.abort "Failed to open '$path' for reading ($exception)."

read-file path/string [block] [--on-error] -> any:
read-file path/string [block] [--if-error] -> any:
stream/file.Stream? := null
exception := catch: stream = file.Stream.for-read path
if not stream:
return on-error.call exception
return if-error.call exception
try:
return block.call stream.in
finally:
stream.close

write-file path/string --cli/Cli [block] -> none:
write-file path block --on-error=: | exception |
write-file path block --if-error=: | exception |
cli.ui.abort "Failed to open '$path' for writing ($exception)."

write-file path/string [block] [--on-error] -> none:
write-file path/string [block] [--if-error] -> none:
stream/file.Stream? := null
exception := catch: stream = file.Stream.for-write path
if not stream:
on-error.call exception
if-error.call exception
return
try:
block.call stream.out
Expand Down Expand Up @@ -263,24 +263,24 @@ parse-pod-specification-file path/string --cli/Cli -> PodSpecification:
Parses a string like "1h 30m 10s" or "1h30m10s" into a Duration.
*/
parse-duration str/string -> Duration:
return parse-duration str --on-error=: throw "Invalid duration string: $it"
return parse-duration str --if-error=: throw "Invalid duration string: $it"

parse-duration str/string [--on-error] -> Duration:
parse-duration str/string [--if-error] -> Duration:
UNITS ::= ["h", "m", "s"]
splits-with-missing := UNITS.map: str.index-of it
splits := splits-with-missing.filter: it != -1
if splits.is-empty or not splits.is-sorted:
return on-error.call str
return if-error.call str
if splits.last != str.size - 1:
return on-error.call str
return if-error.call str

last-unit := -1
values := {:}
splits.do: | split/int |
unit := str[split]
value-string := str[last-unit + 1..split]
value := int.parse value-string.trim --on-error=:
return on-error.call str
value := int.parse value-string.trim --if-error=:
return if-error.call str
values[unit] = value
last-unit = split

Expand Down
4 changes: 2 additions & 2 deletions tests/parse-duration-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import expect show *
import artemis.cli.utils show parse-duration

parse str/string -> Duration:
return parse-duration str --on-error=: throw "Illegal"
return parse-duration str --if-error=: throw "Illegal"

expect-parse-error str/string:
parse-duration str --on-error=: return
parse-duration str --if-error=: return
throw "Expected parse error"

expect-equals-seconds s/int duration/Duration:
Expand Down
18 changes: 9 additions & 9 deletions tests/pod-designation-parse-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import artemis.cli.pod-registry show PodReference
import expect show *

main:
reference := PodReference.parse "foo@bar" --on-error=: throw it
reference := PodReference.parse "foo@bar" --if-error=: throw it
expect-equals "foo" reference.name
expect-equals "bar" reference.tag

expect-throw "Cannot specify both revision and tag: 'foo@bar#baz'.":
PodReference.parse "foo@bar#baz" --on-error=: throw it
PodReference.parse "foo@bar#baz" --if-error=: throw it

reference = PodReference.parse "foo#123" --on-error=: throw it
reference = PodReference.parse "foo#123" --if-error=: throw it
expect-equals "foo" reference.name
expect-equals 123 reference.revision

expect-throw "Invalid revision: 'bar'.":
PodReference.parse "foo#bar" --on-error=: throw it
PodReference.parse "foo#bar" --if-error=: throw it

uuid-string := "01234567-89ab-cdef-0123-456789abcdef"
reference = PodReference.parse uuid-string --on-error=: throw it
reference = PodReference.parse uuid-string --if-error=: throw it
expect-equals uuid-string "$reference.id"

expect-throw "Invalid pod reference: 'foo'.":
PodReference.parse "foo" --on-error=: throw it
PodReference.parse "foo" --if-error=: throw it

reference = PodReference.parse "foo" --allow-name-only --on-error=: throw it
reference = PodReference.parse "foo" --allow-name-only --if-error=: throw it
expect-equals "foo" reference.name

expect-throw "Invalid pod reference: 'foo'.":
PodReference.parse "foo" --on-error=: throw it
PodReference.parse "foo" --if-error=: throw it

reference = PodReference.parse "foo@bar@baz" --on-error=: throw it
reference = PodReference.parse "foo@bar@baz" --if-error=: throw it
expect-equals "foo" reference.name
expect-equals "bar@baz" reference.tag
Loading