From 5d65957ba78f2e5e144fda634c653af4c4643fb2 Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Fri, 9 May 2025 14:49:04 +0200 Subject: [PATCH] chore: fix code quality --- book/checkout.go | 2 +- commands/local_new.go | 17 +++++++++-------- commands/local_proxy_start.go | 3 +-- envs/docker.go | 7 ++++--- envs/envs.go | 2 +- envs/local.go | 6 +++--- humanlog/symfony.go | 2 +- local/fcgi_client/fcgiclient.go | 2 +- local/html/html.go | 4 ++-- local/php/envs.go | 2 +- local/pid/pidfile.go | 2 +- local/proxy/config.go | 2 +- local/proxy/proxy.go | 11 ++++++----- 13 files changed, 32 insertions(+), 30 deletions(-) diff --git a/book/checkout.go b/book/checkout.go index 33c6b507..9cf9b3c4 100644 --- a/book/checkout.go +++ b/book/checkout.go @@ -37,7 +37,7 @@ func (b *Book) Checkout(step string) error { // FIXME: keep vendor/ node_modules/ around before git clean, but them back as they will be updated the right way, less Internet traffic // FIXME: if the checkout is to a later step, no need to remove the DB, we can just migrate it os.Chdir(b.Dir) - step = strings.Replace(step, ".", "-", -1) + step = strings.ReplaceAll(step, ".", "-") tag := fmt.Sprintf("step-%s", step) branch := "work-" + tag printBanner("[GIT] Check for not yet committed changes", b.Debug) diff --git a/commands/local_new.go b/commands/local_new.go index 114c600b..3ce21afc 100644 --- a/commands/local_new.go +++ b/commands/local_new.go @@ -307,21 +307,22 @@ func parseDockerComposeServices(dir string) []*CloudService { for _, service := range project.Services { for _, port := range service.Ports { var s *CloudService - if port.Target == 3306 { + switch port.Target { + case 3306: s = &CloudService{Type: "mysql"} - } else if port.Target == 5432 { + case 5432: s = &CloudService{Type: "postgresql"} - } else if port.Target == 6379 { + case 6379: s = &CloudService{Type: "redis"} - } else if port.Target == 11211 { + case 11211: s = &CloudService{Type: "memcached"} - } else if port.Target == 5672 { + case 5672: s = &CloudService{Type: "rabbitmq"} - } else if port.Target == 9200 { + case 9200: s = &CloudService{Type: "elasticsearch"} - } else if port.Target == 27017 { + case 27017: s = &CloudService{Type: "mongodb"} - } else if port.Target == 9092 { + case 9092: s = &CloudService{Type: "kafka"} } _, done := seen[service.Name] diff --git a/commands/local_proxy_start.go b/commands/local_proxy_start.go index 47f5d8b4..4287066c 100644 --- a/commands/local_proxy_start.go +++ b/commands/local_proxy_start.go @@ -112,8 +112,7 @@ var localProxyStartCmd = &console.Command{ if err != nil { return errors.WithStack(err) } - var lw io.Writer - lw = f + var lw io.Writer = f logger := zerolog.New(decorateLogger(lw, c.Bool("no-humanize"))).With().Timestamp().Logger() config, err := proxy.Load(homeDir) diff --git a/envs/docker.go b/envs/docker.go index 7efe2cb2..58a8b5e1 100644 --- a/envs/docker.go +++ b/envs/docker.go @@ -469,11 +469,12 @@ func (l *Local) dockerServiceToRelationship(client *docker.Client, container con "rel": "simple", } // Official HTTP(s) ports or well know alternatives - if p.PrivatePort == 80 || p.PrivatePort == 8008 || p.PrivatePort == 8080 || p.PrivatePort == 8081 { + switch p.PrivatePort { + case 80, 8008, 8080, 8081: rels[""]["scheme"] = "http" - } else if p.PrivatePort == 443 || p.PrivatePort == 8443 { + case 443, 8443: rels[""]["scheme"] = "https" - } else { + default: rels[""]["scheme"] = "tcp" } return rels diff --git a/envs/envs.go b/envs/envs.go index c36a2d1d..4bcb241d 100644 --- a/envs/envs.go +++ b/envs/envs.go @@ -121,7 +121,7 @@ func extractRelationshipsEnvs(env Environment) Envs { if i != 0 { prefix = fmt.Sprintf("%s_%d_", key, i) } - prefix = strings.Replace(prefix, "-", "_", -1) + prefix = strings.ReplaceAll(prefix, "-", "_") // HA support via scheme-replica isPostgreSQL := strings.HasPrefix(scheme.(string), "pgsql") diff --git a/envs/local.go b/envs/local.go index 46f5a3bc..649de5ef 100644 --- a/envs/local.go +++ b/envs/local.go @@ -68,7 +68,7 @@ func (l *Local) FindRelationshipPrefix(frel, fscheme string) string { if i != 0 { prefix = fmt.Sprintf("%s_%d_", key, i) } - return strings.Replace(prefix, "-", "_", -1) + return strings.ReplaceAll(prefix, "-", "_") } } } @@ -105,7 +105,7 @@ func (l *Local) FindServiceUrl(serviceOrRelationship string) (string, bool) { continue } - prefix := fmt.Sprintf("%s_", strings.Replace(strings.ToUpper(serviceOrRelationship), "-", "_", -1)) + prefix := fmt.Sprintf("%s_", strings.ReplaceAll(strings.ToUpper(serviceOrRelationship), "-", "_")) if i != 0 { prefix += fmt.Sprintf("%d_", i) } @@ -128,7 +128,7 @@ func (l *Local) FindServiceUrl(serviceOrRelationship string) (string, bool) { continue } - prefix := fmt.Sprintf("%s_", strings.Replace(strings.ToUpper(key), "-", "_", -1)) + prefix := fmt.Sprintf("%s_", strings.ReplaceAll(strings.ToUpper(key), "-", "_")) if i != 0 { prefix += fmt.Sprintf("%d_", i) } diff --git a/humanlog/symfony.go b/humanlog/symfony.go index 790ae425..53e79fcf 100644 --- a/humanlog/symfony.go +++ b/humanlog/symfony.go @@ -31,7 +31,7 @@ import ( // [2018-11-19 12:52:00] console.DEBUG: www {"xxx":"yyy","code":1} [] // or [2019-11-13T07:16:50.260544+01:00] console.DEBUG: www {"xxx":"yyy","code":1} [] -var symfonyLogLineRegexp = regexp.MustCompile("^\\[(\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}|\\d{4}\\-\\d{2}\\-\\d{2}T\\d{2}\\:\\d{2}\\:\\d{2}\\.\\d+\\+\\d{2}\\:\\d{2})\\] ([^\\.]+)\\.([^\\:]+)\\: (.+) (\\[.*?\\]|{.*?}) (\\[.*?\\]|{.*?})\\s*$") +var symfonyLogLineRegexp = regexp.MustCompile(`^\[(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}|\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}\.\d+\+\d{2}\:\d{2})\] ([^\.]+)\.([^\:]+)\: (.+) (\[.*?\]|{.*?}) (\[.*?\]|{.*?})\s*$`) func convertSymfonyLog(in []byte) (*line, error) { allMatches := symfonyLogLineRegexp.FindAllSubmatch(in, -1) diff --git a/local/fcgi_client/fcgiclient.go b/local/fcgi_client/fcgiclient.go index 77ff9da6..ea1146f9 100644 --- a/local/fcgi_client/fcgiclient.go +++ b/local/fcgi_client/fcgiclient.go @@ -251,7 +251,7 @@ type bufWriter struct { } func (w *bufWriter) Close() error { - if err := w.Writer.Flush(); err != nil { + if err := w.Flush(); err != nil { w.closer.Close() return errors.WithStack(err) } diff --git a/local/html/html.go b/local/html/html.go index b160213d..4d4bb39f 100644 --- a/local/html/html.go +++ b/local/html/html.go @@ -25,7 +25,7 @@ import ( ) func CreateAction(url, text string, args ...interface{}) string { - text = strings.Replace(text, "\n", "
", -1) + text = strings.ReplaceAll(text, "\n", "
") text = fmt.Sprintf(text, args...) return fmt.Sprintf(`
%s
`, url, text) } @@ -43,7 +43,7 @@ func CreateTerminal(text string, args ...interface{}) string { } func doCreateTerminal(text string, color string, args ...interface{}) string { - text = strings.Replace(text, "\n", "
", -1) + text = strings.ReplaceAll(text, "\n", "
") text = fmt.Sprintf(text, args...) return fmt.Sprintf(`
diff --git a/local/php/envs.go b/local/php/envs.go index 1465c552..9aaebb6b 100644 --- a/local/php/envs.go +++ b/local/php/envs.go @@ -80,7 +80,7 @@ func (p *Server) generateEnv(req *http.Request) map[string]string { // iterate over request headers and append them to the environment variables in the valid format for k, v := range req.Header { - key := strings.Replace(strings.ToUpper(k), "-", "_", -1) + key := strings.ReplaceAll(strings.ToUpper(k), "-", "_") // ignore HTTP_HOST -- see https://httpoxy.org/ if key == "HOST" { continue diff --git a/local/pid/pidfile.go b/local/pid/pidfile.go index 243a2012..db0ee4fd 100644 --- a/local/pid/pidfile.go +++ b/local/pid/pidfile.go @@ -146,7 +146,7 @@ func (p *PidFile) WaitForExit() error { select { case err := <-ch: return err - case _ = <-time.After(30 * time.Second): + case <-time.After(30 * time.Second): return errors.Errorf("Time out detected during \"%s\" process exit", p.ShortName()) } } diff --git a/local/proxy/config.go b/local/proxy/config.go index d5db1627..2e5028d1 100644 --- a/local/proxy/config.go +++ b/local/proxy/config.go @@ -282,7 +282,7 @@ func (c *Config) doNormalizeDomain(domain string) string { continue } // glob matching - if strings.HasSuffix(domain, strings.Replace(d, "*.", ".", -1)) { + if strings.HasSuffix(domain, strings.ReplaceAll(d, "*.", ".")) { m := d + "." + c.TLD // always use the longest possible domain for matching if len(m) > len(match) { diff --git a/local/proxy/proxy.go b/local/proxy/proxy.go index 50bfc571..2cdd1360 100644 --- a/local/proxy/proxy.go +++ b/local/proxy/proxy.go @@ -188,10 +188,11 @@ func New(config *Config, ca *cert.CA, logger *log.Logger, debug bool) *Proxy { } r.URL.Scheme = "http" r.URL.Host = r.Host - if r.URL.Path == "/proxy.pac" { + switch r.URL.Path { + case "/proxy.pac": p.servePacFile(w, r) return - } else if r.URL.Path == "/" { + case "/": p.serveIndex(w, r) return } @@ -310,7 +311,7 @@ $ symfony server:start --daemon --dir=%s`, } func (p *Proxy) Start() error { - go p.Config.Watch() + go p.Watch() return errors.WithStack(http.ListenAndServe(":"+strconv.Itoa(p.Port), p.proxy)) } @@ -322,7 +323,7 @@ func (p *Proxy) servePacFile(w http.ResponseWriter, r *http.Request) { // No need to fall back to p.Host and p.Port as r.Host is already checked // upper in the stacktrace. w.Header().Add("Content-Type", "application/x-ns-proxy-autoconfig") - w.Write([]byte(fmt.Sprintf(`// Only proxy *.%s requests + fmt.Fprintf(w, `// Only proxy *.%s requests // Configuration file in ~/.symfony5/proxy.json function FindProxyForURL (url, host) { if (dnsDomainIs(host, '.%s')) { @@ -335,7 +336,7 @@ function FindProxyForURL (url, host) { return 'DIRECT'; } -`, p.TLD, p.TLD, r.Host))) +`, p.TLD, p.TLD, r.Host) } func (p *Proxy) serveIndex(w http.ResponseWriter, r *http.Request) {