From 4129b8ad05df9a06b4685dd9007ac385356a084a Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Wed, 7 Oct 2020 15:19:05 -0600 Subject: [PATCH] Support railway init projectid (#15) * project not found if graphql error when fetching project * allow initing a project with id --- cmd/init.go | 75 ++++++++++++++++++++++++++++++++-------------- errors/main.go | 13 ++++---- gateway/project.go | 4 +-- 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 55c5eefbc..186414ab4 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -9,6 +9,27 @@ import ( "github.com/railwayapp/cli/ui" ) +func (h *Handler) saveProjectAndEnvironment(ctx context.Context, project *entity.Project) error { + if len(project.Environments) > 1 { + environment, err := ui.PromptEnvironments(project.Environments) + if err != nil { + return err + } + + err = h.cfg.SetEnvironment(environment.Id) + if err != nil { + return err + } + } else if len(project.Environments) == 1 { + err := h.cfg.SetEnvironment(project.Environments[0].Id) + if err != nil { + return err + } + } + + return nil +} + func (h *Handler) initNew(ctx context.Context, req *entity.CommandRequest) error { name, err := ui.PromptText("Enter project name") if err != nil { @@ -27,11 +48,9 @@ func (h *Handler) initNew(ctx context.Context, req *entity.CommandRequest) error return err } - if len(project.Environments) > 0 { - err = h.cfg.SetEnvironment(project.Environments[0].Id) - if err != nil { - return err - } + err = h.saveProjectAndEnvironment(ctx, project) + if err != nil { + return err } fmt.Printf("🎉 Created project %s\n", name) @@ -51,53 +70,63 @@ func (h *Handler) initFromAccount(ctx context.Context, req *entity.CommandReques return err } - // Todo, prompt for environment - err = h.cfg.SetProjectConfigs(&entity.ProjectConfig{ - Project: project.Id, - Environment: project.Environments[0].Id, - }) + err = h.cfg.SetProject(project.Id) + if err != nil { + return err + } + err = h.saveProjectAndEnvironment(ctx, project) if err != nil { - return nil + return err } + fmt.Printf("Connected to project %s 🎉\n", project.Name) + return nil } -func (h *Handler) initFromID(ctx context.Context, req *entity.CommandRequest) error { - projectId, err := ui.PromptText("Enter your project id") +func (h *Handler) saveProjectWithID(ctx context.Context, projectID string) error { + project, err := h.ctrl.GetProject(ctx, projectID) if err != nil { return err } - project, err := h.ctrl.GetProject(ctx, projectId) + err = h.cfg.SetProject(project.Id) if err != nil { return err } - err = h.cfg.SetProject(project.Id) + err = h.cfg.SetProject(projectID) if err != nil { return err } - err = h.cfg.SetProject(projectId) + err = h.saveProjectAndEnvironment(ctx, project) if err != nil { return err } - if len(project.Environments) > 0 { - err = h.cfg.SetEnvironment(project.Environments[0].Id) - if err != nil { - return err - } - } - fmt.Printf("Connected to project %s 🎉\n", project.Name) return nil } +func (h *Handler) initFromID(ctx context.Context, req *entity.CommandRequest) error { + projectID, err := ui.PromptText("Enter your project id") + if err != nil { + return err + } + + return h.saveProjectWithID(ctx, projectID) +} + func (h *Handler) Init(ctx context.Context, req *entity.CommandRequest) error { + if len(req.Args) > 0 { + // projectID provided as argument + projectID := req.Args[0] + return h.saveProjectWithID(ctx, projectID) + } + isLoggedIn, _ := h.ctrl.IsLoggedIn(ctx) if !isLoggedIn { diff --git a/errors/main.go b/errors/main.go index 151653fd7..4b91089ec 100644 --- a/errors/main.go +++ b/errors/main.go @@ -5,10 +5,11 @@ import "errors" type RailwayError error var ( - UserConfigNotFound RailwayError = errors.New("Not logged in. Please run railway login.") - ProjectConfigNotFound RailwayError = errors.New("Not connected to a project. Run railway init to get started.") - ProjectNotFound RailwayError = errors.New("Project not found.") - ProjectCreateFailed RailwayError = errors.New("There was a problem creating the project") - ProductionTokenNotSet RailwayError = errors.New("RAILWAY_TOKEN environment variable not set") - CommandNotSpecified RailwayError = errors.New("Specify a command to run in side the railway environment. railway run ") + UserConfigNotFound RailwayError = errors.New("Not logged in. Please run railway login.") + ProjectConfigNotFound RailwayError = errors.New("Not connected to a project. Run railway init to get started.") + ProjectNotFound RailwayError = errors.New("Project not found.") + ProblemFetchingProjects RailwayError = errors.New("There was a problem fetching your projects") + ProjectCreateFailed RailwayError = errors.New("There was a problem creating the project") + ProductionTokenNotSet RailwayError = errors.New("RAILWAY_TOKEN environment variable not set") + CommandNotSpecified RailwayError = errors.New("Specify a command to run in side the railway environment. railway run ") ) diff --git a/gateway/project.go b/gateway/project.go index 76e69c5d2..558671250 100644 --- a/gateway/project.go +++ b/gateway/project.go @@ -39,7 +39,7 @@ func (g *Gateway) GetProject(ctx context.Context, projectId string) (*entity.Pro Project *entity.Project `json:"projectById"` } if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil { - return nil, err + return nil, errors.ProjectNotFound } return resp.Project, nil } @@ -153,7 +153,7 @@ func (g *Gateway) GetProjects(ctx context.Context) ([]*entity.Project, error) { } `json:"me"` } if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil { - return nil, err + return nil, errors.ProblemFetchingProjects } return resp.Me.Projects, nil }