forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.go
More file actions
172 lines (147 loc) · 3.46 KB
/
Copy pathproject.go
File metadata and controls
172 lines (147 loc) · 3.46 KB
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
169
170
171
172
package gateway
import (
"context"
"fmt"
gql "github.com/machinebox/graphql"
"github.com/pkg/browser"
configs "github.com/railwayapp/cli/configs"
"github.com/railwayapp/cli/entity"
"github.com/railwayapp/cli/errors"
)
// GetProject returns the project associated with the projectId, as well as
// it's environments, plugins, etc
func (g *Gateway) GetProject(ctx context.Context, projectId string) (*entity.Project, error) {
gqlReq := gql.NewRequest(`
query ($projectId: ID!) {
projectById(projectId: $projectId) {
id,
name,
plugins {
id,
name,
},
environments {
id,
name
},
}
}
`)
gqlReq.Var("projectId", projectId)
g.authorize(ctx, gqlReq.Header)
var resp struct {
Project *entity.Project `json:"projectById"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProjectConfigNotFound
}
return resp.Project, nil
}
func (g *Gateway) CreateProject(ctx context.Context, req *entity.CreateProjectRequest) (*entity.Project, error) {
gqlReq := gql.NewRequest(`
mutation($name: String) {
createProject(name: $name) {
id,
name
environments {
id
name
}
}
}
`)
g.authorize(ctx, gqlReq.Header)
gqlReq.Var("name", req.Name)
var resp struct {
Project *entity.Project `json:"createProject"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProjectCreateFailed
}
return resp.Project, nil
}
func (g *Gateway) UpdateProject(ctx context.Context, req *entity.UpdateProjectRequest) (*entity.Project, error) {
gqlReq := gql.NewRequest(`
mutation($projectId: ID!) {
updateProject(projectId: $projectId) {
id,
name
}
}
`)
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
gqlReq.Var("projectId", req.Id)
var resp struct {
Project *entity.Project `json:"createProject"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, err
}
return resp.Project, nil
}
func (g *Gateway) DeleteProject(ctx context.Context, projectId string) error {
gqlReq := gql.NewRequest(`
mutation($projectId: ID!) {
deleteProject(projectId: $projectId)
}
`)
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return err
}
gqlReq.Var("projectId", projectId)
var resp struct {
Deleted bool `json:"deleteProject"`
}
return g.gqlClient.Run(ctx, gqlReq, &resp)
}
// GetProjects returns all projects associated with the user, as well as
// their environments associated with those projects, error otherwise
// Performs a dual join
func (g *Gateway) GetProjects(ctx context.Context) ([]*entity.Project, error) {
gqlReq := gql.NewRequest(`
query {
me {
projects {
id,
name,
plugins {
id,
name,
},
environments {
id,
name
},
}
}
}
`)
// TODO build this into the GQL client
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
var resp struct {
Me struct {
Projects []*entity.Project
} `json:"me"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProblemFetchingProjects
}
return resp.Me.Projects, nil
}
func GetRailwayUrl() string {
url := "https://railway.app"
if configs.IsDevMode() {
url = fmt.Sprintf("http://localhost:3000")
}
return url
}
func (g *Gateway) OpenProjectInBrowser(projectID string, environmentID string) {
browser.OpenURL(fmt.Sprintf("%s/project/%s?environmentId=%s", GetRailwayUrl(), projectID, environmentID))
}