Skip to content

Commit c765a19

Browse files
fix cli logger (#236)
* fix logger * bump
1 parent 40e4c9e commit c765a19

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.216
1+
VERSION=v0.0.217
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.216/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.217/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.216/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.217/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.0
8-
version: 0.0.216
8+
version: 0.0.217
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/log/logrus.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,46 @@ func GetLogrusEntry(l Logger) (*logrus.Entry, error) {
6262
return adpt.Entry, nil
6363
}
6464

65+
func initCommands(cmds []*cobra.Command, initFunc func(*cobra.Command)) {
66+
for _, cmd := range cmds {
67+
initFunc(cmd)
68+
if cmd.HasSubCommands() {
69+
initCommands(cmd.Commands(), initFunc)
70+
}
71+
}
72+
}
73+
6574
func (l *logrusAdapter) AddPFlags(cmd *cobra.Command) {
6675
flags := pflag.NewFlagSet("logrus", pflag.ContinueOnError)
6776
flags.StringVar(&l.c.Level, "log-level", l.c.Level, `set the log level, e.g. "debug", "info", "warn", "error"`)
6877
format := flags.String("log-format", defaultFormatter, `set the log format: "text", "json"`)
6978

7079
cmd.PersistentFlags().AddFlagSet(flags)
71-
orgPreRun := cmd.PersistentPreRunE
72-
73-
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
74-
switch *format {
75-
case string(FormatterJSON), string(FormatterText):
76-
l.c.Format = LogrusFormatter(*format)
77-
default:
78-
return fmt.Errorf("invalid log format: %s", *format)
79-
}
8080

81-
if err := l.configure(flags); err != nil {
82-
return err
83-
}
84-
85-
if orgPreRun != nil {
86-
return orgPreRun(cmd, args)
81+
initFunc := func(cmd *cobra.Command) {
82+
orgPreRun := cmd.PreRunE
83+
84+
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
85+
switch *format {
86+
case string(FormatterJSON), string(FormatterText):
87+
l.c.Format = LogrusFormatter(*format)
88+
default:
89+
return fmt.Errorf("invalid log format: %s", *format)
90+
}
91+
92+
if err := l.configure(flags); err != nil {
93+
return err
94+
}
95+
96+
if orgPreRun != nil {
97+
return orgPreRun(cmd, args)
98+
}
99+
return nil
87100
}
88-
return nil
89101
}
90102

103+
cobra.OnInitialize(func() { initCommands(cmd.Commands(), initFunc) })
104+
91105
cmdutil.LogFormat = *format
92106
cmdutil.LogLevel = l.c.Level
93107
}

0 commit comments

Comments
 (0)