7
7
"os"
8
8
"slices"
9
9
"strings"
10
- "sync"
11
10
12
11
"github.com/DefangLabs/defang/src/pkg"
13
12
"github.com/DefangLabs/defang/src/pkg/cli/client"
@@ -56,7 +55,7 @@ type ByocBaseClient struct {
56
55
ShouldDelegateSubdomain bool
57
56
TenantID string
58
57
59
- loadProjOnce func () ( * compose.Project , error )
58
+ project * compose.Project
60
59
bootstrapLister BootstrapLister
61
60
}
62
61
@@ -82,15 +81,6 @@ func NewByocBaseClient(ctx context.Context, grpcClient client.GrpcClient, tenant
82
81
},
83
82
bootstrapLister : bl ,
84
83
}
85
- b .loadProjOnce = sync .OnceValues (func () (* compose.Project , error ) {
86
- proj , err := b .GrpcClient .Loader .LoadCompose (ctx )
87
- if err != nil {
88
- return nil , err
89
- }
90
- b .PrivateDomain = DnsSafeLabel (proj .Name ) + ".internal"
91
- b .ProjectName = proj .Name
92
- return proj , nil
93
- })
94
84
return b
95
85
}
96
86
@@ -104,20 +94,42 @@ func (b *ByocBaseClient) GetVersions(context.Context) (*defangv1.Version, error)
104
94
}
105
95
106
96
func (b * ByocBaseClient ) LoadProject (ctx context.Context ) (* compose.Project , error ) {
107
- return b .loadProjOnce ()
97
+ if b .project != nil {
98
+ return b .project , nil
99
+ }
100
+ project , err := b .Loader .LoadProject (ctx )
101
+ if err != nil {
102
+ return nil , err
103
+ }
104
+
105
+ b .project = project
106
+ b .setProjectName (project .Name )
107
+
108
+ return project , nil
108
109
}
109
110
110
111
func (b * ByocBaseClient ) LoadProjectName (ctx context.Context ) (string , error ) {
111
-
112
- proj , err := b .loadProjOnce ()
113
- if err == nil {
114
- b .ProjectName = proj .Name
115
- return proj .Name , nil
112
+ if b .ProjectName != "" {
113
+ return b .ProjectName , nil
116
114
}
117
- if ! errors .Is (err , types .ErrComposeFileNotFound ) {
115
+ projectName , err := b .Loader .LoadProjectName (ctx ) // Load the project to get the name
116
+ if err != nil {
117
+ if errors .Is (err , types .ErrComposeFileNotFound ) {
118
+ return b .loadProjectNameFromRemote (ctx )
119
+ }
120
+
118
121
return "" , err
119
122
}
120
123
124
+ b .setProjectName (projectName )
125
+ return projectName , nil
126
+ }
127
+
128
+ func (b * ByocBaseClient ) ServiceDNS (name string ) string {
129
+ return DnsSafeLabel (name ) // TODO: consider merging this with getPrivateFqdn
130
+ }
131
+
132
+ func (b * ByocBaseClient ) loadProjectNameFromRemote (ctx context.Context ) (string , error ) {
121
133
// Get the list of projects from remote
122
134
projectNames , err := b .bootstrapLister .BootstrapList (ctx )
123
135
if err != nil {
@@ -132,7 +144,7 @@ func (b *ByocBaseClient) LoadProjectName(ctx context.Context) (string, error) {
132
144
}
133
145
if len (projectNames ) == 1 {
134
146
term .Debug ("Using default project: " , projectNames [0 ])
135
- b .ProjectName = projectNames [0 ]
147
+ b .setProjectName ( projectNames [0 ])
136
148
return projectNames [0 ], nil
137
149
}
138
150
@@ -141,14 +153,15 @@ func (b *ByocBaseClient) LoadProjectName(ctx context.Context) (string, error) {
141
153
if ! slices .Contains (projectNames , projectName ) {
142
154
return "" , fmt .Errorf ("project %q specified by COMPOSE_PROJECT_NAME not found" , projectName )
143
155
}
144
- term .Debug ("Using project from COMPOSE_PROJECT_NAME environment variable:" , projectNames [ 0 ] )
145
- b .ProjectName = projectName
156
+ term .Debug ("Using project from COMPOSE_PROJECT_NAME environment variable:" , projectName )
157
+ b .setProjectName ( projectName )
146
158
return projectName , nil
147
159
}
148
160
149
161
return "" , errors .New ("multiple projects found; please go to the correct project directory where the compose file is or set COMPOSE_PROJECT_NAME" )
150
162
}
151
163
152
- func (b * ByocBaseClient ) ServiceDNS (name string ) string {
153
- return DnsSafeLabel (name ) // TODO: consider merging this with getPrivateFqdn
164
+ func (b * ByocBaseClient ) setProjectName (projectName string ) {
165
+ b .ProjectName = projectName
166
+ b .PrivateDomain = DnsSafeLabel (b .ProjectName ) + ".internal"
154
167
}
0 commit comments