From 553e77e061d5c1a0079bbdd99afdfdc85c03f7db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20=22Pisco=22=20Fernandes?= Date: Tue, 1 Apr 2025 18:51:40 +0100 Subject: [PATCH] chore: fix linter rules --- cmd/cloudflared/generic_service.go | 2 + cmd/cloudflared/service_template.go | 80 +------------------ cmd/cloudflared/tunnel/teamnet_subcommands.go | 6 +- cmd/cloudflared/updater/workers_update.go | 5 +- credentials/credentials_test.go | 2 +- management/events.go | 2 +- retry/backoffhandler.go | 8 +- 7 files changed, 15 insertions(+), 90 deletions(-) diff --git a/cmd/cloudflared/generic_service.go b/cmd/cloudflared/generic_service.go index 764d362f..bd49d2b4 100644 --- a/cmd/cloudflared/generic_service.go +++ b/cmd/cloudflared/generic_service.go @@ -7,6 +7,8 @@ import ( "os" cli "github.com/urfave/cli/v2" + + "github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil" ) func runApp(app *cli.App, graceShutdownC chan struct{}) { diff --git a/cmd/cloudflared/service_template.go b/cmd/cloudflared/service_template.go index 725d1f86..5fb9ae12 100644 --- a/cmd/cloudflared/service_template.go +++ b/cmd/cloudflared/service_template.go @@ -1,8 +1,8 @@ package main import ( - "bufio" "bytes" + "errors" "fmt" "io" "os" @@ -44,7 +44,7 @@ func (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error { return err } if _, err = os.Stat(resolvedPath); err == nil { - return fmt.Errorf(serviceAlreadyExistsWarn(resolvedPath)) + return errors.New(serviceAlreadyExistsWarn(resolvedPath)) } var buffer bytes.Buffer @@ -118,49 +118,6 @@ func ensureConfigDirExists(configDir string) error { return err } -// openFile opens the file at path. If create is set and the file exists, returns nil, true, nil -func openFile(path string, create bool) (file *os.File, exists bool, err error) { - expandedPath, err := homedir.Expand(path) - if err != nil { - return nil, false, err - } - if create { - fileInfo, err := os.Stat(expandedPath) - if err == nil && fileInfo.Size() > 0 { - return nil, true, nil - } - file, err = os.OpenFile(expandedPath, os.O_RDWR|os.O_CREATE, 0600) - } else { - file, err = os.Open(expandedPath) - } - return file, false, err -} - -func copyCredential(srcCredentialPath, destCredentialPath string) error { - destFile, exists, err := openFile(destCredentialPath, true) - if err != nil { - return err - } else if exists { - // credentials already exist, do nothing - return nil - } - defer destFile.Close() - - srcFile, _, err := openFile(srcCredentialPath, false) - if err != nil { - return err - } - defer srcFile.Close() - - // Copy certificate - _, err = io.Copy(destFile, srcFile) - if err != nil { - return fmt.Errorf("unable to copy %s to %s: %v", srcCredentialPath, destCredentialPath, err) - } - - return nil -} - func copyFile(src, dest string) error { srcFile, err := os.Open(src) if err != nil { @@ -187,36 +144,3 @@ func copyFile(src, dest string) error { ok = true return nil } - -func copyConfig(srcConfigPath, destConfigPath string) error { - // Copy or create config - destFile, exists, err := openFile(destConfigPath, true) - if err != nil { - return fmt.Errorf("cannot open %s with error: %s", destConfigPath, err) - } else if exists { - // config already exists, do nothing - return nil - } - defer destFile.Close() - - srcFile, _, err := openFile(srcConfigPath, false) - if err != nil { - fmt.Println("Your service needs a config file that at least specifies the hostname option.") - fmt.Println("Type in a hostname now, or leave it blank and create the config file later.") - fmt.Print("Hostname: ") - reader := bufio.NewReader(os.Stdin) - input, _ := reader.ReadString('\n') - if input == "" { - return err - } - fmt.Fprintf(destFile, "hostname: %s\n", input) - } else { - defer srcFile.Close() - _, err = io.Copy(destFile, srcFile) - if err != nil { - return fmt.Errorf("unable to copy %s to %s: %v", srcConfigPath, destConfigPath, err) - } - } - - return nil -} diff --git a/cmd/cloudflared/tunnel/teamnet_subcommands.go b/cmd/cloudflared/tunnel/teamnet_subcommands.go index 9eb5dcfb..9134e280 100644 --- a/cmd/cloudflared/tunnel/teamnet_subcommands.go +++ b/cmd/cloudflared/tunnel/teamnet_subcommands.go @@ -22,7 +22,7 @@ var ( Usage: "The ID or name of the virtual network to which the route is associated to.", } - routeAddError = errors.New("You must supply exactly one argument, the ID or CIDR of the route you want to delete") + errAddRoute = errors.New("You must supply exactly one argument, the ID or CIDR of the route you want to delete") ) func buildRouteIPSubcommand() *cli.Command { @@ -187,7 +187,7 @@ func deleteRouteCommand(c *cli.Context) error { } if c.NArg() != 1 { - return routeAddError + return errAddRoute } var routeId uuid.UUID @@ -195,7 +195,7 @@ func deleteRouteCommand(c *cli.Context) error { if err != nil { _, network, err := net.ParseCIDR(c.Args().First()) if err != nil || network == nil { - return routeAddError + return errAddRoute } var vnetId *uuid.UUID diff --git a/cmd/cloudflared/updater/workers_update.go b/cmd/cloudflared/updater/workers_update.go index 1ab5062f..a43792cb 100644 --- a/cmd/cloudflared/updater/workers_update.go +++ b/cmd/cloudflared/updater/workers_update.go @@ -134,7 +134,7 @@ func (v *WorkersVersion) Apply() error { if err := os.Rename(newFilePath, v.targetPath); err != nil { //attempt rollback - os.Rename(oldFilePath, v.targetPath) + _ = os.Rename(oldFilePath, v.targetPath) return err } os.Remove(oldFilePath) @@ -181,7 +181,7 @@ func download(url, filepath string, isCompressed bool) error { tr := tar.NewReader(gr) // advance the reader pass the header, which will be the single binary file - tr.Next() + _, _ = tr.Next() r = tr } @@ -249,7 +249,6 @@ func runWindowsBatch(batchFile string) error { if exitError, ok := err.(*exec.ExitError); ok { return fmt.Errorf("Error during update : %s;", string(exitError.Stderr)) } - } return err } diff --git a/credentials/credentials_test.go b/credentials/credentials_test.go index 8132b196..f4981dfa 100644 --- a/credentials/credentials_test.go +++ b/credentials/credentials_test.go @@ -14,7 +14,7 @@ func TestCredentialsRead(t *testing.T) { require.NoError(t, err) dir := t.TempDir() certPath := filepath.Join(dir, originCertFile) - os.WriteFile(certPath, file, fs.ModePerm) + _ = os.WriteFile(certPath, file, fs.ModePerm) user, err := Read(certPath, &nopLog) require.NoError(t, err) require.Equal(t, certPath, user.CertPath()) diff --git a/management/events.go b/management/events.go index 2983f875..306a1dde 100644 --- a/management/events.go +++ b/management/events.go @@ -74,7 +74,7 @@ type EventLog struct { type LogEventType int8 const ( - // Cloudflared events are signficant to cloudflared operations like connection state changes. + // Cloudflared events are significant to cloudflared operations like connection state changes. // Cloudflared is also the default event type for any events that haven't been separated into a proper event type. Cloudflared LogEventType = iota HTTP diff --git a/retry/backoffhandler.go b/retry/backoffhandler.go index 945541a3..a2647ecb 100644 --- a/retry/backoffhandler.go +++ b/retry/backoffhandler.go @@ -79,8 +79,8 @@ func (b *BackoffHandler) BackoffTimer() <-chan time.Time { } else { b.retries++ } - maxTimeToWait := time.Duration(b.GetBaseTime() * 1 << (b.retries)) - timeToWait := time.Duration(rand.Int63n(maxTimeToWait.Nanoseconds())) + maxTimeToWait := b.GetBaseTime() * (1 << b.retries) + timeToWait := time.Duration(rand.Int63n(maxTimeToWait.Nanoseconds())) // #nosec G404 return b.Clock.After(timeToWait) } @@ -103,7 +103,7 @@ func (b *BackoffHandler) Backoff(ctx context.Context) bool { // period expires, the number of retries & backoff duration is reset. func (b *BackoffHandler) SetGracePeriod() time.Duration { maxTimeToWait := b.GetBaseTime() * 2 << (b.retries + 1) - timeToWait := time.Duration(rand.Int63n(maxTimeToWait.Nanoseconds())) + timeToWait := time.Duration(rand.Int63n(maxTimeToWait.Nanoseconds())) // #nosec G404 b.resetDeadline = b.Clock.Now().Add(timeToWait) return timeToWait @@ -118,7 +118,7 @@ func (b BackoffHandler) GetBaseTime() time.Duration { // Retries returns the number of retries consumed so far. func (b *BackoffHandler) Retries() int { - return int(b.retries) + return int(b.retries) // #nosec G115 } func (b *BackoffHandler) ReachedMaxRetries() bool {