Skip to content

Commit df38cca

Browse files
feat(export): support multiple values within --add-label flag
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent 29ef9c4 commit df38cca

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

cmd/werf/export/export.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"os"
89
"strings"
910
"text/template"
1011

@@ -29,6 +30,7 @@ var commonCmdData common.CmdData
2930
func NewExportCmd(ctx context.Context) *cobra.Command {
3031
var tagTemplateList []string
3132
var addLabelArray []string
33+
var addLabelSeparator string
3234

3335
ctx = common.NewContextWithCmdData(ctx, &commonCmdData)
3436
cmd := common.SetCommandContext(ctx, &cobra.Command{
@@ -66,18 +68,17 @@ func NewExportCmd(ctx context.Context) *cobra.Command {
6668
return fmt.Errorf("required at least one tag template: use the --tag option to specify templates")
6769
}
6870

69-
var addLabelMap map[string]string
70-
var err error
71-
{
72-
addLabelArray := append(util.PredefinedValuesByEnvNamePrefix("WERF_EXPORT_ADD_LABEL_"), addLabelArray...)
73-
addLabelMap, err = common.InputArrayToKeyValueMap(addLabelArray, "=", ",")
74-
if err != nil {
75-
common.PrintHelp(cmd)
76-
return fmt.Errorf("unsupported --add-label value: %w", err)
77-
}
71+
result, err := common.InputArrayToKeyValueMap(
72+
append(util.PredefinedValuesByEnvNamePrefix("WERF_EXPORT_ADD_LABEL_"), addLabelArray...),
73+
addLabelSeparator,
74+
common.DefaultKeyValueSeparator,
75+
)
76+
if err != nil {
77+
common.PrintHelp(cmd)
78+
return fmt.Errorf("unsupported --add-label value: %w", err)
7879
}
7980

80-
return run(ctx, args, tagTemplateList, addLabelMap)
81+
return run(ctx, args, tagTemplateList, result)
8182
},
8283
})
8384

@@ -130,9 +131,16 @@ func NewExportCmd(ctx context.Context) *cobra.Command {
130131
It is necessary to use image name shortcut %image% or %image_slug% if multiple images are exported (e.g. REPO:TAG-%image% or REPO-%image%:TAG)`)
131132

132133
cmd.Flags().StringArrayVarP(&addLabelArray, "add-label", "", []string{}, `Add label to exported images (can specify multiple).
133-
Format: labelName=labelValue.
134+
Format: labelName=labelValue[<separator>labelName=labelValue ...]. The default separator is a newline ("\n"), but it can be customized using the --add-label-separator flag.
134135
Also, can be specified with $WERF_EXPORT_ADD_LABEL_* (e.g. $WERF_EXPORT_ADD_LABEL_1=labelName1=labelValue1, $WERF_EXPORT_ADD_LABEL_2=labelName2=labelValue2)`)
135136

137+
defaultValue := common.DefaultAnnoAndLabelPairSeparator
138+
if os.Getenv("WERF_EXPORT_ADD_LABEL_SEPARATOR") != "" {
139+
defaultValue = os.Getenv("WERF_EXPORT_ADD_LABEL_SEPARATOR")
140+
}
141+
142+
cmd.Flags().StringVarP(&addLabelSeparator, "add-label-separator", "", defaultValue, `Separator for --add-label values (default $WERF_EXPORT_ADD_LABEL_SEPARATOR or "\n")`)
143+
136144
return cmd
137145
}
138146

docs/_includes/reference/cli/werf_export.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ werf export [IMAGE_NAME...] [options]
3737
```shell
3838
--add-label="[]"
3939
Add label to exported images (can specify multiple).
40-
Format: labelName=labelValue.
40+
Format: labelName=labelValue[<separator>labelName=labelValue ...]. The default
41+
separator is a newline ("\n"), but it can be customized using the --add-label-separator
42+
flag.
4143
Also, can be specified with $WERF_EXPORT_ADD_LABEL_* (e.g.
4244
$WERF_EXPORT_ADD_LABEL_1=labelName1=labelValue1,
4345
$WERF_EXPORT_ADD_LABEL_2=labelName2=labelValue2)
46+
--add-label-separator="\n"
47+
Separator for --add-label values (default $WERF_EXPORT_ADD_LABEL_SEPARATOR or "\n")
4448
--cache-repo="[]"
4549
Specify one or multiple cache repos with images that will be used as a cache. Cache
4650
will be populated when pushing newly built images into the primary repo and when

0 commit comments

Comments
 (0)