• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 4061311189

01 Feb 2023 05:38AM UTC coverage: 62.009% (-0.8%) from 62.839%
4061311189

Pull #828

github

Han Qiao
chore: support binds in run with stream
Pull Request #828: feat: Allow serving all edge functions using edge runtime

91 of 91 new or added lines in 4 files covered. (100.0%)

3617 of 5833 relevant lines covered (62.01%)

1260.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.69
/cmd/functions.go
1
package cmd
2

3
import (
4
        "bufio"
5
        "errors"
6
        "fmt"
7
        "os"
8
        "os/signal"
9
        "strings"
10

11
        "github.com/spf13/afero"
12
        "github.com/spf13/cobra"
13
        "github.com/supabase/cli/internal/functions/delete"
14
        "github.com/supabase/cli/internal/functions/deploy"
15
        "github.com/supabase/cli/internal/functions/download"
16
        new_ "github.com/supabase/cli/internal/functions/new"
17
        "github.com/supabase/cli/internal/functions/serve"
18
        "github.com/supabase/cli/internal/login"
19
        "github.com/supabase/cli/internal/utils"
20
)
21

22
var (
23
        functionsCmd = &cobra.Command{
24
                GroupID: groupManagementAPI,
25
                Use:     "functions",
26
                Short:   "Manage Supabase Edge functions",
27
        }
28

29
        projectRef string
30

31
        functionsDeleteCmd = &cobra.Command{
32
                Use:   "delete <Function name>",
33
                Short: "Delete a Function from Supabase",
34
                Long:  "Delete a Function from the linked Supabase project. This does NOT remove the Function locally.",
35
                Args:  cobra.ExactArgs(1),
36
                RunE: func(cmd *cobra.Command, args []string) error {
×
37
                        fsys := afero.NewOsFs()
×
38
                        if err := PromptLogin(fsys); err != nil {
×
39
                                return err
×
40
                        }
×
41
                        ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
×
42
                        return delete.Run(ctx, args[0], projectRef, fsys)
×
43
                },
44
        }
45

46
        functionsDownloadCmd = &cobra.Command{
47
                Use:   "download <Function name>",
48
                Short: "Download a Function from Supabase",
49
                Long:  "Download the source code for a Function from the linked Supabase project.",
50
                Args:  cobra.ExactArgs(1),
51
                RunE: func(cmd *cobra.Command, args []string) error {
×
52
                        fsys := afero.NewOsFs()
×
53
                        if err := PromptLogin(fsys); err != nil {
×
54
                                return err
×
55
                        }
×
56
                        ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
×
57
                        return download.Run(ctx, args[0], projectRef, fsys)
×
58
                },
59
        }
60

61
        noVerifyJWT     = new(bool)
62
        useLegacyBundle bool
63
        importMapPath   string
64

65
        functionsDeployCmd = &cobra.Command{
66
                Use:   "deploy <Function name>",
67
                Short: "Deploy a Function to Supabase",
68
                Long:  "Deploy a Function to the linked Supabase project.",
69
                Args:  cobra.ExactArgs(1),
70
                RunE: func(cmd *cobra.Command, args []string) error {
×
71
                        fsys := afero.NewOsFs()
×
72
                        if err := PromptLogin(fsys); err != nil {
×
73
                                return err
×
74
                        }
×
75
                        if err := PromptProjectRef(fsys); err != nil {
×
76
                                return err
×
77
                        }
×
78
                        ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
×
79
                        // Fallback to config if user did not set the flag.
×
80
                        if !cmd.Flags().Changed("no-verify-jwt") {
×
81
                                noVerifyJWT = nil
×
82
                        }
×
83
                        return deploy.Run(ctx, args[0], projectRef, noVerifyJWT, useLegacyBundle, importMapPath, fsys)
×
84
                },
85
        }
86

87
        functionsNewCmd = &cobra.Command{
88
                Use:   "new <Function name>",
89
                Short: "Create a new Function locally",
90
                Args:  cobra.ExactArgs(1),
91
                RunE: func(cmd *cobra.Command, args []string) error {
×
92
                        ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
×
93
                        return new_.Run(ctx, args[0], afero.NewOsFs())
×
94
                },
×
95
        }
96

97
        envFilePath string
98
        serveAll    bool
99

100
        functionsServeCmd = &cobra.Command{
101
                Use:   "serve <Function name>",
102
                Short: "Serve a Function locally",
103
                Args:  cobra.RangeArgs(0, 1),
104
                RunE: func(cmd *cobra.Command, args []string) error {
×
105
                        ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
×
106
                        // Fallback to config if user did not set the flag.
×
107
                        if !cmd.Flags().Changed("no-verify-jwt") {
×
108
                                noVerifyJWT = nil
×
109
                        }
×
110
                        slug := ""
×
111
                        if len(args) > 1 {
×
112
                                slug = args[0]
×
113
                        }
×
114
                        return serve.Run(ctx, slug, envFilePath, noVerifyJWT, importMapPath, serveAll, afero.NewOsFs())
×
115
                },
116
        }
117
)
118

