Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cli/cmd/root.go at develop · Security-Labz/cli · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
Security-Labz
cli
Repository navigation
Code
Pull requests
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
cli
/
cmd
/
root.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
270 lines (250 loc) · 7.65 KB
develop
Breadcrumbs
cli
/
cmd
/
root.go
Copy path
Top
File metadata and controls
Code
Blame
270 lines (250 loc) · 7.65 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
package
cmd
import
(
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"time"
"github.com/getsentry/sentry-go"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/debug"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"golang.org/x/mod/semver"
)
const
(
groupQuickStart
=
"quick-start"
groupLocalDev
=
"local-dev"
groupManagementAPI
=
"management-api"
)
func
IsManagementAPI
(
cmd
*
cobra.
Command
)
bool
{
for
cmd
!=
cmd
.
Root
() {
if
cmd
.
GroupID
==
groupManagementAPI
{
return
true
}
// Find the last assigned group
if
len
(
cmd
.
GroupID
)
>
0
{
break
}
cmd
=
cmd
.
Parent
()
}
return
false
}
func
promptLogin
(
fsys
afero.
Fs
)
error
{
if
_
,
err
:=
utils
.
LoadAccessTokenFS
(
fsys
);
err
==
utils
.
ErrMissingToken
{
utils
.
CmdSuggestion
=
fmt
.
Sprintf
(
"Run %s first."
,
utils
.
Aqua
(
"supabase login"
))
return
errors
.
New
(
"You need to be logged-in in order to use Management API commands."
)
}
else
{
return
err
}
}
var
experimental
=
[]
*
cobra.
Command
{
bansCmd
,
restrictionsCmd
,
vanityCmd
,
sslEnforcementCmd
,
genKeysCmd
,
postgresCmd
,
branchesCmd
,
storageCmd
,
}
func
IsExperimental
(
cmd
*
cobra.
Command
)
bool
{
for
_
,
exp
:=
range
experimental
{
if
cmd
==
exp
||
cmd
.
Parent
()
==
exp
{
return
true
}
}
return
false
}
var
(
sentryOpts
=
sentry.
ClientOptions
{
Dsn
:
utils
.
SentryDsn
,
Release
:
utils
.
Version
,
ServerName
:
"<redacted>"
,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate
:
1.0
,
}
createTicket
bool
rootCmd
=
&
cobra.
Command
{
Use
:
"supabase"
,
Short
:
"Supabase CLI "
+
utils
.
Version
,
Version
:
utils
.
Version
,
PersistentPreRunE
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
)
error
{
if
IsExperimental
(
cmd
)
&&
!
viper
.
GetBool
(
"EXPERIMENTAL"
) {
return
errors
.
New
(
"must set the --experimental flag to run this command"
)
}
cmd
.
SilenceUsage
=
true
// Change workdir
fsys
:=
afero
.
NewOsFs
()
if
err
:=
utils
.
ChangeWorkDir
(
fsys
);
err
!=
nil
{
return
err
}
// Add common flags
ctx
:=
cmd
.
Context
()
if
IsManagementAPI
(
cmd
) {
if
err
:=
promptLogin
(
fsys
);
err
!=
nil
{
return
err
}
ctx
,
_
=
signal
.
NotifyContext
(
ctx
,
os
.
Interrupt
)
if
cmd
.
Flags
().
Lookup
(
"project-ref"
)
!=
nil
{
if
err
:=
flags
.
ParseProjectRef
(
ctx
,
fsys
);
err
!=
nil
{
return
err
}
}
}
if
err
:=
flags
.
ParseDatabaseConfig
(
cmd
.
Flags
(),
fsys
);
err
!=
nil
{
return
err
}
// Prepare context
if
viper
.
GetBool
(
"DEBUG"
) {
http
.
DefaultTransport
=
debug
.
NewTransport
()
fmt
.
Fprintln
(
os
.
Stderr
,
cmd
.
Root
().
Short
)
}
cmd
.
SetContext
(
ctx
)
// Setup sentry last to ignore errors from parsing cli flags
apiHost
,
err
:=
url
.
Parse
(
utils
.
GetSupabaseAPIHost
())
if
err
!=
nil
{
return
err
}
sentryOpts
.
Environment
=
apiHost
.
Host
return
sentry
.
Init
(
sentryOpts
)
},
SilenceErrors
:
true
,
}
)
func
Execute
() {
defer
recoverAndExit
()
if
err
:=
rootCmd
.
Execute
();
err
!=
nil
{
panic
(
err
)
}
// Check upgrade last because --version flag is initialised after execute
version
,
err
:=
checkUpgrade
(
rootCmd
.
Context
(),
afero
.
NewOsFs
())
if
err
!=
nil
{
fmt
.
Fprintln
(
utils
.
GetDebugLogger
(),
err
)
}
if
semver
.
Compare
(
version
,
"v"
+
utils
.
Version
)
>
0
{
fmt
.
Fprintln
(
os
.
Stderr
,
suggestUpgrade
(
version
))
}
if
len
(
utils
.
CmdSuggestion
)
>
0
{
fmt
.
Fprintln
(
os
.
Stderr
,
utils
.
CmdSuggestion
)
}
}
func
checkUpgrade
(
ctx
context.
Context
,
fsys
afero.
Fs
) (
string
,
error
) {
if
shouldFetchRelease
(
fsys
) {
version
,
err
:=
utils
.
GetLatestRelease
(
ctx
)
if
exists
,
_
:=
afero
.
DirExists
(
fsys
,
utils
.
SupabaseDirPath
);
exists
{
// If user is offline, write an empty file to skip subsequent checks
err
=
utils
.
WriteFile
(
utils
.
CliVersionPath
, []
byte
(
version
),
fsys
)
}
return
version
,
err
}
version
,
err
:=
afero
.
ReadFile
(
fsys
,
utils
.
CliVersionPath
)
if
err
!=
nil
{
return
""
,
errors
.
Errorf
(
"failed to read cli version: %w"
,
err
)
}
return
string
(
version
),
nil
}
func
shouldFetchRelease
(
fsys
afero.
Fs
)
bool
{
// Always fetch latest release when using --version flag
if
vf
:=
rootCmd
.
Flag
(
"version"
);
vf
!=
nil
&&
vf
.
Changed
{
return
true
}
if
fi
,
err
:=
fsys
.
Stat
(
utils
.
CliVersionPath
);
err
==
nil
{
expiry
:=
fi
.
ModTime
().
Add
(
time
.
Hour
*
10
)
// Skip if last checked is less than 10 hours ago
return
time
.
Now
().
After
(
expiry
)
}
return
true
}
func
suggestUpgrade
(
version
string
)
string
{
const
guide
=
"https://supabase.com/docs/guides/cli/getting-started#updating-the-supabase-cli"
return
fmt
.
Sprintf
(
`A new version of Supabase CLI is available: %s (currently installed v%s)
We recommend updating regularly for new features and bug fixes: %s`
,
utils
.
Yellow
(
version
),
utils
.
Version
,
utils
.
Bold
(
guide
))
}
func
recoverAndExit
() {
err
:=
recover
()
if
err
==
nil
{
return
}
var
msg
string
switch
err
:=
err
.(
type
) {
case
string
:
msg
=
err
case
error
:
if
!
errors
.
Is
(
err
,
context
.
Canceled
)
&&
len
(
utils
.
CmdSuggestion
)
==
0
&&
!
viper
.
GetBool
(
"DEBUG"
) {
utils
.
CmdSuggestion
=
utils
.
SuggestDebugFlag
}
msg
=
err
.
Error
()
default
:
msg
=
fmt
.
Sprintf
(
"%#v"
,
err
)
}
// Log error to console
fmt
.
Fprintln
(
os
.
Stderr
,
utils
.
Red
(
msg
))
if
len
(
utils
.
CmdSuggestion
)
>
0
{
fmt
.
Fprintln
(
os
.
Stderr
,
utils
.
CmdSuggestion
)
}
// Report error to sentry
if
createTicket
&&
len
(
utils
.
SentryDsn
)
>
0
{
sentry
.
ConfigureScope
(
addSentryScope
)
eventId
:=
sentry
.
CurrentHub
().
Recover
(
err
)
if
eventId
!=
nil
&&
sentry
.
Flush
(
2
*
time
.
Second
) {
fmt
.
Fprintln
(
os
.
Stderr
,
"Sent crash report:"
,
*
eventId
)
fmt
.
Fprintln
(
os
.
Stderr
,
"Quote the crash ID above when filing a bug report: https://github.com/supabase/cli/issues/new/choose"
)
}
}
os
.
Exit
(
1
)
}
func
init
() {
cobra
.
OnInitialize
(
func
() {
viper
.
SetEnvPrefix
(
"SUPABASE"
)
viper
.
SetEnvKeyReplacer
(
strings
.
NewReplacer
(
"-"
,
"_"
))
viper
.
AutomaticEnv
()
})
flags
:=
rootCmd
.
PersistentFlags
()
flags
.
Bool
(
"debug"
,
false
,
"output debug logs to stderr"
)
flags
.
String
(
"workdir"
,
""
,
"path to a Supabase project directory"
)
flags
.
Bool
(
"experimental"
,
false
,
"enable experimental features"
)
flags
.
String
(
"network-id"
,
""
,
"use the specified docker network instead of a generated one"
)
flags
.
VarP
(
&
utils
.
OutputFormat
,
"output"
,
"o"
,
"output format of status variables"
)
flags
.
Var
(
&
utils
.
DNSResolver
,
"dns-resolver"
,
"lookup domain names using the specified resolver"
)
flags
.
BoolVar
(
&
createTicket
,
"create-ticket"
,
false
,
"create a support ticket for any CLI error"
)
cobra
.
CheckErr
(
viper
.
BindPFlags
(
flags
))
rootCmd
.
SetVersionTemplate
(
"{{.Version}}
\n
"
)
rootCmd
.
AddGroup
(
&
cobra.
Group
{
ID
:
groupQuickStart
,
Title
:
"Quick Start:"
})
rootCmd
.
AddGroup
(
&
cobra.
Group
{
ID
:
groupLocalDev
,
Title
:
"Local Development:"
})
rootCmd
.
AddGroup
(
&
cobra.
Group
{
ID
:
groupManagementAPI
,
Title
:
"Management APIs:"
})
}
// instantiate new rootCmd is a bit tricky with cobra, but it can be done later with the following
// approach for example: https://github.com/portworx/pxc/tree/master/cmd
func
GetRootCmd
()
*
cobra.
Command
{
return
rootCmd
}
func
addSentryScope
(
scope
*
sentry.
Scope
) {
serviceImages
:=
utils
.
Config
.
GetServiceImages
()
imageToVersion
:=
make
(
map
[
string
]
interface
{},
len
(
serviceImages
))
for
_
,
image
:=
range
serviceImages
{
parts
:=
strings
.
Split
(
image
,
":"
)
// Bypasses sentry's IP sanitization rule, ie. 15.1.0.147
if
net
.
ParseIP
(
parts
[
1
])
!=
nil
{
imageToVersion
[
parts
[
0
]]
=
"v"
+
parts
[
1
]
}
else
{
imageToVersion
[
parts
[
0
]]
=
parts
[
1
]
}
}
scope
.
SetContext
(
"Services"
,
imageToVersion
)
scope
.
SetContext
(
"Config"
,
map
[
string
]
interface
{}{
"Image Registry"
:
utils
.
GetRegistry
(),
"Project ID"
:
flags
.
ProjectRef
,
})
}
You can’t perform that action at this time.