Skip to content

Commit fdf964f

Browse files
committed
fix: all linter warnings
1 parent 53acf9d commit fdf964f

11 files changed

Lines changed: 278 additions & 200 deletions

cmd/root.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,21 @@ func Execute() {
6969

7070
func 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

cmd/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func fetchConfig() {
6969
if viper.Get(domainBlacklistGlobsKey) != nil {
7070
g := viper.GetStringSlice(domainBlacklistGlobsKey)
7171
// empty string slice config creates a single slice with a "[]" -> fix
72-
if g != nil && !(len(g) == 1 && g[0] == "[]") {
72+
if g != nil && (len(g) != 1 || g[0] != "[]") {
7373
domainBlacklistGlobs = viper.GetStringSlice(domainBlacklistGlobsKey)
7474
}
7575
}

infrastructure/cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func (c defaultCache) Get(url string) (*URLCheckResult, bool) {
5050
value, found := c.cache.Get(url)
5151

5252
if found {
53-
return value.(*URLCheckResult), true
53+
if v, ok := value.(*URLCheckResult); ok {
54+
return v, true
55+
}
5456
}
5557

5658
return nil, false

infrastructure/domain_rate_limited_checker.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ func (c *DomainRateLimitedChecker) CheckURL(ctx context.Context, url string) *UR
4949
if limiterInstance, ok := c.domains.Load(key); !ok {
5050
limiter = rate.NewLimiter(c.ratePerSecond /*per second*/, 1 /*burst*/)
5151
} else {
52-
limiter = limiterInstance.(*rate.Limiter)
52+
l, typeOK := limiterInstance.(*rate.Limiter)
53+
if !typeOK {
54+
limiter = rate.NewLimiter(c.ratePerSecond, 1)
55+
c.domains.Store(key, limiter)
56+
} else {
57+
limiter = l
58+
}
5359
}
5460
if err := limiter.Wait(ctx); err != nil {
5561
nowEpoch := time.Now().Unix()
@@ -58,7 +64,7 @@ func (c *DomainRateLimitedChecker) CheckURL(ctx context.Context, url string) *UR
5864
return &URLCheckResult{
5965
Status: Dropped,
6066
Code: CustomHTTPErrorCode,
61-
Error: fmt.Errorf("domain rate limiter aborted: %v", err),
67+
Error: fmt.Errorf("domain rate limiter aborted: %w", err),
6268
FetchedAtEpochSeconds: nowEpoch,
6369
BodyPatternsFound: []string{},
6470
}

0 commit comments

Comments
 (0)