Skip to content

Commit 0077af2

Browse files
committed
added support for existing evg files
1 parent 62c4c0e commit 0077af2

File tree

3 files changed

+91
-32
lines changed

3 files changed

+91
-32
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/goccy/go-yaml"
7+
"io/ioutil"
8+
"os"
9+
"path"
10+
)
11+
12+
type Config struct {
13+
User string `json:"User"`
14+
BaseUrl string `json:"BaseUrl"`
15+
ApiKey string `json:"ApiKey"`
16+
DefaultProject string `json:"defaultProject"`
17+
}
18+
19+
// LoadConfig loads the config file that contains the User, baseurl and apikey
20+
// required to interact with the evergreen api.
21+
func LoadConfig() (Config, error) {
22+
home, err := os.UserHomeDir()
23+
if err != nil {
24+
return Config{}, err
25+
}
26+
27+
evergreenConfigPath := path.Join(home, ".evergreen.yml")
28+
fileBytes, err := ioutil.ReadFile(evergreenConfigPath)
29+
30+
if err == nil {
31+
fmt.Println(fmt.Sprintf("Found an existing evergreen configuration in %s", evergreenConfigPath))
32+
return loadConfigFromEvergreenYaml(fileBytes)
33+
}
34+
35+
configFilePath := path.Join(home, ".evergreen-prompt.json")
36+
bytes, err := ioutil.ReadFile(configFilePath)
37+
if err != nil {
38+
return Config{}, fmt.Errorf("could not read evergreen-prompt.json config file from: %s", configFilePath)
39+
}
40+
41+
config := Config{}
42+
if err := json.Unmarshal(bytes, &config); err != nil {
43+
return Config{}, err
44+
}
45+
return config, nil
46+
}
47+
48+
type EvergreenYamlConfiguration struct {
49+
BaseUrl string `yaml:"ui_server_host"`
50+
ApiKey string `yaml:"api_key"`
51+
User string `yaml:"User"`
52+
Projects []Project `yaml:"projects"`
53+
}
54+
55+
type Project struct {
56+
Name string `yaml:"name"`
57+
Default bool `yaml:"default"`
58+
ProjectsForDirectory map[string]string `yaml:"projects_for_directory"`
59+
}
60+
61+
func loadConfigFromEvergreenYaml(fileBytes []byte) (Config, error) {
62+
evgConfig := EvergreenYamlConfiguration{}
63+
if err := yaml.Unmarshal(fileBytes, &evgConfig); err != nil {
64+
return Config{}, err
65+
}
66+
67+
defaultProject := ""
68+
for _, p := range evgConfig.Projects {
69+
if p.Default {
70+
defaultProject = p.Name
71+
break
72+
}
73+
}
74+
75+
return Config{
76+
User: evgConfig.User,
77+
BaseUrl: evgConfig.BaseUrl,
78+
ApiKey: evgConfig.ApiKey,
79+
DefaultProject: defaultProject,
80+
}, nil
81+
}

pkg/evergreen/client/evergreen_client.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"fmt"
99
"io/ioutil"
1010
"net/http"
11-
"os"
12-
"path"
1311
)
1412

1513
type EvergreenClient struct {
@@ -20,29 +18,7 @@ type EvergreenClient struct {
2018

2119
projects []string
2220
currentProject string
23-
ActiveProject string
24-
}
25-
26-
type Config struct {
27-
User string `json:"user"`
28-
BaseUrl string `json:"baseUrl"`
29-
ApiKey string `json:"apiKey"`
30-
}
31-
32-
// LoadConfig loads the config file that contains the user, baseurl and apikey
33-
// required to interact with the evergreen api.
34-
func LoadConfig() (Config, error) {
35-
home, err := os.UserHomeDir()
36-
if err != nil {
37-
return Config{}, err
38-
}
39-
configFilePath := path.Join(home, ".evergreen-prompt.json")
40-
bytes, err := ioutil.ReadFile(configFilePath)
41-
config := Config{}
42-
if err := json.Unmarshal(bytes, &config); err != nil {
43-
return Config{}, err
44-
}
45-
return config, nil
21+
DefaultProject string
4622
}
4723

4824
func NewEvergreenClient() (*EvergreenClient, error) {
@@ -55,11 +31,12 @@ func NewEvergreenClient() (*EvergreenClient, error) {
5531

5632
func newEvergreenClientFromConfig(config Config) *EvergreenClient {
5733
return &EvergreenClient{
58-
apiKey: config.ApiKey,
59-
username: config.User,
60-
baseUrl: config.BaseUrl,
61-
client: &http.Client{},
62-
projects: nil,
34+
apiKey: config.ApiKey,
35+
username: config.User,
36+
baseUrl: config.BaseUrl,
37+
client: &http.Client{},
38+
projects: nil,
39+
DefaultProject: config.DefaultProject,
6340
}
6441
}
6542

pkg/evgprompt/executor.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func (e *Executor) Execute(in string) {
3636

3737
if project := flags.GetProjectValue(in); project != "" {
3838
args = append(args, "-p", project)
39+
} else if e.client.DefaultProject != "" {
40+
args = append(args, "-p", e.client.DefaultProject)
3941
}
4042

4143
if flags.HasSpecifiedUncommitted(in) {
@@ -70,9 +72,8 @@ func (e *Executor) Execute(in string) {
7072
}
7173
fmt.Println(string(out))
7274

73-
id := getPatchIdFromCliOutput(string(out))
74-
7575
if priority := flags.GetPriorityValue(in); priority != "" {
76+
id := getPatchIdFromCliOutput(string(out))
7677
// set priority of patch
7778
_, err := e.client.PatchPatch(id, patch.Body{Priority: 10})
7879
if err != nil {

0 commit comments

Comments
 (0)