@@ -5,6 +5,7 @@ package project
5
5
6
6
import (
7
7
"context"
8
+ "errors"
8
9
"fmt"
9
10
"io/fs"
10
11
"maps"
@@ -16,7 +17,6 @@ import (
16
17
"github.com./azure/azure-dev/cli/azd/internal/scaffold"
17
18
"github.com./azure/azure-dev/cli/azd/pkg/environment"
18
19
"github.com./azure/azure-dev/cli/azd/pkg/infra"
19
- "github.com./azure/azure-dev/cli/azd/pkg/infra/provisioning"
20
20
"github.com./azure/azure-dev/cli/azd/pkg/osutil"
21
21
"github.com./psanford/memfs"
22
22
)
@@ -41,18 +41,10 @@ func infraFs(_ context.Context, prjConfig *ProjectConfig) (fs.FS, error) {
41
41
return files , nil
42
42
}
43
43
44
- // Returns the infrastructure configuration that points to a temporary, generated `infra` directory on the filesystem.
45
- func tempInfra (
46
- ctx context.Context ,
47
- prjConfig * ProjectConfig ) (* Infra , error ) {
48
- tmpDir , err := os .MkdirTemp ("" , "azd-infra" )
49
- if err != nil {
50
- return nil , fmt .Errorf ("creating temporary directory: %w" , err )
51
- }
52
-
44
+ func infraFsToDir (ctx context.Context , prjConfig * ProjectConfig , dir string ) error {
53
45
files , err := infraFs (ctx , prjConfig )
54
46
if err != nil {
55
- return nil , err
47
+ return err
56
48
}
57
49
58
50
err = fs .WalkDir (files , "." , func (path string , d fs.DirEntry , err error ) error {
@@ -64,7 +56,7 @@ func tempInfra(
64
56
return nil
65
57
}
66
58
67
- target := filepath .Join (tmpDir , path )
59
+ target := filepath .Join (dir , path )
68
60
if err := os .MkdirAll (filepath .Dir (target ), osutil .PermissionDirectoryOwnerOnly ); err != nil {
69
61
return err
70
62
}
@@ -77,18 +69,10 @@ func tempInfra(
77
69
return os .WriteFile (target , contents , osutil .PermissionFile )
78
70
})
79
71
if err != nil {
80
- return nil , fmt .Errorf ("writing infrastructure: %w" , err )
72
+ return fmt .Errorf ("writing infrastructure: %w" , err )
81
73
}
82
74
83
- return & Infra {
84
- Options : provisioning.Options {
85
- Provider : provisioning .Bicep ,
86
- Path : tmpDir ,
87
- Module : DefaultModule ,
88
- },
89
- cleanupDir : tmpDir ,
90
- IsCompose : true ,
91
- }, nil
75
+ return nil
92
76
}
93
77
94
78
// Generates the filesystem of all infrastructure files to be placed, rooted at the project directory.
@@ -99,13 +83,32 @@ func infraFsForProject(ctx context.Context, prjConfig *ProjectConfig) (fs.FS, er
99
83
return nil , err
100
84
}
101
85
102
- infraPathPrefix := DefaultPath
86
+ infraPrefix := DefaultPath
103
87
if prjConfig .Infra .Path != "" {
104
- infraPathPrefix = prjConfig .Infra .Path
88
+ infraPrefix = prjConfig .Infra .Path
89
+ }
90
+
91
+ infraRoot := infraPrefix
92
+ if ! filepath .IsAbs (infraPrefix ) {
93
+ infraRoot = filepath .Join (prjConfig .Path , infraPrefix )
94
+ }
95
+
96
+ infraDir , err := os .Stat (infraRoot )
97
+ if ! errors .Is (err , os .ErrNotExist ) && err != nil {
98
+ return nil , fmt .Errorf ("error reading infra directory: %w" , err )
99
+ }
100
+
101
+ fi , err := os .Stat (filepath .Join (infraRoot , ".azd" ))
102
+ if ! errors .Is (err , os .ErrNotExist ) && err != nil {
103
+ return nil , fmt .Errorf ("error reading .azd file in infra: %w" , err )
104
+ }
105
+
106
+ if infraDir != nil && fi == nil { // if the infra directory is not managed by azd, generate it to infra/azd
107
+ infraPrefix = filepath .Join (infraPrefix , "azd" )
105
108
}
106
109
107
- // root the generated content at the project directory
108
110
generatedFS := memfs .New ()
111
+ // root the generated content at the project directory
109
112
err = fs .WalkDir (infraFS , "." , func (path string , d fs.DirEntry , err error ) error {
110
113
if err != nil {
111
114
return err
@@ -115,7 +118,7 @@ func infraFsForProject(ctx context.Context, prjConfig *ProjectConfig) (fs.FS, er
115
118
return nil
116
119
}
117
120
118
- err = generatedFS .MkdirAll (filepath .Join (infraPathPrefix , filepath .Dir (path )), osutil .PermissionDirectoryOwnerOnly )
121
+ err = generatedFS .MkdirAll (filepath .Join (infraPrefix , filepath .Dir (path )), osutil .PermissionDirectoryOwnerOnly )
119
122
if err != nil {
120
123
return err
121
124
}
@@ -125,10 +128,18 @@ func infraFsForProject(ctx context.Context, prjConfig *ProjectConfig) (fs.FS, er
125
128
return err
126
129
}
127
130
128
- return generatedFS .WriteFile (filepath .Join (infraPathPrefix , path ), contents , d .Type ().Perm ())
131
+ return generatedFS .WriteFile (filepath .Join (infraPrefix , path ), contents , d .Type ().Perm ())
129
132
})
130
133
if err != nil {
131
- return nil , err
134
+ return nil , fmt .Errorf ("generating: %w" , err )
135
+ }
136
+
137
+ if fi == nil {
138
+ // create a sentinel file to indicate that the infra directory is managed by azd
139
+ err = generatedFS .WriteFile (filepath .Join (infraPrefix , ".azd" ), []byte {}, osutil .PermissionFileOwnerOnly )
140
+ if err != nil {
141
+ return nil , fmt .Errorf ("writing sentinel: %w" , err )
142
+ }
132
143
}
133
144
134
145
return generatedFS , nil
0 commit comments