77
88 "github.com/usetero/cli/internal/app/onboarding/msgs"
99 "github.com/usetero/cli/internal/core/bootstrap"
10+ "github.com/usetero/cli/internal/domain"
1011)
1112
1213func (m * Model ) toCoreState () bootstrap.State {
@@ -29,6 +30,15 @@ func (m *Model) applyCoreState(state bootstrap.State) {
2930 m .state .ddSite = state .DDSite
3031 m .state .ddAPIKey = state .DDAPIKey
3132 m .state .ddAccount = state .DDAccount
33+ m .syncServicesToState ()
34+ }
35+
36+ func (m * Model ) syncServicesToState () {
37+ accountID := domain .AccountID ("" )
38+ if m .state .account != nil {
39+ accountID = m .state .account .ID
40+ }
41+ m .services = m .services .WithAccountID (accountID )
3242}
3343
3444func (m * Model ) handlePreflightResolved (msg msgs.PreflightResolved ) TransitionOutcome {
@@ -56,14 +66,8 @@ func (m *Model) handlePreflightResolved(msg msgs.PreflightResolved) TransitionOu
5666 Account : msg .State .Account ,
5767 },
5868 )
59- nextGate := Gate ( next )
69+ nextGate := next
6070 m .applyCoreState (nextState )
61- if m .state .org != nil && m .state .account == nil {
62- m .services = m .services .WithAccountID ("" )
63- }
64- if m .state .account != nil {
65- m .services = m .services .WithAccountID (m .state .account .ID )
66- }
6771 m .scope .Info ("preflight decision" ,
6872 slog .String ("outcome" , string (msg .State .Outcome )),
6973 slog .String ("next_gate" , nextGate .String ()))
@@ -74,129 +78,137 @@ func (m *Model) handleAuthenticated(msg msgs.Authenticated) TransitionOutcome {
7478 m .scope .Info ("user authenticated" , "user_id" , msg .User .ID )
7579 nextState , next := bootstrap .ApplyAuthenticated (m .toCoreState (), msg .User )
7680 m .applyCoreState (nextState )
77- return advance (Gate ( next ) )
81+ return advance (next )
7882}
7983
8084func (m * Model ) handleRoleSelected (msg msgs.RoleSelected ) TransitionOutcome {
8185 m .scope .Info ("role selected" , slog .String ("role" , msg .Role ))
8286 nextState , next := bootstrap .ApplyRoleSelected (m .toCoreState (), msg .Role )
8387 m .applyCoreState (nextState )
84- return advance (Gate ( next ) )
88+ return advance (next )
8589}
8690
8791func (m * Model ) handleOrgSelected (msg msgs.OrgSelected ) TransitionOutcome {
8892 m .scope .Info ("organization selected" , slog .String ("org_id" , msg .Org .ID .String ()))
8993 nextState , next := bootstrap .ApplyOrgSelected (m .toCoreState (), msg .Org )
9094 m .applyCoreState (nextState )
91- m .services = m .services .WithAccountID ("" )
92- return advance (Gate (next ))
95+ return advance (next )
9396}
9497
9598func (m * Model ) handleNoOrgs () TransitionOutcome {
9699 m .scope .Debug ("no organizations found" )
97100 nextState , next := bootstrap .ApplyNoOrgs (m .toCoreState ())
98101 m .applyCoreState (nextState )
99- return advance (Gate ( next ) )
102+ return advance (next )
100103}
101104
102105func (m * Model ) handleOrgCreated (msg msgs.OrgCreated ) TransitionOutcome {
103106 m .scope .Info ("organization created" , slog .String ("org_id" , msg .Org .ID .String ()))
104107 nextState , next := bootstrap .ApplyOrgCreated (m .toCoreState (), msg .Org )
105108 m .applyCoreState (nextState )
106- m .services = m .services .WithAccountID ("" )
107- return advance (Gate (next ))
109+ return advance (next )
108110}
109111
110112func (m * Model ) handleAccountSelected (msg msgs.AccountSelected ) TransitionOutcome {
111113 m .scope .Info ("account selected" , slog .String ("account_id" , msg .Account .ID .String ()))
112114 nextState , next := bootstrap .ApplyAccountSelected (m .toCoreState (), msg .Org , msg .Account )
113115 m .applyCoreState (nextState )
114- return advance (Gate ( next ) )
116+ return advance (next )
115117}
116118
117119func (m * Model ) handleNoAccounts (msg msgs.NoAccounts ) TransitionOutcome {
118120 m .scope .Debug ("no accounts found" )
119121 nextState , next := bootstrap .ApplyNoAccounts (m .toCoreState (), msg .Org )
120122 m .applyCoreState (nextState )
121- return advance (Gate ( next ) )
123+ return advance (next )
122124}
123125
124126func (m * Model ) handleAccountCreated (msg msgs.AccountCreated ) TransitionOutcome {
125127 m .scope .Info ("account created" , slog .String ("account_id" , msg .Account .ID .String ()))
126128 nextState , next := bootstrap .ApplyAccountCreated (m .toCoreState (), msg .Org , msg .Account )
127129 m .applyCoreState (nextState )
128- return advance (Gate ( next ) )
130+ return advance (next )
129131}
130132
131133func (m * Model ) handleRuntimeReady (msg msgs.RuntimeReady ) TransitionOutcome {
132134 m .scope .Info ("runtime initialized" , slog .String ("account_id" , msg .Account .ID .String ()))
133135 nextState , next := bootstrap .ApplyRuntimeReady (m .toCoreState (), msg .Org , msg .Account )
134136 m .applyCoreState (nextState )
135- m .services = m .services .WithAccountID (msg .Account .ID )
136- return advance (Gate (next ))
137+ return advance (next )
137138}
138139
139140func (m * Model ) handleDatadogReady () TransitionOutcome {
140141 m .scope .Debug ("datadog ready" )
141142 nextState , next := bootstrap .ApplyDatadogReady (m .toCoreState ())
142143 m .applyCoreState (nextState )
143- return advance (Gate ( next ) )
144+ return advance (next )
144145}
145146
146147func (m * Model ) handleDatadogNeeded () TransitionOutcome {
147148 m .scope .Debug ("datadog setup needed" )
148149 nextState , next := bootstrap .ApplyDatadogNeeded (m .toCoreState ())
149150 m .applyCoreState (nextState )
150- return advance (Gate ( next ) )
151+ return advance (next )
151152}
152153
153154func (m * Model ) handleDatadogRegionSelected (msg msgs.DatadogRegionSelected ) TransitionOutcome {
154155 m .scope .Info ("datadog region selected" , slog .String ("site" , string (msg .Site )))
155156 nextState , next := bootstrap .ApplyDatadogRegionSelected (m .toCoreState (), msg .Site )
156157 m .applyCoreState (nextState )
157- return advance (Gate ( next ) )
158+ return advance (next )
158159}
159160
160161func (m * Model ) handleDatadogAPIKeyEntered (msg msgs.DatadogAPIKeyEntered ) TransitionOutcome {
161162 m .scope .Debug ("datadog api key validated" )
162163 nextState , next := bootstrap .ApplyDatadogAPIKeyEntered (m .toCoreState (), msg .APIKey )
163164 m .applyCoreState (nextState )
164- return advance (Gate ( next ) )
165+ return advance (next )
165166}
166167
167168func (m * Model ) handleDatadogAccountCreated (msg msgs.DatadogAccountCreated ) TransitionOutcome {
168169 m .scope .Info ("datadog account created" , slog .String ("datadog_account_id" , msg .DatadogAccountID .String ()))
169170 nextState , next := bootstrap .ApplyDatadogAccountCreated (m .toCoreState (), msg .DatadogAccountID )
170171 m .applyCoreState (nextState )
171- return advance (Gate ( next ) )
172+ return advance (next )
172173}
173174
174175func (m * Model ) handleDatadogDiscoveryComplete () TransitionOutcome {
175176 m .scope .Info ("datadog discovery complete" )
176177 nextState , next := bootstrap .ApplyDatadogDiscoveryComplete (m .toCoreState ())
177178 m .applyCoreState (nextState )
178- return advance (Gate ( next ) )
179+ return advance (next )
179180}
180181
181182func (m * Model ) handleWorkspaceSelected (msg msgs.WorkspaceSelected ) TransitionOutcome {
182183 m .scope .Info ("workspace selected" , slog .String ("workspace_id" , string (msg .Workspace .ID )))
183184 nextState , next := bootstrap .ApplyWorkspaceSelected (m .toCoreState (), msg .Workspace )
184185 m .applyCoreState (nextState )
185- return advance (Gate ( next ) )
186+ return advance (next )
186187}
187188
188189func (m * Model ) handleSyncComplete () TransitionOutcome {
190+ completion , ok := bootstrap .CompleteOnboarding (m .toCoreState ())
191+ if ! ok {
192+ m .scope .Error ("sync completed without required onboarding state" ,
193+ slog .Bool ("has_user" , m .state .user != nil ),
194+ slog .Bool ("has_org" , m .state .org != nil ),
195+ slog .Bool ("has_account" , m .state .account != nil ),
196+ slog .Bool ("has_workspace" , m .state .workspace != nil ),
197+ )
198+ return noop ()
199+ }
200+
189201 m .scope .Info ("onboarding complete" ,
190- slog .String ("org_id" , m . state . org .ID .String ()),
191- slog .String ("account_id" , m . state . account .ID .String ()),
192- slog .String ("workspace_id" , string (m . state . workspace .ID )),
202+ slog .String ("org_id" , completion . Org .ID .String ()),
203+ slog .String ("account_id" , completion . Account .ID .String ()),
204+ slog .String ("workspace_id" , string (completion . Workspace .ID )),
193205 )
194206 return advanceWith ("" , func () tea.Msg {
195207 return msgs.OnboardingComplete {
196- User : m . state . user ,
197- Org : * m . state . org ,
198- Account : * m . state . account ,
199- Workspace : * m . state . workspace ,
208+ User : completion . User ,
209+ Org : completion . Org ,
210+ Account : completion . Account ,
211+ Workspace : completion . Workspace ,
200212 }
201213 })
202214}
0 commit comments