Skip to content

Commit 2827b2f

Browse files
authored
fix: Use path and filepath operation appropriately
Using path package methods can cause errors on windows machines. path methods are used for url operations and unix specific operation. filepath methods are used for file system paths and its cross platform. Remove strings.HasSuffix and use filepath.Ext and path.Ext for file and url extenstions respectively.
1 parent 6dc8ed7 commit 2827b2f

File tree

6 files changed

+12
-13
lines changed

6 files changed

+12
-13
lines changed

cmd/cloudflared/service_template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"io"
88
"os"
99
"os/exec"
10-
"path"
10+
"path/filepath"
1111
"text/template"
1212

1313
homedir "github.com/mitchellh/go-homedir"
@@ -57,7 +57,7 @@ func (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error {
5757
fileMode = st.FileMode
5858
}
5959

60-
plistFolder := path.Dir(resolvedPath)
60+
plistFolder := filepath.Dir(resolvedPath)
6161
err = os.MkdirAll(plistFolder, 0o755)
6262
if err != nil {
6363
return fmt.Errorf("error creating %s: %v", plistFolder, err)

cmd/cloudflared/tunnel/subcommand_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (sc *subcommandContext) readTunnelCredentials(credFinder CredFinder) (conne
112112

113113
var credentials connection.Credentials
114114
if err = json.Unmarshal(body, &credentials); err != nil {
115-
if strings.HasSuffix(filePath, ".pem") {
115+
if filepath.Ext(filePath) == ".pem" {
116116
return connection.Credentials{}, fmt.Errorf("The tunnel credentials file should be .json but you gave a .pem. " +
117117
"The tunnel credentials file was originally created by `cloudflared tunnel create`. " +
118118
"You may have accidentally used the filepath to cert.pem, which is generated by `cloudflared tunnel " +

cmd/cloudflared/updater/workers_update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"net/url"
1111
"os"
1212
"os/exec"
13+
"path"
1314
"path/filepath"
1415
"runtime"
15-
"strings"
1616
"text/template"
1717
"time"
1818

@@ -198,15 +198,15 @@ func download(url, filepath string, isCompressed bool) error {
198198

199199
// isCompressedFile is a really simple file extension check to see if this is a macos tar and gzipped
200200
func isCompressedFile(urlstring string) bool {
201-
if strings.HasSuffix(urlstring, ".tgz") {
201+
if path.Ext(urlstring) == ".tgz" {
202202
return true
203203
}
204204

205205
u, err := url.Parse(urlstring)
206206
if err != nil {
207207
return false
208208
}
209-
return strings.HasSuffix(u.Path, ".tgz")
209+
return path.Ext(u.Path) == ".tgz"
210210
}
211211

212212
// writeBatchFile writes a batch file out to disk

credentials/credentials_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package credentials
33
import (
44
"io/fs"
55
"os"
6-
"path"
6+
"path/filepath"
77
"testing"
88

99
"github.com/stretchr/testify/require"
@@ -13,7 +13,7 @@ func TestCredentialsRead(t *testing.T) {
1313
file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem")
1414
require.NoError(t, err)
1515
dir := t.TempDir()
16-
certPath := path.Join(dir, originCertFile)
16+
certPath := filepath.Join(dir, originCertFile)
1717
os.WriteFile(certPath, file, fs.ModePerm)
1818
user, err := Read(certPath, &nopLog)
1919
require.NoError(t, err)

credentials/origin_cert_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"io/fs"
66
"os"
7-
"path"
7+
"path/filepath"
88
"testing"
99

1010
"github.com/rs/zerolog"
@@ -63,7 +63,7 @@ func TestFindOriginCert_Valid(t *testing.T) {
6363
file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem")
6464
require.NoError(t, err)
6565
dir := t.TempDir()
66-
certPath := path.Join(dir, originCertFile)
66+
certPath := filepath.Join(dir, originCertFile)
6767
_ = os.WriteFile(certPath, file, fs.ModePerm)
6868
path, err := FindOriginCert(certPath, &nopLog)
6969
require.NoError(t, err)
@@ -72,7 +72,7 @@ func TestFindOriginCert_Valid(t *testing.T) {
7272

7373
func TestFindOriginCert_Missing(t *testing.T) {
7474
dir := t.TempDir()
75-
certPath := path.Join(dir, originCertFile)
75+
certPath := filepath.Join(dir, originCertFile)
7676
_, err := FindOriginCert(certPath, &nopLog)
7777
require.Error(t, err)
7878
}

logger/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"io"
66
"os"
7-
"path"
87
"path/filepath"
98
"sync"
109
"time"
@@ -249,7 +248,7 @@ func createRollingLogger(config RollingConfig) (io.Writer, error) {
249248
}
250249

251250
rotatingFileInit.writer = &lumberjack.Logger{
252-
Filename: path.Join(config.Dirname, config.Filename),
251+
Filename: filepath.Join(config.Dirname, config.Filename),
253252
MaxBackups: config.maxBackups,
254253
MaxSize: config.maxSize,
255254
MaxAge: config.maxAge,

0 commit comments

Comments
 (0)