Skip to content

Commit

Permalink
Support railway init projectid (#15)
Browse files Browse the repository at this point in the history
* project not found if graphql error when fetching project

* allow initing a project with id
  • Loading branch information
coffee-cup authored Oct 7, 2020
1 parent 8a37445 commit 4129b8a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
75 changes: 52 additions & 23 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions errors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cmd>")
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 <cmd>")
)
4 changes: 2 additions & 2 deletions gateway/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4129b8a

Please sign in to comment.