Skip to content

Commit 6cb5699

Browse files
committed
fix: fail to get base dir when config is a directory
Signed-off-by: Bird <[email protected]>
1 parent d864684 commit 6cb5699

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

internal/pkg/configmanager/configmanager.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package configmanager
22

33
import (
44
"fmt"
5-
"path/filepath"
65
"strings"
76

87
"github.com/devstream-io/devstream/pkg/util/file"
8+
"github.com/devstream-io/devstream/pkg/util/log"
99
)
1010

1111
// Manager is used to load the config file from the ConfigFilePath and finally get the Config object.
@@ -96,7 +96,12 @@ func (m *Manager) getConfigFromFileWithGlobalVars() (*Config, error) {
9696
if err != nil {
9797
return nil, fmt.Errorf("failed to get coreConfig from config file. Error: %w", err)
9898
}
99-
coreConfig.State.BaseDir = filepath.Dir(m.ConfigFilePath)
99+
100+
coreConfig.State.BaseDir, err = file.GetFileAbsDirPathOrDirItself(m.ConfigFilePath)
101+
if err != nil {
102+
return nil, fmt.Errorf("failed to get base dir of config. Error: %w", err)
103+
}
104+
log.Debugf("baseDir of config and state is %s", coreConfig.State.BaseDir)
100105

101106
return &Config{
102107
Config: *coreConfig,

pkg/util/file/file.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ func GetFileAbsDirPath(fileLoc string) (string, error) {
7878
}
7979
return filepath.Dir(fileAbs), nil
8080
}
81+
82+
// GetFileAbsDirPathOrDirItself will return abs dir path,
83+
// for file: return its parent directory
84+
// for directory: return dir itself
85+
func GetFileAbsDirPathOrDirItself(fileLoc string) (string, error) {
86+
fileAbs, err := filepath.Abs(fileLoc)
87+
if err != nil {
88+
return "", fmt.Errorf("%s not exists", fileLoc)
89+
}
90+
file, err := os.Stat(fileAbs)
91+
if err != nil {
92+
return "", fmt.Errorf("%s not exists", fileLoc)
93+
}
94+
// if it is a directory, return itself
95+
if file.IsDir() {
96+
return fileAbs, nil
97+
}
98+
// if it is a file, return its parent directory
99+
return filepath.Dir(fileAbs), nil
100+
}

pkg/util/file/file_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,30 @@ var _ = Describe("GetFileAbsDirPath func", func() {
8383
})
8484
})
8585
})
86+
87+
var _ = Describe("GetFileAbsDirPathOrDirItself func", func() {
88+
var baseDir, fileName string
89+
When("param if file", func() {
90+
BeforeEach(func() {
91+
baseDir = GinkgoT().TempDir()
92+
testFile, err := os.CreateTemp(baseDir, "test")
93+
Expect(err).Error().ShouldNot(HaveOccurred())
94+
fileName = filepath.Base(testFile.Name())
95+
})
96+
It("should return parent directory of file", func() {
97+
path, err := file.GetFileAbsDirPathOrDirItself(filepath.Join(baseDir, fileName))
98+
Expect(err).Error().ShouldNot(HaveOccurred())
99+
Expect(path).Should(Equal(baseDir))
100+
})
101+
})
102+
When("param is dir", func() {
103+
BeforeEach(func() {
104+
baseDir = GinkgoT().TempDir()
105+
})
106+
It("should return dir itself", func() {
107+
path, err := file.GetFileAbsDirPathOrDirItself(baseDir)
108+
Expect(err).Error().ShouldNot(HaveOccurred())
109+
Expect(path).Should(Equal(baseDir))
110+
})
111+
})
112+
})

0 commit comments

Comments
 (0)