-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
firebase.go
83 lines (66 loc) · 1.72 KB
/
firebase.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
package segments
import (
"encoding/json"
"errors"
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)
const (
FIREBASENOACTIVECONFIG = "NO ACTIVE CONFIG FOUND"
)
type Firebase struct {
props properties.Properties
env platform.Environment
Project string
}
type FirebaseData struct {
ActiveProject map[string]string `json:"activeProjects"`
}
func (f *Firebase) Template() string {
return " {{ .Project}} "
}
func (f *Firebase) Init(props properties.Properties, env platform.Environment) {
f.props = props
f.env = env
}
func (f *Firebase) Enabled() bool {
cfgDir := filepath.Join(f.env.Home(), ".config", "configstore")
configFile, err := f.getActiveConfig(cfgDir)
if err != nil {
f.env.Error(err)
return false
}
data, err := f.getFirebaseData(configFile)
if err != nil {
f.env.Error(err)
return false
}
// Within the activeProjects is a key value pair
// of the path to the project and the project name
// Test if the current directory is a project path
// and if it is, return the project name
for key, value := range data.ActiveProject {
if key == f.env.Pwd() {
f.Project = value
return true
}
}
return false
}
func (f *Firebase) getActiveConfig(cfgDir string) (string, error) {
activeConfigFile := filepath.Join(cfgDir, "firebase-tools.json")
activeConfigData := f.env.FileContent(activeConfigFile)
if len(activeConfigData) == 0 {
return "", errors.New(FIREBASENOACTIVECONFIG)
}
return activeConfigData, nil
}
func (f *Firebase) getFirebaseData(configFile string) (*FirebaseData, error) {
var data FirebaseData
err := json.Unmarshal([]byte(configFile), &data)
if err != nil {
return nil, err
}
return &data, nil
}