Skip to content

Commit 9c4c8ea

Browse files
author
Youen Péron
authored
feat(stats): add environment variables to controls URL and template (#202)
* feat(stats): add environment variables to controls URL and template
1 parent ab990b4 commit 9c4c8ea

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Types of changes
1414
- `Fixed` for any bug fixes.
1515
- `Security` in case of vulnerabilities.
1616

17+
## [1.17.0]
18+
- `Added` Environment variable `PIMO_STATS_TEMPLATE` to control the format of generated stats
19+
- `Added` Environment variable `PIMO_STATS_URL` to generate a stat file or HTTP POST
20+
1721
## [1.16.0]
1822

1923
- `Added` flag to declare seed in command line

cmd/pimo/main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,20 @@ var (
6363
repeatWhile string
6464
statisticsDestination string
6565
statsTemplate string
66+
statsDestinationEnv = os.Getenv("PIMO_STATS_URL")
67+
statsTemplateEnv = os.Getenv("PIMO_STATS_TEMPLATE")
6668
)
6769

6870
func main() {
6971
rootCmd := &cobra.Command{
7072
Use: "pimo",
7173
Short: "Command line to mask data from jsonlines",
72-
Long: `Pimo is a tool to mask private data contained in jsonlines by using masking configurations`,
74+
Long: `Pimo is a tool to mask private data contained in jsonlines by using masking configurations
75+
76+
Environment Variables:
77+
PIMO_STATS_URL The URL where statistics will be sent
78+
PIMO_STATS_TEMPLATE The template string to format statistics`,
79+
7380
Version: fmt.Sprintf(`%v (commit=%v date=%v by=%v)
7481
Copyright (C) 2021 CGI France
7582
License GPLv3: GNU GPL version 3 <https://gnu.org/licenses/gpl.html>.
@@ -95,8 +102,8 @@ There is NO WARRANTY, to the extent permitted by law.`, version, commit, buildDa
95102
rootCmd.PersistentFlags().StringArrayVarP(&maskingOneLiner, "mask", "m", []string{}, "one liner masking")
96103
rootCmd.PersistentFlags().StringVar(&repeatUntil, "repeat-until", "", "mask each input repeatedly until the given condition is met")
97104
rootCmd.PersistentFlags().StringVar(&repeatWhile, "repeat-while", "", "mask each input repeatedly while the given condition is met")
98-
rootCmd.PersistentFlags().StringVar(&statisticsDestination, "stats", "", "generate execution statistics in the specified dump file")
99-
rootCmd.PersistentFlags().StringVar(&statsTemplate, "statsTemplate", "", "template string to format stats (to include them you have to specify them as `{{ .Stats }}` like `{\"software\":\"PIMO\",\"stats\":{{ .Stats }}}`)")
105+
rootCmd.PersistentFlags().StringVar(&statisticsDestination, "stats", statsDestinationEnv, "generate execution statistics in the specified dump file")
106+
rootCmd.PersistentFlags().StringVar(&statsTemplate, "statsTemplate", statsTemplateEnv, "template string to format stats (to include them you have to specify them as `{{ .Stats }}` like `{\"software\":\"PIMO\",\"stats\":{{ .Stats }}}`)")
100107

101108
rootCmd.AddCommand(&cobra.Command{
102109
Use: "jsonschema",

test/suites/dump-statistics.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,29 @@ testcases:
4848
- result.systemout ShouldContainSubstring {"software":"PIMO","stats"
4949
- result.systemout ShouldContainSubstring ignoredPaths":0
5050
- result.systemout ShouldContainSubstring duration
51+
52+
53+
- name: dump file with template from env
54+
steps:
55+
- script: rm -f masking.yml
56+
- script: rm -f pimo-stats-template.json
57+
- script: |-
58+
cat > masking.yml <<EOF
59+
version: "1"
60+
masking:
61+
- selector:
62+
jsonpath: "name"
63+
mask:
64+
add: "Dorothy"
65+
EOF
66+
- script: |-
67+
echo '{ "name": "John" }' | PIMO_STATS_TEMPLATE='{"software":"PIMO","stats":{{ .Stats }}}' PIMO_STATS_URL=pimo-stats-template.json pimo -v 3 --log-json
68+
assertions:
69+
- result.code ShouldEqual 0
70+
- result.systemerr ShouldContainSubstring Statistics exported to file
71+
- script: cat pimo-stats-template.json
72+
assertions:
73+
- result.code ShouldEqual 0
74+
- result.systemout ShouldContainSubstring {"software":"PIMO","stats"
75+
- result.systemout ShouldContainSubstring ignoredPaths":0
76+
- result.systemout ShouldContainSubstring duration

0 commit comments

Comments
 (0)