Skip to content

Commit

Permalink
fix: Implement repeated options (#138)
Browse files Browse the repository at this point in the history
* implement repeated options

update deps, upgrade to go 1.21
  • Loading branch information
unRob committed Apr 20, 2024
1 parent 7300430 commit ea96815
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .milpa/docs/milpa/command/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ The environment available to a **script** is composed of four groups:
2. `MILPA_OPT_*` variables hold values for every **option** defined, and
3. [global environment variables](/.milpa/docs/milpa/environment.md) that affect `milpa`'s overall behavior.

> ℹ️ `variadic` arguments and `repeated` options are passed an array of values (i.e. read with `${MILPA_OPT_THING[@]}`)

### Command metadata: `MILPA_COMMAND_*`

Expand Down
16 changes: 9 additions & 7 deletions .milpa/docs/milpa/command/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ The `arguments` list describes the positional arguments that may be passed to a
arguments:
# available in to the command as the MILPA_ARG_INCREMENT environment variable
- name: increment
# this description shows up during auto-completion and in the command's help page
descrption: the increment to apply to the last git version
# this description shows up when rendering the command's help
description: the increment to apply to the last git version
# a default may be specified, it'll be passed to your command if none is provided
default: patch
# if marked as required, the command won't run unless this argument is provided
Expand All @@ -99,7 +99,7 @@ arguments:
## Options
The `options` map describes the named options that may be passed to a command. Options require a `name` and a `description`. The `name` of the option will become available to commands through the environment variable named `MILPA_OPT_$NAME` where `$NAME` means the uppercased value of key for an option. For example, an option at the key `scheme` will be available as your command's environment variable `MILPA_OPT_SCHEME`.
The `options` map describes the named options that may be passed to a command (i.e. `--name value`). Options require a `name` and a `description`. The `name` of the option will become available to commands through the environment variable named `MILPA_OPT_$NAME` where `$NAME` means the uppercased value of key for an option. For example, an option at the key `scheme` will be available as your command's environment variable `MILPA_OPT_SCHEME`.

> ⚠️ Options are not available as positional arguments to your command. The same character restrictions as arguments apply to options.

Expand All @@ -110,20 +110,22 @@ options:
# it will be available to your script as the $MILPA_OPT_SCHEME environment variable
# and may be specified on the command line as either `--scheme "semver"` or `--scheme=semver`.
scheme:
# options require a description, this will show during completions
# and on the command's help page
# options require a description, this will show when rendering the command's help
description: Determines the format of the tags for this repo.
# Sometimes, very commonly used flags might benefit from setting a short name
# in this case, users would be able to use `-s calver`
short-name: s
# a default value may be passed to the command if none is provided by the user
default: semver
# the values provided at the command line
# flags can be boolean. Since environment variables can only be strings,
# false values (the default) will be passed as an empty string "", while
# true values will be passed as the string "true"
type: bool # or `string`
type: string # or `bool`
# string flags may be repeated multiple times
# should an option be repeated, it's `default:` then must also be a list!
repeated: false
# the `values` property specifies how to provide completions and perform validation on
# the values provided at the command line
values: {}
```
---
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
golang 1.20.2
golang 1.21.1
24 changes: 12 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
module github.com/unrob/milpa

go 1.20
go 1.21

require (
git.rob.mx/nidito/chinampa v0.1.4
github.com/alecthomas/chroma/v2 v2.12.0
git.rob.mx/nidito/chinampa v0.2.1
github.com/alecthomas/chroma/v2 v2.13.0
github.com/alessio/shellescape v1.4.2
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/charmbracelet/glamour v0.7.0
github.com/fatih/color v1.16.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/yuin/goldmark v1.7.0
github.com/yuin/goldmark v1.7.1
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -36,11 +36,11 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/yuin/goldmark-emoji v1.0.2 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
60 changes: 30 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
git.rob.mx/nidito/chinampa v0.1.4 h1:rXmtuwYNtBmpLgWnNK7b5Z/styyuFfCP3RqL+qOzlok=
git.rob.mx/nidito/chinampa v0.1.4/go.mod h1:isI138ZQ3GN/ZcuRrNPJ48fYnPC+U642gUe5bonhwUo=
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
git.rob.mx/nidito/chinampa v0.2.1 h1:DlXiu2j8aKNMb5Z2Vr291DwiCl19ikyGrVpJEQn5kIw=
git.rob.mx/nidito/chinampa v0.2.1/go.mod h1:5X0gMayjUVs6biK6UoNZjEEc1wDEod+0PZg3ZrGgGUo=
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs=
github.com/alecthomas/chroma/v2 v2.12.0 h1:Wh8qLEgMMsN7mgyG8/qIpegky2Hvzr4By6gEF7cmWgw=
github.com/alecthomas/chroma/v2 v2.12.0/go.mod h1:4TQu7gdfuPjSh76j78ietmqh9LiurGF0EpseFXdKMBw=
github.com/alecthomas/chroma/v2 v2.13.0 h1:VP72+99Fb2zEcYM0MeaWJmV+xQvz5v5cxRHd+ooU1lI=
github.com/alecthomas/chroma/v2 v2.13.0/go.mod h1:BUGjjsD+ndS6eX37YgTchSEG+Jg9Jv1GiZs9sqPqztk=
github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0=
github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
Expand All @@ -22,26 +24,28 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4=
github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand All @@ -65,8 +69,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand All @@ -75,32 +79,28 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.3.7/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GAsO5s=
github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY=
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ=
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
18 changes: 15 additions & 3 deletions internal/command/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ func envValue(opts command.Options, f *pflag.Flag) (*string, *string) {
envName = fmt.Sprintf("%s%s", _c.OutputPrefixOpt, strings.ToUpper(strings.ReplaceAll(name, "-", "_")))

if opt := opts[name]; opt != nil {
value = opt.ToString()
if opt.Repeated {
temp := []string{}
for _, v := range opt.ToValue().([]string) {
temp = append(temp, shellescape.Quote(v))
}
value = fmt.Sprintf("( %s )", strings.Join(temp, " "))
} else {
value = opt.ToString()
}
}

if value == "false" && f.Value.Type() == "bool" {
Expand All @@ -74,10 +82,14 @@ func envValue(opts command.Options, f *pflag.Flag) (*string, *string) {

// ToEnv writes shell variables to dst.
func OptionsToEnv(cmd *command.Command, dst *[]string, prefix string) {
cmd.Cobra.Flags().VisitAll(func(f *pflag.Flag) {
cmd.FlagSet().VisitAll(func(f *pflag.Flag) {
envName, value := envValue(cmd.Options, f)
if envName != nil && value != nil {
*dst = append(*dst, fmt.Sprintf("%s%s=%s", prefix, *envName, shellescape.Quote(*value)))
if opt := cmd.Options[f.Name]; opt != nil && opt.Repeated {
*dst = append(*dst, fmt.Sprintf("%s%s=%s", prefix, *envName, *value))
} else {
*dst = append(*dst, fmt.Sprintf("%s%s=%s", prefix, *envName, shellescape.Quote(*value)))
}
}
})
}
Expand Down
112 changes: 112 additions & 0 deletions internal/command/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,115 @@ func TestArgumentsToEnv(t *testing.T) {
})
}
}

func TestOptionsToEnv(t *testing.T) {
cases := []struct {
Command *command.Command
Args []string
Expect []string
Env []string
}{
{
Args: []string{"test", "string", "--first", "something"},
Expect: []string{"export MILPA_OPT_FIRST=something"},
Command: &command.Command{
Meta: Meta{
Name: []string{"test", "string"},
},
Options: command.Options{
"first": &command.Option{
Type: command.ValueTypeString,
},
},
},
},
{
Args: []string{"test", "int", "--first", "1"},
Expect: []string{"export MILPA_OPT_FIRST=1"},
Command: &command.Command{
Meta: Meta{
Name: []string{"test", "int"},
},
Options: command.Options{
"first": &command.Option{
Type: command.ValueTypeInt,
},
},
},
},
{
Args: []string{"test", "bool", "--first"},
Expect: []string{"export MILPA_OPT_FIRST=true"},
Command: &command.Command{
Meta: Meta{
Name: []string{"test", "bool"},
},
Options: command.Options{
"first": &command.Option{
Type: command.ValueTypeBoolean,
},
},
},
},
{
Args: []string{"test", "bool-false", "--first", "false"},
Expect: []string{"export MILPA_OPT_FIRST="},
Command: &command.Command{
Meta: Meta{
Name: []string{"test", "bool-false"},
},
Options: command.Options{
"first": &command.Option{
Type: command.ValueTypeBoolean,
},
},
},
},
{
Args: []string{"test", "repeated", "--pato", "quem", "--pato", "quem quem"},
Expect: []string{"export MILPA_OPT_PATO=( quem 'quem quem' )"},
Command: &command.Command{
Meta: Meta{
Name: []string{"test", "repeated"},
},
Options: command.Options{
"pato": &command.Option{
Type: command.ValueTypeString,
Repeated: true,
},
},
},
},
}

for _, c := range cases {
t.Run(c.Command.FullName(), func(t *testing.T) {
dst := []string{}
c.Command.SetBindings()
err := c.Command.FlagSet().Parse(c.Args)
if err != nil {
t.Fatalf("Could not parse test arguments (%+v): %s", c.Args, err)
}
c.Command.Options.Parse(c.Command.FlagSet())
OptionsToEnv(c.Command, &dst, "export ")

if err := c.Command.Options.AreValid(); err != nil {
t.Fatalf("Unexpected failure validating: %s", err)
}

for _, expected := range c.Expect {
found := false
for _, actual := range dst {
if strings.HasPrefix(actual, expected) {
found = true
break
}
}

if !found {
t.Fatalf("Expected line %v not found in %v", expected, dst)
}
}
})
}
}
2 changes: 1 addition & 1 deletion repos/internal/commands/dev/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if [[ "$MILPA_OPT_COVERAGE" ]]; then
milpa dev test unit --coverage
milpa dev test integration --coverage
mkdir -p "$MILPA_ROOT/test/coverage/doctor"
GOCOVERDIR="$MILPA_ROOT/test/coverage/doctor" milpa itself doctor
MILPA_PATH="$(pwd)/.milpa" MILPA_PATH_PARSED=true GOCOVERDIR="$MILPA_ROOT/test/coverage/doctor" milpa itself doctor
milpa dev test coverage-report
milpa dev build
else
Expand Down

0 comments on commit ea96815

Please sign in to comment.