-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
168 lines (154 loc) · 5.54 KB
/
client.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
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
package firebase
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
firebase "github.com/rapido-labs/firebase-admin-go/v4"
"github.com/rapido-labs/firebase-admin-go/v4/remoteconfig"
"github.com/rapido-labs/firebase-ctl/internal/config"
"github.com/rapido-labs/firebase-ctl/internal/model"
"github.com/rapido-labs/firebase-ctl/internal/utils"
"github.com/spf13/afero"
"google.golang.org/api/option"
)
type ClientStore struct {
remoteConfigClient ConfigClient
customFs *customFs
}
func (cs *ClientStore) isRemoteEnabled() bool {
return cs.remoteConfigClient != nil
}
func (cs *ClientStore) GetLatestRemoteConfig() (*remoteconfig.RemoteConfig, error) {
if !cs.isRemoteEnabled() {
return nil, fmt.Errorf("remote client is not configured")
}
latestRemoteConfigResponse, err := cs.remoteConfigClient.GetRemoteConfig("")
if err != nil {
return nil, err
}
return latestRemoteConfigResponse.RemoteConfig, err
}
func (cs *ClientStore) BackupRemoteConfig(rc *remoteconfig.RemoteConfig, outputDir string) error {
sourceDump := model.ConvertToSourceConfig(*rc)
for i := range sourceDump.Parameters {
valueType := "string"
if sourceDump.Parameters[i].DefaultValue != nil &&
(strings.HasPrefix(sourceDump.Parameters[i].DefaultValue.ExplicitValue, "{") ||
strings.HasPrefix(sourceDump.Parameters[i].DefaultValue.ExplicitValue, "[")) {
valueType = "json"
}
parameter := sourceDump.Parameters[i]
parameter.ValueType = valueType
sourceDump.Parameters[i] = parameter
}
conditionsFilePath := filepath.Join(outputDir, config.ConditionsDir, config.ConditionsFile)
err := cs.customFs.WriteJsonToFile(sourceDump.Conditions, conditionsFilePath)
if err != nil {
return fmt.Errorf("error writing to conditions file: %v", err.Error())
}
parameterFilePath := filepath.Join(outputDir, config.ParametersDir, config.ParametersFile)
err = cs.customFs.WriteJsonToFile(sourceDump.Parameters, parameterFilePath)
if err != nil {
return fmt.Errorf("error writing to parameter file: %s", err.Error())
}
return nil
}
func (cs *ClientStore) GetLocalConfig(dir string) (*model.Config, error) {
remoteConfig := &model.Config{
Conditions: []model.Condition{},
Parameters: map[string]model.Parameter{},
ParameterGroups: nil,
}
conditionsFilePath := filepath.Join(dir, config.ConditionsDir, config.ConditionsFile)
err := cs.customFs.UnmarshalFromFile(conditionsFilePath, &(remoteConfig.Conditions))
if err != nil {
return remoteConfig, err
}
parametersDirPath := filepath.Join(dir, config.ParametersDir)
err = cs.customFs.UnMarshalFromDir(parametersDirPath, &(remoteConfig.Parameters))
if err != nil {
return remoteConfig, err
}
secretParametersDirPath := filepath.Join(dir, config.SecretParametersDir)
if _, err := os.Stat(secretParametersDirPath); !os.IsNotExist(err) {
err = cs.customFs.UnMarshalFromDir(secretParametersDirPath, &(remoteConfig.Parameters))
return remoteConfig, err
}
return remoteConfig, err
}
func (cs *ClientStore) pushConfigToRemote(rc remoteconfig.RemoteConfig, validateOnly bool) error {
if !cs.isRemoteEnabled() {
return fmt.Errorf("remote client not implemented")
}
template := remoteconfig.Template{
Conditions: rc.Conditions,
Parameters: rc.Parameters,
ParameterGroups: rc.ParameterGroups,
Version: remoteconfig.Version{
Description: "",
IsLegacy: false,
RollbackSource: 0,
UpdateOrigin: "REST_API",
UpdateTime: time.Now(),
UpdateType: "FORCED_UPDATE",
UpdateUser: nil,
VersionNumber: 0,
},
}
_, err := cs.remoteConfigClient.PublishTemplate(context.Background(), template, validateOnly)
if err != nil {
return fmt.Errorf("error publishing template: %s ", err.Error())
}
return nil
}
func (cs *ClientStore) ValidateOnRemote(sourceConfig model.Config) error {
rc := sourceConfig.ToRemoteConfig()
return cs.pushConfigToRemote(*rc, true)
}
func (cs *ClientStore) ApplyConfig(sourceConfig model.Config) error {
rc := sourceConfig.ToRemoteConfig()
return cs.pushConfigToRemote(*rc, false)
}
func (cs *ClientStore) GetRemoteConfigDiff(inputDir string) error {
sourceConfig, err := cs.GetLocalConfig(inputDir)
if err != nil {
return err
}
remoteConfig, err := cs.GetLatestRemoteConfig()
if err != nil {
return err
}
convertedSourceConfig := sourceConfig.ToRemoteConfig()
utils.PrintDiff(*convertedSourceConfig, *remoteConfig)
return nil
}
type ConfigClient interface {
GetRemoteConfig(versionNumber string) (*remoteconfig.Response, error)
PublishTemplate(ctx context.Context, template remoteconfig.Template, validateOnly bool) (*remoteconfig.Template, error)
}
func getFirebaseApp(ctx context.Context) (*firebase.App, error) {
firebaseConfig, err := config.GetFirebaseConfig()
if err != nil {
return nil, err
}
opts := option.WithCredentialsFile(firebaseConfig.Service_account_json_path)
app, err := firebase.NewApp(ctx, nil, opts)
if err != nil {
return nil, fmt.Errorf("error while getting firebase app: %s", err)
}
return app, nil
}
func GetClientStore(ctx context.Context) (*ClientStore, error) {
firebaseApp, err := getFirebaseApp(ctx)
if err != nil {
return &ClientStore{remoteConfigClient: nil, customFs: &customFs{afero.NewOsFs()}} , fmt.Errorf("error creating firebase remote config app: %v", err.Error())
}
client, err := firebaseApp.RemoteConfig(ctx)
if err != nil {
return &ClientStore{remoteConfigClient: client, customFs: &customFs{afero.NewOsFs()}}, fmt.Errorf("error creating firebase remote config client: %v", err.Error())
}
return &ClientStore{remoteConfigClient: client, customFs: &customFs{afero.NewOsFs()}}, nil
}