| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | SpecParsing.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Declarations for parsers that turn delimited command-line spec strings |
| 12 | (e.g. --secret, --ulimit, --label, --filter) into structured values. |
| 13 | |
| 14 | --*/ |
| 15 | #pragma once |
| 16 | |
| 17 | #include "ContainerModel.h" |
| 18 | #include "InspectModel.h" |
| 19 | #include <string> |
| 20 | #include <string_view> |
| 21 | #include <tuple> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | #include <wslc.h> |
| 25 | |
| 26 | namespace wsl::windows::wslc::services { |
| 27 | struct BuildSecret; |
| 28 | struct BuildOutput; |
| 29 | } // namespace wsl::windows::wslc::services |
| 30 | |
| 31 | namespace wsl::windows::wslc::validation { |
| 32 | |
| 33 | // The two halves of a spec token split at its first separator (default '='). |
| 34 | // HadSeparator distinguishes "key" (no separator) from "key=" (empty value). |
| 35 | struct KeyValueSplit |
| 36 | { |
| 37 | std::wstring Key; |
| 38 | std::wstring Value; |
| 39 | bool HadSeparator; |
| 40 | }; |
| 41 | |
| 42 | // Splits value at the first occurrence of separator. When no separator is present the whole |
| 43 | // string is returned as Key, Value is empty, and HadSeparator is false. |
| 44 | KeyValueSplit SplitKeyValue(const std::wstring& value, wchar_t separator = L'='); |
| 45 | |
| 46 | // Parses a docker-style --secret spec ("id=...,type=...,src=...") and resolves its value bytes. |
| 47 | services::BuildSecret ParseSecretSpec(const std::wstring& spec); |
| 48 | |
| 49 | // Parses a docker-style --output spec ("type=...,dest=...,<attr>=...") into a BuildOutput. |
| 50 | services::BuildOutput ParseOutputSpec(const std::wstring& spec); |
| 51 | |
| 52 | // Serializes a BuildOutput back into a canonical buildx --output spec ("type=...,dest=...,<attr>=..."). |
| 53 | std::wstring FormatOutputSpec(const services::BuildOutput& output); |
| 54 | |
| 55 | // True when the exporter produces a destination (file, directory, or stdout stream) that the client |
| 56 | // must materialize, versus running entirely in the build VM. Mirrors `docker buildx build --output`: |
| 57 | // local/tar/oci always stream a result back; docker streams only when a 'dest=' is given (an omitted |
| 58 | // dest loads the image into the VM store); image/registry/cacheonly never stream. |
| 59 | bool OutputStreamsToClient(const services::BuildOutput& output); |
| 60 | |
| 61 | // True when the exporter writes a directory tree rather than a single file/stream. The local exporter |
| 62 | // is always a directory; oci/docker export an OCI layout directory when 'tar=false' is set. |
| 63 | bool OutputIsDirectory(const services::BuildOutput& output); |
| 64 | |
| 65 | // Parses a --ulimit spec ("<name>=<soft>[:<hard>]") into (name, soft, hard). -1 means unlimited. |
| 66 | std::tuple<std::string, int64_t, int64_t> ParseUlimit(const std::wstring& input, const std::wstring& argName = {}); |
| 67 | |
| 68 | // Parses a --label spec ("key[=value]"); the key must be non-empty. |
| 69 | std::pair<std::string, std::string> ParseLabel(const std::wstring& value); |
| 70 | |
| 71 | // Parses a driver option spec ("key[=value]"); a missing value yields an empty string. |
| 72 | std::pair<std::string, std::string> ParseDriverOption(const std::wstring& value); |
| 73 | |
| 74 | // Parses a --filter spec ("key=value"); the separator is required. |
| 75 | std::pair<std::string, std::string> ParseFilter(const std::wstring& value); |
| 76 | |
| 77 | struct ParsedNetworkArgument |
| 78 | { |
| 79 | std::string Name; |
| 80 | std::vector<std::string> Aliases; |
| 81 | }; |
| 82 | |
| 83 | // Parses a --network spec ("network" or "name=network,alias=alias"). |
| 84 | ParsedNetworkArgument ParseNetworkArgument(std::wstring_view value, const std::wstring& argName = {}); |
| 85 | |
| 86 | // Parses a signal by name ("SIGKILL"/"KILL", case-insensitive) or number ("9") into a WSLCSignal. |
| 87 | WSLCSignal GetWSLCSignalFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 88 | |
| 89 | // Parses a timestamp given as Unix epoch seconds or an RFC3339 string into epoch seconds. |
| 90 | LONGLONG GetTimestampFromString(const std::wstring& value, const std::wstring& argName = {}); |
| 91 | |
| 92 | // Parses an output format ("json"/"table") into a FormatType. |
| 93 | models::FormatType GetFormatTypeFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 94 | |
| 95 | // Parses the inspect family's sole supported format ("json") into its compact json::dump() indent. |
| 96 | int GetInspectJsonIndentFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 97 | |
| 98 | // Parses an image pull policy ("always"/"missing"/"never"). |
| 99 | models::PullPolicy GetPullPolicyFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 100 | |
| 101 | // Parses a build progress type ("auto"/"tty"/"plain"/"quiet") into a ProgressMode. |
| 102 | models::ProgressMode GetProgressModeFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 103 | |
| 104 | // Parses an inspect target ("image"/"container"/"network"/"volume") into an InspectType. |
| 105 | models::InspectType GetInspectTypeFromString(const std::wstring& input, const std::wstring& argName); |
| 106 | |
| 107 | // Parses a Docker-style memory size (e.g. "512m", "1.5g") into a byte count. |
| 108 | int64_t GetMemorySizeFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 109 | |
| 110 | // Parses a Go-style duration (e.g. "1.5h", "500ms") into nanoseconds. |
| 111 | int64_t GetDurationNanosFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 112 | |
| 113 | // Parses a fractional CPU count into nano-CPUs (cpus * 1e9). |
| 114 | int64_t GetNanoCpusFromString(const std::wstring& input, const std::wstring& argName = {}); |
| 115 | |
| 116 | } // namespace wsl::windows::wslc::validation |