-
Notifications
You must be signed in to change notification settings - Fork 111
/
dotenv.go
118 lines (97 loc) · 2.58 KB
/
dotenv.go
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
package devtool
import (
"context"
"fmt"
"os"
"os/exec"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/spf13/cobra"
)
func DotenvCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "dotenv",
Short: "Utilities for managing .env files",
}
cmd.AddCommand(DotenvRefreshCmd(ch))
cmd.AddCommand(DotenvUploadCmd(ch))
return cmd
}
func DotenvRefreshCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "refresh {cloud}",
Short: "Refresh .env file from shared storage",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
preset := args[0]
if preset != "cloud" {
return fmt.Errorf(".env not used for preset %q", preset)
}
err := checkRillRepo()
if err != nil {
return err
}
err = downloadDotenv(cmd.Context(), preset)
if err != nil {
return err
}
return nil
},
}
return cmd
}
func DotenvUploadCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "upload {cloud}",
Short: "Distribute your current .env file to the team",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
preset := args[0]
if preset != "cloud" {
return fmt.Errorf(".env not used for preset %q", preset)
}
err := checkRillRepo()
if err != nil {
return err
}
err = checkDotenv()
if err != nil {
return err
}
ch.PrintfWarn("This will overwrite the .env file in shared storage with the contents of your local .env file.\n")
ch.PrintfWarn("The updated .env will automatically be used by other users of the devtool.\n")
ok, err := cmdutil.ConfirmPrompt("Do you want to continue?", "", false)
if err != nil {
return err
}
if !ok {
return nil
}
err = uploadDotenv(cmd.Context(), preset)
if err != nil {
return err
}
return nil
},
}
return cmd
}
var dotenvURLs = map[string]string{
"cloud": "gs://rill-devtool/dotenv/cloud-dev.env",
}
func checkDotenv() error {
_, err := os.Stat(".env")
if err != nil {
return fmt.Errorf(".env file not found at the root of the rill repository")
}
return nil
}
func downloadDotenv(ctx context.Context, preset string) error {
err := exec.CommandContext(ctx, "gcloud", "storage", "cp", dotenvURLs[preset], ".env").Run()
if err != nil {
return fmt.Errorf("error syncing '.env' file from GCS (you must be a Rill team member and have authenticated `gcloud`): %w", err)
}
return nil
}
func uploadDotenv(ctx context.Context, preset string) error {
return exec.CommandContext(ctx, "gcloud", "storage", "cp", ".env", dotenvURLs[preset]).Run()
}