Skip to content

Commit 03ebdec

Browse files
replace loadProjOnce with setProjectName
1 parent e6ab39b commit 03ebdec

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

src/pkg/cli/client/byoc/baseclient.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"slices"
99
"strings"
10-
"sync"
1110

1211
"github.com/DefangLabs/defang/src/pkg"
1312
"github.com/DefangLabs/defang/src/pkg/cli/client"
@@ -56,7 +55,7 @@ type ByocBaseClient struct {
5655
ShouldDelegateSubdomain bool
5756
TenantID string
5857

59-
loadProjOnce func() (*compose.Project, error)
58+
project *compose.Project
6059
bootstrapLister BootstrapLister
6160
}
6261

@@ -82,15 +81,6 @@ func NewByocBaseClient(ctx context.Context, grpcClient client.GrpcClient, tenant
8281
},
8382
bootstrapLister: bl,
8483
}
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-
})
9484
return b
9585
}
9686

@@ -104,20 +94,42 @@ func (b *ByocBaseClient) GetVersions(context.Context) (*defangv1.Version, error)
10494
}
10595

10696
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
108109
}
109110

110111
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
116114
}
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+
118121
return "", err
119122
}
120123

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) {
121133
// Get the list of projects from remote
122134
projectNames, err := b.bootstrapLister.BootstrapList(ctx)
123135
if err != nil {
@@ -132,7 +144,7 @@ func (b *ByocBaseClient) LoadProjectName(ctx context.Context) (string, error) {
132144
}
133145
if len(projectNames) == 1 {
134146
term.Debug("Using default project: ", projectNames[0])
135-
b.ProjectName = projectNames[0]
147+
b.setProjectName(projectNames[0])
136148
return projectNames[0], nil
137149
}
138150

@@ -141,14 +153,15 @@ func (b *ByocBaseClient) LoadProjectName(ctx context.Context) (string, error) {
141153
if !slices.Contains(projectNames, projectName) {
142154
return "", fmt.Errorf("project %q specified by COMPOSE_PROJECT_NAME not found", projectName)
143155
}
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)
146158
return projectName, nil
147159
}
148160

149161
return "", errors.New("multiple projects found; please go to the correct project directory where the compose file is or set COMPOSE_PROJECT_NAME")
150162
}
151163

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"
154167
}

0 commit comments

Comments
 (0)