MCPcopy Index your code
hub / github.com/crowdsecurity/crowdsec / initialize

Method initialize

cmd/crowdsec-cli/main.go:123–186  ·  view source on GitHub ↗

initialize is called before the subcommand is executed.

()

Source from the content-addressed store, hash-verified

121
122// initialize is called before the subcommand is executed.
123func (cli *cliRoot) initialize() error {
124 var err error
125
126 // log level in cscli always comes from flags,
127 // the logging options in config.yaml are ignored
128
129 log.SetLevel(cli.logFlags.Level())
130 log.SetFormatter(&log.TextFormatter{
131 DisableTimestamp: true,
132 DisableLevelTruncation: true,
133 })
134
135 csConfig, mergedConfig, err = loadConfigFor(os.Args[1])
136 if err != nil {
137 return err
138 }
139
140 // recap of the enabled feature flags, because logging
141 // was not enabled when we set them from envvars
142 if fflist := csconfig.ListFeatureFlags(); fflist != "" {
143 log.Debugf("Enabled feature flags: %s", fflist)
144 }
145
146 if cli.flagBranch != "" {
147 csConfig.Cscli.HubBranch = cli.flagBranch
148 }
149
150 if cli.outputFormat != "" {
151 csConfig.Cscli.Output = cli.outputFormat
152 }
153
154 if csConfig.Cscli.Output == "" {
155 csConfig.Cscli.Output = "human"
156 }
157
158 if csConfig.Cscli.Output != "human" && csConfig.Cscli.Output != "json" && csConfig.Cscli.Output != "raw" {
159 return fmt.Errorf("output format '%s' not supported: must be one of human, json, raw", csConfig.Cscli.Output)
160 }
161
162 if csConfig.Cscli.Output == "json" {
163 log.SetFormatter(&log.JSONFormatter{})
164 log.SetLevel(log.ErrorLevel)
165 } else if csConfig.Cscli.Output == "raw" {
166 log.SetLevel(log.ErrorLevel)
167 }
168
169 if cli.outputColor != "" {
170 csConfig.Cscli.Color = cli.outputColor
171
172 if cli.outputColor != "yes" && cli.outputColor != "no" && cli.outputColor != "auto" {
173 return fmt.Errorf("output color '%s' not supported: must be one of yes, no, auto", cli.outputColor)
174 }
175
176 if cli.outputColor == "no" {
177 os.Setenv("NO_COLOR", "1")
178 }
179 }
180

Callers 1

NewCommandMethod · 0.95

Calls 4

ListFeatureFlagsFunction · 0.92
loadConfigForFunction · 0.85
SetLevelMethod · 0.80
LevelMethod · 0.80

Tested by

no test coverage detected