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
2 changes: 1 addition & 1 deletion bash/patchman-client
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ build_json_report() {
# Convert tags to JSON array
local tags_json="[]"
if [ -n "${tags}" ] && [ "${tags}" != "Default" ] ; then
tags_json=$(echo "${tags}" | tr ',' '\n' | jq -R . | jq -s .)
tags_json=$(echo "${tags}" | jq -R 'if test(",") then split(",") else [splits("\\s+")] end | map(gsub("^\\s+|\\s+$"; "")) | map(select(length > 0))')
fi

# Convert reboot to boolean
Expand Down
2 changes: 1 addition & 1 deletion bash/patchman-client.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ server=https://patchman.example.com
# Options to curl
curl_options="--insecure --connect-timeout 60 --max-time 300"

# Space delimited tags to send
# Comma or space delimited tags to send
tags="Server"

# Does the client output a report of the upload (e.g. for cronjob output)
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Description: Patchman client - meta-package with hooks

Package: patchman-client-bash
Architecture: all
Depends: ${misc:Depends}, bash, curl, coreutils, gawk, grep, sed
Depends: ${misc:Depends}, bash, curl, coreutils, gawk, grep, sed, jq
Recommends: jq
Provides: patchman-client-impl
Description: Patchman client - bash implementation
Expand Down
14 changes: 8 additions & 6 deletions go/lib/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ func NewReport(osInfo si.OSInfo) *Report {
}
}

// SetTags sets the tags from a comma-separated string
// SetTags sets the tags from a comma or space delimited string
func (r *Report) SetTags(tags string) {
if tags == "" {
return
parts := strings.Fields(tags)
if strings.Contains(tags, ",") {
parts = strings.Split(tags, ",")
}
r.Tags = strings.Split(tags, ",")
for i, tag := range r.Tags {
r.Tags[i] = strings.TrimSpace(tag)
for _, tag := range parts {
if tag = strings.TrimSpace(tag); tag != "" {
r.Tags = append(r.Tags, tag)
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions go/lib/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ func TestSetTags(t *testing.T) {
}
}

func TestSetTagsDelimiters(t *testing.T) {
cases := map[string][]string{
"web prod": {"web", "prod"},
"web, prod": {"web", "prod"},
"web,,prod,": {"web", "prod"},
" ": nil,
}
for in, want := range cases {
rpt := NewReport(si.OSInfo{Hostname: "test"})
rpt.SetTags(in)
if strings.Join(rpt.Tags, "|") != strings.Join(want, "|") {
t.Errorf("SetTags(%q) = %q, want %q", in, rpt.Tags, want)
}
}
}

func TestAddRPMs(t *testing.T) {
osInfo := si.OSInfo{Hostname: "test"}
rpt := NewReport(osInfo)
Expand Down
2 changes: 1 addition & 1 deletion patchman-client.spec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ the client implementations (bash, python, or go).

%package bash
Summary: Patchman client - bash implementation
Requires: bash curl coreutils gawk grep sed
Requires: bash curl coreutils gawk grep sed jq
Recommends: jq
Provides: patchman-client-impl

Expand Down
3 changes: 2 additions & 1 deletion python/patchman_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def __init__(
self.server = server.rstrip('/') if server else ''
self.api_key = api_key
self.hostname = hostname or self._get_hostname()
self.tags = [t.strip() for t in tags.split(',') if t.strip()] if tags else []
sep = ',' if ',' in tags else None
self.tags = [t.strip() for t in tags.split(sep) if t.strip()] if tags else []
self.verbose = verbose
self.debug = debug
self.local_updates = local_updates
Expand Down
20 changes: 20 additions & 0 deletions tests/test_client_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,26 @@ def test_config_quoted_values(self):
finally:
os.unlink(conf_path)

def test_tags_comma_or_space_delimited(self):
"""Tags split on commas if present, otherwise on whitespace."""
import importlib
spec = importlib.util.spec_from_file_location(
'client', os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'python', 'patchman_client', 'client.py',
),
)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
for tags, expected in (
('web prod', ['web', 'prod']),
('web, prod', ['web', 'prod']),
('my tag,web', ['my tag', 'web']),
('', []),
):
client = mod.PatchmanClient(hostname='test', tags=tags)
self.assertEqual(client.tags, expected)


if __name__ == '__main__':
unittest.main()
Loading