Use path and filepath operation appropriately.

path methods are used for url operations.
filepath methods are used for file system paths and its cross platform.
Remove strings.HasSuffix and used filepath.Ext and path.Ext for file and url extenstions respectively
This commit is contained in:
Gofastasf 2025-02-01 14:51:06 +00:00
parent 2feccd772c
commit 1577e0a77f
6 changed files with 12 additions and 13 deletions

View File

@ -7,7 +7,7 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path" "path/filepath"
"text/template" "text/template"
homedir "github.com/mitchellh/go-homedir" homedir "github.com/mitchellh/go-homedir"
@ -57,7 +57,7 @@ func (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error {
fileMode = st.FileMode fileMode = st.FileMode
} }
plistFolder := path.Dir(resolvedPath) plistFolder := filepath.Dir(resolvedPath)
err = os.MkdirAll(plistFolder, 0o755) err = os.MkdirAll(plistFolder, 0o755)
if err != nil { if err != nil {
return fmt.Errorf("error creating %s: %v", plistFolder, err) return fmt.Errorf("error creating %s: %v", plistFolder, err)

View File

@ -94,7 +94,7 @@ func (sc *subcommandContext) readTunnelCredentials(credFinder CredFinder) (conne
var credentials connection.Credentials var credentials connection.Credentials
if err = json.Unmarshal(body, &credentials); err != nil { if err = json.Unmarshal(body, &credentials); err != nil {
if strings.HasSuffix(filePath, ".pem") { if filepath.Ext(filePath) == ".pem" {
return connection.Credentials{}, fmt.Errorf("The tunnel credentials file should be .json but you gave a .pem. " + return connection.Credentials{}, fmt.Errorf("The tunnel credentials file should be .json but you gave a .pem. " +
"The tunnel credentials file was originally created by `cloudflared tunnel create`. " + "The tunnel credentials file was originally created by `cloudflared tunnel create`. " +
"You may have accidentally used the filepath to cert.pem, which is generated by `cloudflared tunnel " + "You may have accidentally used the filepath to cert.pem, which is generated by `cloudflared tunnel " +

View File

@ -10,9 +10,9 @@ import (
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"text/template" "text/template"
"time" "time"
@ -198,7 +198,7 @@ func download(url, filepath string, isCompressed bool) error {
// isCompressedFile is a really simple file extension check to see if this is a macos tar and gzipped // isCompressedFile is a really simple file extension check to see if this is a macos tar and gzipped
func isCompressedFile(urlstring string) bool { func isCompressedFile(urlstring string) bool {
if strings.HasSuffix(urlstring, ".tgz") { if path.Ext(urlstring) == ".tgz" {
return true return true
} }
@ -206,7 +206,7 @@ func isCompressedFile(urlstring string) bool {
if err != nil { if err != nil {
return false return false
} }
return strings.HasSuffix(u.Path, ".tgz") return path.Ext(u.Path) == ".tgz"
} }
// writeBatchFile writes a batch file out to disk // writeBatchFile writes a batch file out to disk

View File

@ -3,7 +3,7 @@ package credentials
import ( import (
"io/fs" "io/fs"
"os" "os"
"path" "path/filepath"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -13,7 +13,7 @@ func TestCredentialsRead(t *testing.T) {
file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem") file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem")
require.NoError(t, err) require.NoError(t, err)
dir := t.TempDir() dir := t.TempDir()
certPath := path.Join(dir, originCertFile) certPath := filepath.Join(dir, originCertFile)
os.WriteFile(certPath, file, fs.ModePerm) os.WriteFile(certPath, file, fs.ModePerm)
user, err := Read(certPath, &nopLog) user, err := Read(certPath, &nopLog)
require.NoError(t, err) require.NoError(t, err)

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path" "path/filepath"
"testing" "testing"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -95,7 +95,7 @@ func TestFindOriginCert_Valid(t *testing.T) {
file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem") file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem")
require.NoError(t, err) require.NoError(t, err)
dir := t.TempDir() dir := t.TempDir()
certPath := path.Join(dir, originCertFile) certPath := filepath.Join(dir, originCertFile)
os.WriteFile(certPath, file, fs.ModePerm) os.WriteFile(certPath, file, fs.ModePerm)
path, err := FindOriginCert(certPath, &nopLog) path, err := FindOriginCert(certPath, &nopLog)
require.NoError(t, err) require.NoError(t, err)
@ -104,7 +104,7 @@ func TestFindOriginCert_Valid(t *testing.T) {
func TestFindOriginCert_Missing(t *testing.T) { func TestFindOriginCert_Missing(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
certPath := path.Join(dir, originCertFile) certPath := filepath.Join(dir, originCertFile)
_, err := FindOriginCert(certPath, &nopLog) _, err := FindOriginCert(certPath, &nopLog)
require.Error(t, err) require.Error(t, err)
} }

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"sync" "sync"
"time" "time"
@ -257,7 +256,7 @@ func createRollingLogger(config RollingConfig) (io.Writer, error) {
} }
rotatingFileInit.writer = &lumberjack.Logger{ rotatingFileInit.writer = &lumberjack.Logger{
Filename: path.Join(config.Dirname, config.Filename), Filename: filepath.Join(config.Dirname, config.Filename),
MaxBackups: config.maxBackups, MaxBackups: config.maxBackups,
MaxSize: config.maxSize, MaxSize: config.maxSize,
MaxAge: config.maxAge, MaxAge: config.maxAge,