Skip to content

Commit 3a7e07b

Browse files
committed
feat: create state file relative to config file
Signed-off-by: Bird <[email protected]>
1 parent 73025d4 commit 3a7e07b

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

internal/pkg/backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func GetBackend(state configmanager.State) (Backend, error) {
3333

3434
switch typeName {
3535
case LocalBackend:
36-
return local.NewLocal(state.Options.StateFile)
36+
return local.NewLocal(state.BaseDir, state.Options.StateFile)
3737
case S3Backend:
3838
return s3.NewS3Backend(state.Options.Bucket, state.Options.Region, state.Options.Key)
3939
case K8sBackend, K8sBackendAlias:

internal/pkg/backend/local/local.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ type Local struct {
1818
}
1919

2020
// NewLocal will use DefaultStateFile as statemanager file if filename is not given.
21-
func NewLocal(filename string) (*Local, error) {
21+
func NewLocal(baseDir, filename string) (*Local, error) {
2222
var lFile = filename
2323
if filename == "" {
2424
log.Debugf("The stateFile has not been set, default value %s will be used.", DefaultStateFile)
2525
lFile = DefaultStateFile
2626
}
2727

28+
// if state file is not absolute path, use baseDir as prefix.
29+
if !filepath.IsAbs(lFile) {
30+
lFile = filepath.Join(baseDir, lFile)
31+
}
32+
2833
log.Infof("Using local backend. State file: %s.", filename)
2934

3035
if _, err := os.Stat(lFile); errors.Is(err, os.ErrNotExist) {

internal/pkg/backend/local/local_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@ import (
1111
)
1212

1313
var _ = Describe("NewLocal func", func() {
14-
var tFile, tFileLoc, tempDir string
14+
var tFile, tempDir string
1515

1616
BeforeEach(func() {
1717
tempDir = GinkgoT().TempDir()
1818
tFile = "test_state_file"
19-
tFileLoc = filepath.Join(tempDir, tFile)
2019
})
2120

2221
When("input filename", func() {
2322
It("should create state file", func() {
24-
_, err := local.NewLocal(tFileLoc)
23+
_, err := local.NewLocal(tempDir, tFile)
2524
Expect(err).Should(Succeed())
26-
_, err = os.Stat(tFileLoc)
25+
_, err = os.Stat(filepath.Join(tempDir, tFile))
2726
Expect(err).Error().ShouldNot(HaveOccurred())
2827
})
2928
})
@@ -39,8 +38,7 @@ var _ = Describe("Local struct", func() {
3938
BeforeEach(func() {
4039
tempDir = GinkgoT().TempDir()
4140
tFile = "test_state_file"
42-
tFileLoc = filepath.Join(tempDir, tFile)
43-
tLocal, err = local.NewLocal(tFileLoc)
41+
tLocal, err = local.NewLocal(tempDir, tFile)
4442
Expect(err).ShouldNot(HaveOccurred())
4543
})
4644

internal/pkg/configmanager/configmanager.go

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

33
import (
44
"fmt"
5+
"path/filepath"
56
"strings"
67

78
"github.com/devstream-io/devstream/pkg/util/file"
@@ -95,6 +96,7 @@ func (m *Manager) getConfigFromFileWithGlobalVars() (*Config, error) {
9596
if err != nil {
9697
return nil, fmt.Errorf("failed to get coreConfig from config file. Error: %w", err)
9798
}
99+
coreConfig.State.BaseDir = filepath.Dir(m.ConfigFilePath)
98100

99101
return &Config{
100102
Config: *coreConfig,

internal/pkg/configmanager/coreconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type CoreConfig struct {
99
type State struct {
1010
Backend string `yaml:"backend"`
1111
Options StateConfigOptions `yaml:"options"`
12+
BaseDir string `yaml:"-"` // baseDir is the base directory of the config file
1213
}
1314

1415
// StateConfigOptions is the struct for reading the options of the state backend.

0 commit comments

Comments
 (0)