Skip to content

Commit f735457

Browse files
authored
revert ActionPlan info/warning to StandardLogger (#3536)
1 parent 9a3f94d commit f735457

File tree

9 files changed

+25
-81
lines changed

9 files changed

+25
-81
lines changed

pkg/hubops/disable.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"os"
77

8+
log "github.com/sirupsen/logrus"
9+
810
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
911
)
1012

@@ -53,7 +55,7 @@ func (c *DisableCommand) Prepare(plan *ActionPlan) (bool, error) {
5355
i := c.Item
5456

5557
if i.State.IsLocal() {
56-
plan.Warning(i.FQName() + " is a local item, please delete manually")
58+
log.Warnf("%s is a local item, please delete manually", i.FQName())
5759
return false, nil
5860
}
5961

pkg/hubops/download.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/fatih/color"
13-
"github.com/sirupsen/logrus"
13+
log "github.com/sirupsen/logrus"
1414
"gopkg.in/yaml.v3"
1515

1616
"github.com/crowdsecurity/go-cs-lib/downloader"
@@ -38,13 +38,13 @@ func (c *DownloadCommand) Prepare(plan *ActionPlan) (bool, error) {
3838
i := c.Item
3939

4040
if i.State.IsLocal() {
41-
plan.Info(i.FQName() + " - not downloading local item")
41+
log.Infof("%s - not downloading local item", i.FQName())
4242
return false, nil
4343
}
4444

4545
// XXX: if it's tainted do we upgrade the dependencies anyway?
4646
if i.State.Tainted && !c.Force {
47-
plan.Warning(i.FQName() + " is tainted, use '--force' to overwrite")
47+
log.Warnf("%s is tainted, use '--force' to overwrite", i.FQName())
4848
return false, nil
4949
}
5050

@@ -132,7 +132,7 @@ func downloadDataSet(ctx context.Context, dataFolder string, force bool, reader
132132
BeforeRequest(func(req *http.Request) {
133133
fmt.Printf("downloading %s\n", req.URL)
134134
}).
135-
WithLogger(logrus.WithField("url", dataS.SourceURL))
135+
WithLogger(log.WithField("url", dataS.SourceURL))
136136

137137
if !force {
138138
d = d.WithLastModified().

pkg/hubops/plan.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111

1212
"github.com/AlecAivazis/survey/v2"
13-
"github.com/fatih/color"
1413

1514
"github.com/crowdsecurity/go-cs-lib/slicetools"
1615

@@ -96,14 +95,6 @@ func (p *ActionPlan) AddCommand(c Command) error {
9695
return nil
9796
}
9897

99-
func (p *ActionPlan) Info(msg string) {
100-
fmt.Println(msg)
101-
}
102-
103-
func (p *ActionPlan) Warning(msg string) {
104-
fmt.Printf("%s %s\n", color.YellowString("WARN"), msg)
105-
}
106-
10798
// Description returns a string representation of the action plan.
10899
// If verbose is false, the operations are grouped by item type and operation type.
109100
// If verbose is true, they are listed as they appear in the command slice.

test/bats/20_hub.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ teardown() {
172172
mkdir -p "$CONFIG_DIR/collections"
173173
touch "$CONFIG_DIR/collections/foo.yaml"
174174
rune -0 cscli hub upgrade
175+
assert_stderr --partial 'collections:foo.yaml - not downloading local item'
176+
175177
assert_output - <<-EOT
176-
collections:foo.yaml - not downloading local item
177178
Action plan:
178179
🔄 check & update data files
179180
EOT

test/bats/20_hub_items.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,29 @@ teardown() {
155155
rune -0 mkdir -p "$CONFIG_DIR/scenarios"
156156
rune -0 touch "$CONFIG_DIR/scenarios/foobar.yaml"
157157
rune -0 cscli scenarios remove foobar.yaml
158+
assert_stderr --partial 'scenarios:foobar.yaml is a local item, please delete manually'
158159
assert_output - <<-EOT
159-
WARN scenarios:foobar.yaml is a local item, please delete manually
160160
Nothing to do.
161161
EOT
162162
rune -0 cscli scenarios remove foobar.yaml --purge
163+
assert_stderr --partial 'scenarios:foobar.yaml is a local item, please delete manually'
163164
assert_output - <<-EOT
164-
WARN scenarios:foobar.yaml is a local item, please delete manually
165165
Nothing to do.
166166
EOT
167167
rune -0 cscli scenarios remove foobar.yaml --force
168+
assert_stderr --partial 'scenarios:foobar.yaml is a local item, please delete manually'
168169
assert_output - <<-EOT
169-
WARN scenarios:foobar.yaml is a local item, please delete manually
170170
Nothing to do.
171171
EOT
172172

173173
rune -0 cscli scenarios install crowdsecurity/ssh-bf
174174

175175
rune -0 cscli scenarios remove --all
176-
assert_line "WARN scenarios:foobar.yaml is a local item, please delete manually"
177176
assert_line "disabling scenarios:crowdsecurity/ssh-bf"
177+
assert_stderr --partial "scenarios:foobar.yaml is a local item, please delete manually"
178178

179179
rune -0 cscli scenarios remove --all --purge
180-
assert_line "WARN scenarios:foobar.yaml is a local item, please delete manually"
180+
assert_stderr --partial "scenarios:foobar.yaml is a local item, please delete manually"
181181
assert_line "purging scenarios:crowdsecurity/ssh-bf"
182182
}
183183

test/bats/cscli-hubtype-install.bats

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,16 @@ get_latest_version() {
166166

167167
rune -0 cscli parsers install crowdsecurity/whitelists --dry-run
168168
assert_output - --stderr <<-EOT
169-
WARN parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite
170169
Nothing to do.
171170
EOT
172-
refute_stderr
171+
assert_stderr --partial "parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite"
173172

174173
# XXX should this fail with status 1 instead?
175174
rune -0 cscli parsers install crowdsecurity/whitelists
176175
assert_output - <<-EOT
177-
WARN parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite
178176
Nothing to do.
179177
EOT
180-
refute_stderr
178+
assert_stderr --partial "parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite"
181179

182180
rune -0 cscli parsers install crowdsecurity/whitelists --force
183181
latest_whitelists=$(get_latest_version parsers crowdsecurity/whitelists)
@@ -230,17 +228,14 @@ get_latest_version() {
230228
# and maybe re-evaluate the --ignore flag
231229
rune -0 cscli parsers install crowdsecurity/whitelists --ignore
232230
assert_output - <<-EOT
233-
WARN parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite
234231
Nothing to do.
235232
EOT
236-
refute_stderr
233+
assert_stderr --partial "parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite"
237234

238235
# error on one item, should still install the others
239236
rune -0 cscli parsers install crowdsecurity/whitelists crowdsecurity/pgsql-logs --ignore
240-
refute_stderr
241237
latest_pgsql=$(get_latest_version parsers crowdsecurity/pgsql-logs)
242238
assert_output - <<-EOT
243-
WARN parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite
244239
Action plan:
245240
📥 download
246241
parsers: crowdsecurity/pgsql-logs ($latest_pgsql)
@@ -252,6 +247,7 @@ get_latest_version() {
252247
253248
$RELOAD_MESSAGE
254249
EOT
250+
assert_stderr --partial "parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite"
255251
rune -0 cscli parsers inspect crowdsecurity/pgsql-logs --no-metrics -o json
256252
rune -0 jq -e '.installed==true' <(output)
257253
}
@@ -268,14 +264,14 @@ get_latest_version() {
268264

269265
# attempt to install from hub
270266
rune -0 cscli parsers install crowdsecurity/sshd-logs
271-
assert_line 'parsers:crowdsecurity/sshd-logs - not downloading local item'
267+
assert_stderr --partial 'parsers:crowdsecurity/sshd-logs - not downloading local item'
272268
rune -0 cscli parsers list -o json
273269
rune -0 jq -c '.parsers[] | [.name,.status]' <(output)
274270
assert_json '["crowdsecurity/sshd-logs","enabled,local"]'
275271

276272
# attempt to install from a collection
277273
rune -0 cscli collections install crowdsecurity/sshd
278-
assert_line 'parsers:crowdsecurity/sshd-logs - not downloading local item'
274+
assert_stderr --partial 'parsers:crowdsecurity/sshd-logs - not downloading local item'
279275

280276
# verify it installed the rest of the collection
281277
assert_line 'enabling contexts:crowdsecurity/bf_base'
@@ -293,11 +289,11 @@ get_latest_version() {
293289

294290
# attempt to install from hub
295291
rune -0 cscli parsers install crowdsecurity/sshd-logs
296-
assert_line 'parsers:crowdsecurity/sshd-logs - not downloading local item'
292+
assert_stderr --partial 'parsers:crowdsecurity/sshd-logs - not downloading local item'
297293

298294
# attempt to install from a collection
299295
rune -0 cscli collections install crowdsecurity/sshd
300-
assert_line 'parsers:crowdsecurity/sshd-logs - not downloading local item'
296+
assert_stderr --partial 'parsers:crowdsecurity/sshd-logs - not downloading local item'
301297

302298
# verify it installed the rest of the collection
303299
assert_line 'enabling contexts:crowdsecurity/bf_base'

test/bats/cscli-hubtype-upgrade.bats

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,15 @@ get_latest_version() {
165165

166166
rune -0 cscli parsers upgrade crowdsecurity/whitelists --dry-run
167167
assert_output - <<-EOT
168-
WARN parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite
169168
Nothing to do.
170169
EOT
171-
refute_stderr
170+
assert_stderr --partial "parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite"
172171

173172
rune -0 cscli parsers upgrade crowdsecurity/whitelists
174173
assert_output - <<-EOT
175-
WARN parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite
176174
Nothing to do.
177175
EOT
178-
refute_stderr
176+
assert_stderr --partial "parsers:crowdsecurity/whitelists is tainted, use '--force' to overwrite"
179177

180178
latest_whitelists=$(get_latest_version parsers crowdsecurity/whitelists)
181179

test/lib/setup_file.sh

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -200,50 +200,6 @@ is_stdin_empty() {
200200
}
201201
export -f is_stdin_empty
202202

203-
assert_stderr() {
204-
# it is never useful to call this without arguments
205-
if [[ "$#" -eq 0 ]]; then
206-
# maybe the caller forgot to use '-' with an heredoc
207-
if ! is_stdin_empty; then
208-
fail "${FUNCNAME[0]}: called with stdin and no arguments (heredoc?)"
209-
fi
210-
fail "${FUNCNAME[0]}: called with no arguments"
211-
fi
212-
213-
local oldout="${output}"
214-
run -0 echo "${stderr}"
215-
assert_output "$@"
216-
output="${oldout}"
217-
}
218-
export -f assert_stderr
219-
220-
# like refute_output, but for stderr
221-
refute_stderr() {
222-
# calling this without arguments is ok, as long as stdin in empty
223-
if ! is_stdin_empty; then
224-
fail "${FUNCNAME[0]}: called with stdin (heredoc?)"
225-
fi
226-
227-
local oldout="${output}"
228-
run -0 echo "${stderr}"
229-
refute_output "$@"
230-
output="${oldout}"
231-
}
232-
export -f refute_stderr
233-
234-
# like assert_output, but for stderr
235-
assert_stderr_line() {
236-
if [[ "$#" -eq 0 ]]; then
237-
fail "${FUNCNAME[0]}: called with no arguments"
238-
fi
239-
240-
local oldout="${output}"
241-
run -0 echo "${stderr}"
242-
assert_line "$@"
243-
output="${oldout}"
244-
}
245-
export -f assert_stderr_line
246-
247203
# remove all installed items and data
248204
hub_purge_all() {
249205
local CONFIG_DIR

0 commit comments

Comments
 (0)