LoadConfig returns a configuration parsed from configuration file
(configFile string, disableAgent bool, disableAPI bool, quiet bool)
| 91 | |
| 92 | // LoadConfig returns a configuration parsed from configuration file |
| 93 | func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*csconfig.Config, error) { |
| 94 | cConfig, _, err := csconfig.NewConfig(configFile, disableAgent, disableAPI, quiet) |
| 95 | if err != nil { |
| 96 | return nil, fmt.Errorf("while loading configuration file: %w", err) |
| 97 | } |
| 98 | |
| 99 | if err := trace.Init(filepath.Join(cConfig.ConfigPaths.DataDir, "trace")); err != nil { |
| 100 | return nil, fmt.Errorf("while setting up trace directory: %w", err) |
| 101 | } |
| 102 | |
| 103 | if flags.LogLevel != 0 { |
| 104 | cConfig.Common.LogLevel = flags.LogLevel |
| 105 | if cConfig.API != nil && cConfig.API.Server != nil { |
| 106 | cConfig.API.Server.LogLevel = flags.LogLevel |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if flags.haveTimeMachine() { |
| 111 | // in time-machine mode, we want to see what's happening |
| 112 | cConfig.Common.LogMedia = "stdout" |
| 113 | } |
| 114 | |
| 115 | if err := logging.SetupStandardLogger(cConfig.Common.LogConfig, cConfig.Common.LogLevel, cConfig.Common.ForceColorLogs); err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | |
| 119 | if cConfig.Common.LogMedia != "stdout" { |
| 120 | log.AddHook(newFatalHook()) |
| 121 | } |
| 122 | |
| 123 | if err := csconfig.LoadFeatureFlagsFile(configFile, log.StandardLogger()); err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | |
| 127 | if !cConfig.DisableAgent { |
| 128 | if err := cConfig.LoadCrowdsec(); err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if !cConfig.DisableAPI { |
| 134 | if err := cConfig.LoadAPIServer(false, false); err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if !cConfig.DisableAgent && (cConfig.API == nil || cConfig.API.Client == nil || cConfig.API.Client.Credentials == nil) { |
| 140 | return nil, errors.New("missing local API credentials for crowdsec agent, abort") |
| 141 | } |
| 142 | |
| 143 | if cConfig.DisableAPI && cConfig.DisableAgent { |
| 144 | return nil, errors.New("you must run at least the API Server or crowdsec") |
| 145 | } |
| 146 | |
| 147 | if flags.OneShotDSN != "" && flags.SingleFileType == "" { |
| 148 | return nil, errors.New("-dsn requires a -type argument") |
| 149 | } |
| 150 |
no test coverage detected
searching dependent graphs…