Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9966f5
commit 7fc2b70
Showing
7 changed files
with
269 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package segments | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/jandedobbeleer/oh-my-posh/src/mock" | ||
"github.com/stretchr/testify/assert" | ||
mock2 "github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestFirebaseSegment(t *testing.T) { | ||
cases := []struct { | ||
Case string | ||
CfgData string | ||
ActiveConfig string | ||
ExpectedEnabled bool | ||
ExpectedString string | ||
}{ | ||
{ | ||
Case: "happy path", | ||
ExpectedEnabled: true, | ||
ActiveConfig: `{ | ||
"activeProjects": { | ||
"path": "project-name" | ||
} | ||
}`, | ||
ExpectedString: "project-name", | ||
}, | ||
{ | ||
Case: "no active config", | ||
ExpectedEnabled: false, | ||
}, | ||
{ | ||
Case: "empty config", | ||
ActiveConfig: "{}", | ||
ExpectedEnabled: false, | ||
}, | ||
{ | ||
Case: "bad config", | ||
ActiveConfig: "{bad}", | ||
ExpectedEnabled: false, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
env := new(mock.MockedEnvironment) | ||
env.On("Home").Return("home") | ||
env.On("Pwd").Return("path") | ||
fcPath := filepath.Join("home", ".config", "configstore", "firebase-tools.json") | ||
env.On("FileContent", fcPath).Return(tc.ActiveConfig) | ||
env.On("Error", mock2.Anything).Return() | ||
f := Firebase{ | ||
env: env, | ||
} | ||
f.Enabled() | ||
assert.Equal(t, tc.ExpectedEnabled, f.Enabled()) | ||
if tc.ExpectedEnabled { | ||
assert.Equal(t, tc.ExpectedString, renderTemplate(env, f.Template(), f), tc.Case) | ||
} | ||
} | ||
} | ||
|
||
func TestGetFirebaseActiveConfig(t *testing.T) { | ||
data := | ||
`{ | ||
"activeProjects": { | ||
"path": "project-name" | ||
} | ||
}` | ||
cases := []struct { | ||
Case string | ||
ActiveConfig string | ||
ExpectedString string | ||
ExpectedError string | ||
}{ | ||
{ | ||
Case: "happy path", | ||
ActiveConfig: data, | ||
ExpectedString: data, | ||
}, | ||
{ | ||
Case: "no active config", | ||
ActiveConfig: "", | ||
ExpectedError: FIREBASENOACTIVECONFIG, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
env := new(mock.MockedEnvironment) | ||
env.On("Home").Return("home") | ||
configPath := filepath.Join("home", ".config", "configstore") | ||
contentPath := filepath.Join(configPath, "firebase-tools.json") | ||
env.On("FileContent", contentPath).Return(tc.ActiveConfig) | ||
env.On("Error", mock2.Anything).Return() | ||
f := Firebase{ | ||
env: env, | ||
} | ||
got, err := f.getActiveConfig(configPath) | ||
assert.Equal(t, tc.ExpectedString, got, tc.Case) | ||
if len(tc.ExpectedError) > 0 { | ||
assert.EqualError(t, err, tc.ExpectedError, tc.Case) | ||
} else { | ||
assert.NoError(t, err, tc.Case) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.