Skip to content

Commit aeacc60

Browse files
Fix the code used to get the working directory (#484)
Use os.Executable to get the directory of the exe
1 parent 5f9f9c7 commit aeacc60

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

cns/configuration/configuration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99

1010
"github.com/Azure/azure-container-networking/cns/logger"
11+
"github.com/Azure/azure-container-networking/common"
1112
)
1213

1314
const (
@@ -46,17 +47,16 @@ func ReadConfig() (CNSConfig, error) {
4647
// Check if env set for config path otherwise use default path
4748
configpath, found := os.LookupEnv("CNS_CONFIGURATION_PATH")
4849
if !found {
49-
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
50+
dir, err := common.GetExecutableDirectory()
5051
if err != nil {
5152
logger.Errorf("[Configuration] Failed to find exe dir:%v", err)
5253
return cnsConfig, err
5354
}
5455

5556
configpath = filepath.Join(dir, defaultConfigName)
56-
//dir + string(os.PathSeparator) + defaultConfigName
5757
}
5858

59-
logger.Printf("Config path:%s", configpath)
59+
logger.Printf("[Configuration] Config path:%s", configpath)
6060

6161
content, err := ioutil.ReadFile(configpath)
6262
if err != nil {

cns/networkcontainers/networkcontainers_windows.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"net"
1010
"os"
1111
"os/exec"
12-
"path"
1312
"path/filepath"
1413
"strconv"
1514
"strings"
1615
"sync"
1716

1817
"github.com/Azure/azure-container-networking/cns"
1918
"github.com/Azure/azure-container-networking/cns/logger"
19+
"github.com/Azure/azure-container-networking/common"
2020
"github.com/Azure/azure-container-networking/log"
2121
"github.com/containernetworking/cni/libcni"
2222
)
@@ -250,12 +250,13 @@ func getAzureNetworkContainerBinaryPath() (string, error) {
250250
err error
251251
)
252252

253-
if workingDir, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
253+
workingDir, err = common.GetExecutableDirectory()
254+
if err != nil {
254255
return binaryPath,
255256
fmt.Errorf("[Azure CNS] Unable to find working directory. Error: %v. Cannot continue", err)
256257
}
257258

258-
binaryPath = path.Join(workingDir, binaryAzureNetworkContainer)
259+
binaryPath = filepath.Join(workingDir, binaryAzureNetworkContainer)
259260

260261
if _, err = os.Stat(binaryPath); err != nil {
261262
return binaryPath,

common/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net"
1515
"net/http"
1616
"os"
17+
"path/filepath"
1718
"strings"
1819
"time"
1920

@@ -323,3 +324,26 @@ func GetAzureCloud(url string) (string, error) {
323324

324325
return strings.TrimSpace(string(bodyBytes)), nil
325326
}
327+
328+
func GetExecutableDirectory() (string, error) {
329+
var (
330+
dir string
331+
ex string
332+
err error
333+
)
334+
ex, err = os.Executable()
335+
if err == nil {
336+
dir = filepath.Dir(ex)
337+
} else {
338+
var exReal string
339+
// If a symlink was used to start the process, depending on the operating system,
340+
// the result might be the symlink or the path it pointed to.
341+
// filepath.EvalSymlinks returns stable results
342+
exReal, err = filepath.EvalSymlinks(ex)
343+
if err == nil {
344+
dir = filepath.Dir(exReal)
345+
}
346+
}
347+
348+
return dir, err
349+
}

0 commit comments

Comments
 (0)