119
func init() {
38✔
120
        functionsDeleteCmd.Flags().StringVar(&projectRef, "project-ref", "", "Project ref of the Supabase project.")
38✔
121
        functionsDeployCmd.Flags().BoolVar(noVerifyJWT, "no-verify-jwt", false, "Disable JWT verification for the Function.")
38✔
122
        functionsDeployCmd.Flags().StringVar(&projectRef, "project-ref", "", "Project ref of the Supabase project.")
38✔
123
        functionsDeployCmd.Flags().BoolVar(&useLegacyBundle, "legacy-bundle", false, "Use legacy bundling mechanism.")
38✔
124
        functionsDeployCmd.Flags().StringVar(&importMapPath, "import-map", "", "Path to import map file.")
38✔
125
        functionsServeCmd.Flags().BoolVar(noVerifyJWT, "no-verify-jwt", false, "Disable JWT verification for the Function.")
38✔
126
        functionsServeCmd.Flags().StringVar(&envFilePath, "env-file", "", "Path to an env file to be populated to the Function environment.")
38✔
127
        functionsServeCmd.Flags().StringVar(&importMapPath, "import-map", "", "Path to import map file.")
38✔
128
        functionsServeCmd.Flags().BoolVar(&serveAll, "all", false, "Serve all functions (caution: Experimental feature)")
38✔
129
        functionsDownloadCmd.Flags().StringVar(&projectRef, "project-ref", "", "Project ref of the Supabase project.")
38✔
130
        functionsCmd.AddCommand(functionsDeleteCmd)
38✔
131
        functionsCmd.AddCommand(functionsDeployCmd)
38✔
132
        functionsCmd.AddCommand(functionsNewCmd)
38✔
133
        functionsCmd.AddCommand(functionsServeCmd)
38✔
134
        functionsCmd.AddCommand(functionsDownloadCmd)
38✔
135
        rootCmd.AddCommand(functionsCmd)
38✔
136
}
38✔
137

138
func PromptLogin(fsys afero.Fs) error {
×
139
        if _, err := utils.LoadAccessTokenFS(fsys); err == nil {
×
140
                return nil
×
141
        } else if strings.HasPrefix(err.Error(), "Access token not provided. Supply an access token by running") {
×
142
                return login.Run(os.Stdin, fsys)
×
143
        } else {
×
144
                return err
×
145
        }
×
146
}
147

148
func PromptProjectRef(fsys afero.Fs) error {
×
149
        if len(projectRef) > 0 {
×
150
                return nil
×
151
        } else if err := utils.AssertIsLinkedFS(fsys); err == nil {
×
152
                return nil
×
153
        } else if strings.HasPrefix(err.Error(), "Cannot find project ref. Have you run") {
×
154
                fmt.Fprintf(os.Stderr, `You can find your project ref from the project's dashboard home page, e.g. %s/project/<project-ref>.
×
155
Enter your project ref: `, utils.GetSupabaseDashboardURL())
×
156

×
157
                scanner := bufio.NewScanner(os.Stdin)
×
158
                if !scanner.Scan() {
×
159
                        return errors.New("Cancelled " + utils.Aqua("supabase functions deploy") + ".")
×
160
                }
×
161

162
                projectRef = strings.TrimSpace(scanner.Text())
×
163
                return nil
×
164
        } else {
×
165
                return err
×
166
        }
×
167
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc