Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
sb-cli/cmd/branches.go at develop · sai-sy/sb-cli · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
sai-sy
sb-cli
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
sb-cli
/
cmd
/
branches.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
271 lines (255 loc) · 8.24 KB
develop
Breadcrumbs
sb-cli
/
cmd
/
branches.go
Copy path
Top
File metadata and controls
Code
Blame
271 lines (255 loc) · 8.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package
cmd
import
(
"context"
"fmt"
"os"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/supabase/cli/internal/branches/create"
"github.com/supabase/cli/internal/branches/delete"
"github.com/supabase/cli/internal/branches/disable"
"github.com/supabase/cli/internal/branches/get"
"github.com/supabase/cli/internal/branches/list"
"github.com/supabase/cli/internal/branches/pause"
"github.com/supabase/cli/internal/branches/unpause"
"github.com/supabase/cli/internal/branches/update"
"github.com/supabase/cli/internal/gen/keys"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/cli/pkg/cast"
)
var
(
branchesCmd
=
&
cobra.
Command
{
GroupID
:
groupManagementAPI
,
Use
:
"branches"
,
Short
:
"Manage Supabase preview branches"
,
}
branchRegion
=
utils.
EnumFlag
{
Allowed
:
awsRegions
(),
}
persistent
bool
withData
bool
notifyURL
string
branchCreateCmd
=
&
cobra.
Command
{
Use
:
"create [name]"
,
Short
:
"Create a preview branch"
,
Long
:
"Create a preview branch for the linked project."
,
Args
:
cobra
.
MaximumNArgs
(
1
),
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
body
:=
api.
CreateBranchBody
{
IsDefault
:
cast
.
Ptr
(
false
)}
if
len
(
args
)
>
0
{
body
.
BranchName
=
args
[
0
]
}
cmdFlags
:=
cmd
.
Flags
()
if
cmdFlags
.
Changed
(
"region"
) {
body
.
Region
=
&
branchRegion
.
Value
}
if
cmdFlags
.
Changed
(
"size"
) {
body
.
DesiredInstanceSize
=
(
*
api
.
CreateBranchBodyDesiredInstanceSize
)(
&
size
.
Value
)
}
if
cmdFlags
.
Changed
(
"persistent"
) {
body
.
Persistent
=
&
persistent
}
if
cmdFlags
.
Changed
(
"with-data"
) {
body
.
WithData
=
&
withData
}
if
cmdFlags
.
Changed
(
"notify-url"
) {
body
.
NotifyUrl
=
&
notifyURL
}
return
create
.
Run
(
cmd
.
Context
(),
body
,
afero
.
NewOsFs
())
},
}
branchListCmd
=
&
cobra.
Command
{
Use
:
"list"
,
Short
:
"List all preview branches"
,
Long
:
"List all preview branches of the linked project."
,
Args
:
cobra
.
NoArgs
,
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
return
list
.
Run
(
cmd
.
Context
(),
afero
.
NewOsFs
())
},
}
branchId
string
branchGetCmd
=
&
cobra.
Command
{
Use
:
"get [name]"
,
Short
:
"Retrieve details of a preview branch"
,
Long
:
"Retrieve details of the specified preview branch."
,
Args
:
cobra
.
MaximumNArgs
(
1
),
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
ctx
:=
cmd
.
Context
()
fsys
:=
afero
.
NewOsFs
()
if
len
(
args
)
>
0
{
branchId
=
args
[
0
]
}
else
if
err
:=
promptBranchId
(
ctx
,
fsys
);
err
!=
nil
{
return
err
}
return
get
.
Run
(
ctx
,
branchId
,
fsys
)
},
}
branchStatus
=
utils.
EnumFlag
{
Allowed
: []
string
{
string
(
api
.
BranchResponseStatusRUNNINGMIGRATIONS
),
string
(
api
.
BranchResponseStatusMIGRATIONSPASSED
),
string
(
api
.
BranchResponseStatusMIGRATIONSFAILED
),
string
(
api
.
BranchResponseStatusFUNCTIONSDEPLOYED
),
string
(
api
.
BranchResponseStatusFUNCTIONSFAILED
),
},
}
branchName
string
gitBranch
string
branchUpdateCmd
=
&
cobra.
Command
{
Use
:
"update [name]"
,
Short
:
"Update a preview branch"
,
Long
:
"Update a preview branch by its name or ID."
,
Args
:
cobra
.
MaximumNArgs
(
1
),
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
cmdFlags
:=
cmd
.
Flags
()
var
body
api.
UpdateBranchBody
if
cmdFlags
.
Changed
(
"name"
) {
body
.
BranchName
=
&
branchName
}
if
cmdFlags
.
Changed
(
"git-branch"
) {
body
.
GitBranch
=
&
gitBranch
}
if
cmdFlags
.
Changed
(
"persistent"
) {
body
.
Persistent
=
&
persistent
}
if
cmdFlags
.
Changed
(
"status"
) {
body
.
Status
=
(
*
api
.
UpdateBranchBodyStatus
)(
&
branchStatus
.
Value
)
}
if
cmdFlags
.
Changed
(
"notify-url"
) {
body
.
NotifyUrl
=
&
notifyURL
}
ctx
:=
cmd
.
Context
()
fsys
:=
afero
.
NewOsFs
()
if
len
(
args
)
>
0
{
branchId
=
args
[
0
]
}
else
if
err
:=
promptBranchId
(
ctx
,
fsys
);
err
!=
nil
{
return
err
}
return
update
.
Run
(
cmd
.
Context
(),
branchId
,
body
,
fsys
)
},
}
branchPauseCmd
=
&
cobra.
Command
{
Use
:
"pause [name]"
,
Short
:
"Pause a preview branch"
,
Args
:
cobra
.
MaximumNArgs
(
1
),
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
ctx
:=
cmd
.
Context
()
fsys
:=
afero
.
NewOsFs
()
if
len
(
args
)
>
0
{
branchId
=
args
[
0
]
}
else
if
err
:=
promptBranchId
(
ctx
,
fsys
);
err
!=
nil
{
return
err
}
return
pause
.
Run
(
ctx
,
branchId
)
},
}
branchUnpauseCmd
=
&
cobra.
Command
{
Use
:
"unpause [name]"
,
Short
:
"Unpause a preview branch"
,
Args
:
cobra
.
MaximumNArgs
(
1
),
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
ctx
:=
cmd
.
Context
()
fsys
:=
afero
.
NewOsFs
()
if
len
(
args
)
>
0
{
branchId
=
args
[
0
]
}
else
if
err
:=
promptBranchId
(
ctx
,
fsys
);
err
!=
nil
{
return
err
}
return
unpause
.
Run
(
ctx
,
branchId
)
},
}
branchDeleteCmd
=
&
cobra.
Command
{
Use
:
"delete [name]"
,
Short
:
"Delete a preview branch"
,
Long
:
"Delete a preview branch by its name or ID."
,
Args
:
cobra
.
MaximumNArgs
(
1
),
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
ctx
:=
cmd
.
Context
()
fsys
:=
afero
.
NewOsFs
()
if
len
(
args
)
>
0
{
branchId
=
args
[
0
]
}
else
if
err
:=
promptBranchId
(
ctx
,
fsys
);
err
!=
nil
{
return
err
}
return
delete
.
Run
(
ctx
,
branchId
,
nil
)
},
}
branchDisableCmd
=
&
cobra.
Command
{
Hidden
:
true
,
Use
:
"disable"
,
Short
:
"Disable preview branching"
,
Long
:
"Disable preview branching for the linked project."
,
RunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
return
disable
.
Run
(
cmd
.
Context
(),
afero
.
NewOsFs
())
},
}
)
func
init
() {
branchFlags
:=
branchesCmd
.
PersistentFlags
()
branchFlags
.
StringVar
(
&
flags
.
ProjectRef
,
"project-ref"
,
""
,
"Project ref of the Supabase project."
)
createFlags
:=
branchCreateCmd
.
Flags
()
createFlags
.
Var
(
&
branchRegion
,
"region"
,
"Select a region to deploy the branch database."
)
createFlags
.
Var
(
&
size
,
"size"
,
"Select a desired instance size for the branch database."
)
createFlags
.
BoolVar
(
&
persistent
,
"persistent"
,
false
,
"Whether to create a persistent branch."
)
createFlags
.
BoolVar
(
&
withData
,
"with-data"
,
false
,
"Whether to clone production data to the branch database."
)
createFlags
.
StringVar
(
&
notifyURL
,
"notify-url"
,
""
,
"URL to notify when branch is active healthy."
)
branchesCmd
.
AddCommand
(
branchCreateCmd
)
branchesCmd
.
AddCommand
(
branchListCmd
)
branchesCmd
.
AddCommand
(
branchGetCmd
)
updateFlags
:=
branchUpdateCmd
.
Flags
()
updateFlags
.
StringVar
(
&
branchName
,
"name"
,
""
,
"Rename the preview branch."
)
updateFlags
.
StringVar
(
&
gitBranch
,
"git-branch"
,
""
,
"Change the associated git branch."
)
updateFlags
.
BoolVar
(
&
persistent
,
"persistent"
,
false
,
"Switch between ephemeral and persistent branch."
)
updateFlags
.
Var
(
&
branchStatus
,
"status"
,
"Override the current branch status."
)
updateFlags
.
StringVar
(
&
notifyURL
,
"notify-url"
,
""
,
"URL to notify when branch is active healthy."
)
branchesCmd
.
AddCommand
(
branchUpdateCmd
)
branchesCmd
.
AddCommand
(
branchDeleteCmd
)
branchesCmd
.
AddCommand
(
branchDisableCmd
)
branchesCmd
.
AddCommand
(
branchPauseCmd
)
branchesCmd
.
AddCommand
(
branchUnpauseCmd
)
rootCmd
.
AddCommand
(
branchesCmd
)
}
func
promptBranchId
(
ctx
context.
Context
,
fsys
afero.
Fs
)
error
{
if
console
:=
utils
.
NewConsole
();
!
console
.
IsTTY
{
// Only read from stdin if the terminal is non-interactive
title
:=
"Enter the name of your branch"
if
branchId
=
keys
.
GetGitBranch
(
fsys
);
len
(
branchId
)
>
0
{
title
+=
fmt
.
Sprintf
(
" (or leave blank to use %s)"
,
utils
.
Aqua
(
branchId
))
}
title
+=
": "
if
name
,
err
:=
console
.
PromptText
(
ctx
,
title
);
err
!=
nil
{
return
err
}
else
if
len
(
name
)
>
0
{
branchId
=
name
}
if
len
(
branchId
)
==
0
{
return
errors
.
New
(
"branch name cannot be empty"
)
}
return
nil
}
branches
,
err
:=
list
.
ListBranch
(
ctx
,
flags
.
ProjectRef
)
if
err
!=
nil
{
return
err
}
else
if
len
(
branches
)
==
0
{
utils
.
CmdSuggestion
=
fmt
.
Sprintf
(
"Create your first branch with: %s"
,
utils
.
Aqua
(
"supabase branches create"
))
return
errors
.
Errorf
(
"branching is disabled"
)
}
// Let user choose from a list of branches
items
:=
make
([]utils.
PromptItem
,
len
(
branches
))
for
i
,
branch
:=
range
branches
{
items
[
i
]
=
utils.
PromptItem
{
Summary
:
branch
.
Name
,
Details
:
branch
.
ProjectRef
,
}
}
title
:=
"Select a branch:"
choice
,
err
:=
utils
.
PromptChoice
(
ctx
,
title
,
items
)
if
err
==
nil
{
branchId
=
choice
.
Details
fmt
.
Fprintln
(
os
.
Stderr
,
"Selected branch ID:"
,
branchId
)
}
return
err
}
You can’t perform that action at this time.