7
7
"bytes"
8
8
"fmt"
9
9
"io/fs"
10
- "os"
10
+ stdos "os"
11
11
"path"
12
12
"path/filepath"
13
13
"sort"
@@ -25,6 +25,7 @@ import (
25
25
"github.com/terramate-io/terramate/hcl"
26
26
"github.com/terramate-io/terramate/hcl/ast"
27
27
"github.com/terramate-io/terramate/hcl/info"
28
+ "github.com/terramate-io/terramate/os"
28
29
"github.com/terramate-io/terramate/printer"
29
30
"github.com/terramate-io/terramate/project"
30
31
"github.com/zclconf/go-cty/cty"
@@ -72,7 +73,7 @@ type Tree struct {
72
73
root * Root
73
74
stack * Stack
74
75
75
- dir string
76
+ dir os. Path
76
77
}
77
78
78
79
// DirElem represents a node which is represented by a directory.
@@ -89,7 +90,7 @@ type List[T DirElem] []T
89
90
// the config in fromdir and all parent directories until / is reached.
90
91
// If the configuration is found, it returns the whole configuration tree,
91
92
// configpath != "" and found as true.
92
- func TryLoadConfig (fromdir string ) (tree * Root , configpath string , found bool , err error ) {
93
+ func TryLoadConfig (fromdir os. Path ) (tree * Root , configpath os. Path , found bool , err error ) {
93
94
for {
94
95
ok , err := hcl .IsRootConfig (fromdir )
95
96
if err != nil {
@@ -130,7 +131,7 @@ func NewRoot(tree *Tree) *Root {
130
131
}
131
132
132
133
// LoadRoot loads the root configuration tree.
133
- func LoadRoot (rootdir string ) (* Root , error ) {
134
+ func LoadRoot (rootdir os. Path ) (* Root , error ) {
134
135
rootcfg , err := hcl .ParseDir (rootdir , rootdir )
135
136
if err != nil {
136
137
return nil , err
@@ -148,7 +149,7 @@ func LoadRoot(rootdir string) (*Root, error) {
148
149
func (root * Root ) Tree () * Tree { return & root .tree }
149
150
150
151
// HostDir returns the root directory.
151
- func (root * Root ) HostDir () string { return root .tree .RootDir () }
152
+ func (root * Root ) Path () os. Path { return root .tree .Path () }
152
153
153
154
// Lookup a node from the root using a filesystem query path.
154
155
func (root * Root ) Lookup (path project.Path ) (* Tree , bool ) {
@@ -229,20 +230,20 @@ func (root *Root) LoadSubTree(cfgdir project.Path) error {
229
230
parentNode = root .Tree ()
230
231
}
231
232
232
- rootdir := root .HostDir ()
233
+ rootdir := root .Path ()
233
234
234
235
relpath := strings .TrimPrefix (cfgdir .String (), parent .String ())
235
236
relpath = strings .TrimPrefix (relpath , "/" )
236
237
components := strings .Split (relpath , "/" )
237
238
nextComponent := components [0 ]
238
- subtreeDir := filepath .Join (rootdir , parent .String (), nextComponent )
239
+ subtreeDir := rootdir .Join (parent .String (), nextComponent )
239
240
240
241
node , err := loadTree (root .Tree (), subtreeDir , nil )
241
242
if err != nil {
242
243
return errors .E (err , "failed to load config from %s" , subtreeDir )
243
244
}
244
245
245
- if node .HostDir () == rootdir {
246
+ if node .Path () == rootdir {
246
247
// root configuration reloaded
247
248
* root = * NewRoot (node )
248
249
} else {
@@ -269,8 +270,8 @@ func (root *Root) Runtime() project.Runtime {
269
270
270
271
func (root * Root ) initRuntime () {
271
272
rootfs := cty .ObjectVal (map [string ]cty.Value {
272
- "absolute" : cty .StringVal (root .HostDir ()),
273
- "basename" : cty .StringVal (filepath .ToSlash (filepath .Base (root .HostDir ()))),
273
+ "absolute" : cty .StringVal (root .Path (). String ()),
274
+ "basename" : cty .StringVal (filepath .ToSlash (filepath .Base (root .Path (). String ()))),
274
275
})
275
276
rootpath := cty .ObjectVal (map [string ]cty.Value {
276
277
"fs" : rootfs ,
@@ -288,8 +289,8 @@ func (root *Root) initRuntime() {
288
289
}
289
290
}
290
291
291
- // HostDir is the node absolute directory in the host.
292
- func (tree * Tree ) HostDir () string {
292
+ // Path is the node absolute directory in the host.
293
+ func (tree * Tree ) Path () os. Path {
293
294
return tree .dir
294
295
}
295
296
@@ -299,7 +300,7 @@ func (tree *Tree) Dir() project.Path {
299
300
}
300
301
301
302
// RootDir returns the tree root directory..
302
- func (tree * Tree ) RootDir () string {
303
+ func (tree * Tree ) RootDir () os. Path {
303
304
if tree .Parent != nil {
304
305
return tree .Parent .RootDir ()
305
306
}
@@ -422,13 +423,13 @@ func (l List[T]) Len() int { return len(l) }
422
423
func (l List [T ]) Less (i , j int ) bool { return l [i ].Dir ().String () < l [j ].Dir ().String () }
423
424
func (l List [T ]) Swap (i , j int ) { l [i ], l [j ] = l [j ], l [i ] }
424
425
425
- func loadTree (parentTree * Tree , cfgdir string , rootcfg * hcl.Config ) (_ * Tree , err error ) {
426
+ func loadTree (parentTree * Tree , cfgdir os. Path , rootcfg * hcl.Config ) (_ * Tree , err error ) {
426
427
logger := log .With ().
427
428
Str ("action" , "config.loadTree()" ).
428
- Str ("dir" , cfgdir ).
429
+ Stringer ("dir" , cfgdir ).
429
430
Logger ()
430
431
431
- f , err := os .Open (cfgdir )
432
+ f , err := stdos .Open (cfgdir . String () )
432
433
if err != nil {
433
434
return nil , errors .E (err , "failed to open cfg directory" )
434
435
}
@@ -469,7 +470,7 @@ func loadTree(parentTree *Tree, cfgdir string, rootcfg *hcl.Config) (_ *Tree, er
469
470
470
471
tree .Node = cfg
471
472
tree .Parent = parentTree
472
- parentTree .Children [filepath .Base (cfgdir )] = tree
473
+ parentTree .Children [cfgdir .Base ()] = tree
473
474
474
475
parentTree = tree
475
476
}
@@ -485,7 +486,7 @@ func loadTree(parentTree *Tree, cfgdir string, rootcfg *hcl.Config) (_ *Tree, er
485
486
continue
486
487
}
487
488
488
- dir := filepath .Join (cfgdir , fname )
489
+ dir := cfgdir .Join (fname )
489
490
node , err := loadTree (parentTree , dir , rootcfg )
490
491
if err != nil {
491
492
return nil , errors .E (err , "loading from %s" , dir )
@@ -497,7 +498,7 @@ func loadTree(parentTree *Tree, cfgdir string, rootcfg *hcl.Config) (_ *Tree, er
497
498
return parentTree , nil
498
499
}
499
500
500
- func processTmGenFiles (rootTree * Tree , cfg * hcl.Config , cfgdir string , dirEntries []fs.DirEntry ) error {
501
+ func processTmGenFiles (rootTree * Tree , cfg * hcl.Config , cfgdir os. Path , dirEntries []fs.DirEntry ) error {
501
502
const tmgenSuffix = ".tmgen"
502
503
503
504
tmgenEnabled := rootTree .hasExperiment ("tmgen" )
@@ -509,7 +510,7 @@ func processTmGenFiles(rootTree *Tree, cfg *hcl.Config, cfgdir string, dirEntrie
509
510
continue
510
511
}
511
512
512
- absFname := filepath .Join (cfgdir , fname )
513
+ absFname := cfgdir .Join (fname )
513
514
514
515
if ! tmgenEnabled {
515
516
printer .Stderr .Warn (
@@ -520,7 +521,7 @@ func processTmGenFiles(rootTree *Tree, cfg *hcl.Config, cfgdir string, dirEntrie
520
521
continue
521
522
}
522
523
523
- content , err := os .ReadFile (absFname )
524
+ content , err := stdos .ReadFile (absFname . String () )
524
525
if err != nil {
525
526
return errors .E (err , "failed to read .tmgen file" )
526
527
}
@@ -559,10 +560,10 @@ func processTmGenFiles(rootTree *Tree, cfg *hcl.Config, cfgdir string, dirEntrie
559
560
560
561
implicitGenBlock := hcl.GenHCLBlock {
561
562
IsImplicitBlock : true ,
562
- Dir : project .PrjAbsPath (rootTree .HostDir (), cfgdir ),
563
+ Dir : project .PrjAbsPath (rootTree .Path (), cfgdir ),
563
564
Inherit : inheritAttr ,
564
- Range : info .NewRange (rootTree .HostDir (), hhcl.Range {
565
- Filename : absFname ,
565
+ Range : info .NewRange (rootTree .Path (), hhcl.Range {
566
+ Filename : absFname . String () ,
566
567
Start : hhcl .InitialPos ,
567
568
End : hhcl.Pos {
568
569
Line : nLines ,
@@ -596,13 +597,13 @@ func (tree *Tree) NonEmptyGlobalsParent() *Tree {
596
597
}
597
598
598
599
// IsStack returns true if the given directory is a stack, false otherwise.
599
- func IsStack (root * Root , dir string ) bool {
600
- node , ok := root .Lookup (project .PrjAbsPath (root .HostDir (), dir ))
600
+ func IsStack (root * Root , dir os. Path ) bool {
601
+ node , ok := root .Lookup (project .PrjAbsPath (root .Path (), dir ))
601
602
return ok && node .IsStack ()
602
603
}
603
604
604
605
// NewTree creates a new tree node.
605
- func NewTree (cfgdir string ) * Tree {
606
+ func NewTree (cfgdir os. Path ) * Tree {
606
607
return & Tree {
607
608
dir : cfgdir ,
608
609
Children : make (map [string ]* Tree ),
@@ -677,8 +678,8 @@ func Skip(name string) bool {
677
678
return name [0 ] == '.'
678
679
}
679
680
680
- func parentDir (dir string ) (string , bool ) {
681
- parent := filepath .Dir (dir )
681
+ func parentDir (dir os. Path ) (os. Path , bool ) {
682
+ parent := dir .Dir ()
682
683
return parent , parent != dir
683
684
}
684
685
0 commit comments