@@ -69,22 +69,21 @@ func Execute() {
6969
7070func init () {
7171 cobra .OnInitialize (initConfig )
72+ registerRootPersistentFlags ()
73+ SetUpViper ()
74+ }
7275
73- // Here you will define your flags and configuration settings.
74- // Cobra supports persistent flags, which, if defined here,
75- // will be global for your application.
76-
76+ func defaultConfigHome () string {
7777 home := "$HOME"
78-
7978 if homeString , err := homedir .Dir (); err == nil {
8079 if expandedHomeString , err := homedir .Expand (homeString ); err == nil {
8180 home = expandedHomeString
8281 }
8382 }
83+ return home
84+ }
8485
85- rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "config file (default is " + filepath .Join (home , ".link-checker-service.toml)" ))
86-
87- // HTTP client
86+ func registerHTTPClientPersistentFlags () {
8887 rootCmd .PersistentFlags ().StringP (proxyKey , "" , "" , "HTTP client: proxy server to use, e.g. http://myproxy:8080" )
8988 _ = viper .BindPFlag (proxyKey , rootCmd .PersistentFlags ().Lookup (proxyKey ))
9089 rootCmd .PersistentFlags ().StringP (pacScriptURLKey , "" , "" , "HTTP client: PAC script URL, e.g. http://myproxy/proxy.pac" )
@@ -106,11 +105,9 @@ func init() {
106105 _ = viper .BindPFlag (httpClientMapKey + enableRequestTracingKey , rootCmd .PersistentFlags ().Lookup (enableRequestTracingKey ))
107106 rootCmd .PersistentFlags ().Uint (limitBodyToNBytesKey , 0 , "HTTP client: maximum number of bytes to read from the body when searching for patterns. Unlimited if 0!" )
108107 _ = viper .BindPFlag (httpClientMapKey + limitBodyToNBytesKey , rootCmd .PersistentFlags ().Lookup (limitBodyToNBytesKey ))
109- // service
110- rootCmd .PersistentFlags ().UintP (maxConcurrentHTTPRequestsKey , "c" , 256 , "maximum number of total concurrent HTTP requests" )
111- _ = viper .BindPFlag (maxConcurrentHTTPRequestsKey , rootCmd .PersistentFlags ().Lookup (maxConcurrentHTTPRequestsKey ))
108+ }
112109
113- // cache
110+ func registerCachePersistentFlags () {
114111 rootCmd .PersistentFlags ().String (cacheExpirationIntervalKey , "24h" , "Expire each URL check result after <interval> (in ns/us/ms/s/m/h)" )
115112 _ = viper .BindPFlag (cacheExpirationIntervalKey , rootCmd .PersistentFlags ().Lookup (cacheExpirationIntervalKey ))
116113 rootCmd .PersistentFlags ().String (cacheCleanupIntervalKey , "48h" , "Interval between cache cleanups (in ns/us/ms/s/m/h)" )
@@ -121,6 +118,11 @@ func init() {
121118 _ = viper .BindPFlag (cacheMaxSizeKey , rootCmd .PersistentFlags ().Lookup (cacheMaxSizeKey ))
122119 rootCmd .PersistentFlags ().Int64 (cacheNumCountersKey , 10_000_000 , "Number of 4-bit access counters. Set at approx 10x max unique expected URLs (when cacheUseRistretto enabled)" )
123120 _ = viper .BindPFlag (cacheNumCountersKey , rootCmd .PersistentFlags ().Lookup (cacheNumCountersKey ))
121+ }
122+
123+ func registerServicePersistentFlags () {
124+ rootCmd .PersistentFlags ().UintP (maxConcurrentHTTPRequestsKey , "c" , 256 , "maximum number of total concurrent HTTP requests" )
125+ _ = viper .BindPFlag (maxConcurrentHTTPRequestsKey , rootCmd .PersistentFlags ().Lookup (maxConcurrentHTTPRequestsKey ))
124126
125127 rootCmd .PersistentFlags ().String (retryFailedAfterKey , "30s" , "If a URL check failed, e.g. intermittently, re-run it after <interval> (in ns/us/ms/s/m/h)" )
126128 _ = viper .BindPFlag (retryFailedAfterKey , rootCmd .PersistentFlags ().Lookup (retryFailedAfterKey ))
@@ -137,12 +139,14 @@ func init() {
137139 rootCmd .PersistentFlags ().StringSliceP (urlCheckerPluginsKey , "p" , []string {"urlcheck" },
138140 "provide a list of URL checkers. Additionally, 'urlcheck-noproxy' can be used if a proxy is defined, and an additional check without a proxy makes sense. The argument sequence is the checker sequence." )
139141 _ = viper .BindPFlag (urlCheckerPluginsKey , rootCmd .PersistentFlags ().Lookup (urlCheckerPluginsKey ))
142+ }
140143
141- SetUpViper ()
142-
143- // Cobra also supports local flags, which will only run
144- // when this action is called directly.
145- // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
144+ func registerRootPersistentFlags () {
145+ home := defaultConfigHome ()
146+ rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "config file (default is " + filepath .Join (home , ".link-checker-service.toml)" ))
147+ registerHTTPClientPersistentFlags ()
148+ registerCachePersistentFlags ()
149+ registerServicePersistentFlags ()
146150}
147151
148152// SetUpViper configures environment variable and global flag handling
0 commit comments