Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (h *Handler) Connect(ctx context.Context, req *entity.CommandRequest) error
return err
}

environment, err := h.ctrl.GetEnvironment(ctx)
environment, err := h.ctrl.GetCurrentEnvironment(ctx)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ func (h *Handler) Open(ctx context.Context, req *entity.CommandRequest) error {
if err != nil {
return err
}
environmentId, err := h.cfg.GetEnvironment()
environmentId, err := h.cfg.GetCurrentEnvironment()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason most of the commands call GetEnvironment() as opposed to GetEnvironment to use the environment from flag if it exists?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow? if you're asking why GetEnvironment doesn't just use the flags cause then we'd have to alter it to take the Cmd as a param and when I did it it was nasty

Still not thritted with this implementation but at least it's clear

if err != nil {
return err
}

// If an unknown subcommand is used, show help
if (len(req.Args) > 0) {
if len(req.Args) > 0 {
return req.Cmd.Help()
}

if (req.Cmd.Use == "open") {
if req.Cmd.Use == "open" {
return h.ctrl.OpenProjectInBrowser(ctx, projectId, environmentId)
}

Expand All @@ -33,7 +33,7 @@ func (h *Handler) OpenApp(ctx context.Context, req *entity.CommandRequest) error
if err != nil {
return err
}
environmentId, err := h.cfg.GetEnvironment()
environmentId, err := h.cfg.GetCurrentEnvironment()
if err != nil {
return err
}
Expand Down
54 changes: 20 additions & 34 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import (

var RAIL_PORT = 4411

func (h *Handler) getEnvironment(ctx context.Context, environmentName string) (*entity.Environment, error) {
if environmentName == "" {
return h.ctrl.GetCurrentEnvironment(ctx)
}
return h.ctrl.GetEnvironmentByName(ctx, environmentName)
}

func (h *Handler) Run(ctx context.Context, req *entity.CommandRequest) error {
isEphemeral := false
for _, arg := range req.Args {
Expand All @@ -33,13 +40,16 @@ func (h *Handler) Run(ctx context.Context, req *entity.CommandRequest) error {
return err
}

targetEnvironment := (*string)(nil)
targetEnvironment := ""
parsedArgs := make([]string, 0)
for _, arg := range req.Args {
if matched := rgx.FindStringSubmatch(arg); matched != nil {
if len(matched) < 2 {
return goErr.New("Missing environment selection! \n(e.g --enviroment=production)")
}
targetEnvironment = &matched[1]
targetEnvironment = matched[1]
} else {
parsedArgs = append(parsedArgs, arg)
}
}

Expand All @@ -48,33 +58,10 @@ func (h *Handler) Run(ctx context.Context, req *entity.CommandRequest) error {
return err
}

var environment *entity.Environment
if targetEnvironment != nil {
project, err := h.ctrl.GetProject(ctx, projectCfg.Project)
if err != nil {
return err
}

environment, err = func() (*entity.Environment, error) {
for _, environment := range project.Environments {
if environment.Name == *targetEnvironment {
return environment, nil
}
}
return nil, goErr.New(ui.AlertDanger(fmt.Sprintf("Environment %s does not exist in project", *targetEnvironment)))
}()
if err != nil {
return err
}

} else {
// Get Current Environment for name
environment, err = h.ctrl.GetEnvironment(ctx)
if err != nil {
return err
}
environment, err := h.getEnvironment(ctx, targetEnvironment)
if err != nil {
return err
}

// Add something to the ephemeral env name
if isEphemeral {
environmentName := fmt.Sprintf("%s-ephemeral", environment.Name)
Expand Down Expand Up @@ -107,13 +94,13 @@ func (h *Handler) Run(ctx context.Context, req *entity.CommandRequest) error {
hasDockerfile = false
}

if len(req.Args) == 0 && hasDockerfile {
if len(parsedArgs) == 0 && hasDockerfile {
return h.runInDocker(ctx, pwd, envs)
} else if len(req.Args) == 0 {
} else if len(parsedArgs) == 0 {
return errors.CommandNotSpecified
}

cmd := exec.CommandContext(ctx, req.Args[0], req.Args[1:]...)
cmd := exec.CommandContext(ctx, parsedArgs[0], parsedArgs[1:]...)
cmd.Env = os.Environ()

// Inject railway envs
Expand Down Expand Up @@ -142,11 +129,10 @@ func (h *Handler) Run(ctx context.Context, req *entity.CommandRequest) error {
}

if err != nil {
fmt.Println(err.Error())
if exitError, ok := err.(*exec.ExitError); ok {
fmt.Println(err.Error())
os.Exit(exitError.ExitCode())
}

os.Exit(1)
}

Expand All @@ -168,7 +154,7 @@ func (h *Handler) runInDocker(ctx context.Context, pwd string, envs *entity.Envs
}

// Strip characters not allowed in Docker image names
environment, err := h.ctrl.GetEnvironment(ctx)
environment, err := h.ctrl.GetCurrentEnvironment(ctx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (h *Handler) Status(ctx context.Context, req *entity.CommandRequest) error
if project != nil {
fmt.Printf("Project: %s\n", ui.Bold(fmt.Sprint(ui.MagentaText(project.Name))))

environment, err := h.ctrl.GetEnvironment(ctx)
environment, err := h.ctrl.GetCurrentEnvironment(ctx)
if err != nil {
return err
}
Expand Down
18 changes: 17 additions & 1 deletion cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@ import (
)

func (h *Handler) Up(ctx context.Context, req *entity.CommandRequest) error {
projectConfig, err := h.ctrl.GetProjectConfigs(ctx)
if err != nil {
return err
}
environmentName, err := req.Cmd.Flags().GetString("environment")
if err != nil {
return err
}

environment, err := h.getEnvironment(ctx, environmentName)
if err != nil {
return err
}
ui.StartSpinner(&ui.SpinnerCfg{
Message: "Laying tracks in the clouds...",
})
res, err := h.ctrl.Up(ctx)
res, err := h.ctrl.Upload(ctx, &entity.UploadRequest{
ProjectID: projectConfig.Project,
EnvironmentID: environment.Id,
})
if err != nil {
return err
} else {
Expand Down
6 changes: 3 additions & 3 deletions cmd/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (h *Handler) Variables(ctx context.Context, _ *entity.CommandRequest) error
return err
}

environment, err := h.ctrl.GetEnvironment(ctx)
environment, err := h.ctrl.GetCurrentEnvironment(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest)
return err
}

environment, err := h.ctrl.GetEnvironment(ctx)
environment, err := h.ctrl.GetCurrentEnvironment(ctx)
if err != nil {
return err
}
Expand All @@ -85,7 +85,7 @@ func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandReques
return err
}

environment, err := h.ctrl.GetEnvironment(ctx)
environment, err := h.ctrl.GetCurrentEnvironment(ctx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion configs/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (c *Configs) GetProject() (string, error) {
return projectCfg.Project, nil
}

func (c *Configs) GetEnvironment() (string, error) {
func (c *Configs) GetCurrentEnvironment() (string, error) {
projectCfg, err := c.GetProjectConfigs()
if err != nil {
return "", err
Expand Down
23 changes: 21 additions & 2 deletions controller/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
CLIErrors "github.com/railwayapp/cli/errors"
)

// GetEnvironment returns the currently active environment for the Railway project
func (c *Controller) GetEnvironment(ctx context.Context) (*entity.Environment, error) {
// GetCurrentEnvironment returns the currently active environment for the Railway project
func (c *Controller) GetCurrentEnvironment(ctx context.Context) (*entity.Environment, error) {
projectCfg, err := c.GetProjectConfigs(ctx)
if err != nil {
return nil, err
Expand All @@ -24,6 +24,25 @@ func (c *Controller) GetEnvironment(ctx context.Context) (*entity.Environment, e
return environment, nil
}
}
return nil, CLIErrors.EnvironmentNotSet
}

func (c *Controller) GetEnvironmentByName(ctx context.Context, environmentName string) (*entity.Environment, error) {
projectCfg, err := c.GetProjectConfigs(ctx)
if err != nil {
return nil, err
}

project, err := c.GetProject(ctx, projectCfg.Project)
if err != nil {
return nil, err
}

for _, environment := range project.Environments {
if environment.Name == environmentName {
return environment, nil
}
}
return nil, CLIErrors.EnvironmentNotFound
}

Expand Down
11 changes: 3 additions & 8 deletions controller/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,16 @@ func compress(src string, buf io.Writer) error {
return nil
}

func (c *Controller) Up(ctx context.Context) (*entity.UpResponse, error) {
projectConfig, err := c.GetProjectConfigs(ctx)
if err != nil {
return nil, err
}

func (c *Controller) Upload(ctx context.Context, req *entity.UploadRequest) (*entity.UpResponse, error) {
var buf bytes.Buffer
if err := compress(".", &buf); err != nil {
return nil, err
}

res, err := c.gtwy.Up(ctx, &entity.UpRequest{
Data: buf,
ProjectID: projectConfig.Project,
EnvironmentID: projectConfig.Environment,
ProjectID: req.ProjectID,
EnvironmentID: req.EnvironmentID,
})
if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion entity/requests.go → entity/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package entity

import "bytes"

type UploadRequest struct {
ProjectID string
EnvironmentID string
}

type UpRequest struct {
Data bytes.Buffer
ProjectID string
EnvironmentID string
}

type UpResponse struct {
URL string
URL string
DeploymentDomain string
}
3 changes: 2 additions & 1 deletion errors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var (
ProjectCreateFailed RailwayError = fmt.Errorf("%s\nOne of our trains probably derailed!", ui.RedText("There was a problem creating the project."))
ProjectCreateFromTemplateFailed RailwayError = fmt.Errorf("%s\nOne of our trains probably derailed!", ui.RedText("There was a problem creating the project from template."))
ProductionTokenNotSet RailwayError = fmt.Errorf("%s\nRun %s and head under `tokens` section. You can generate tokens to access Railway environment variables. Set that token in your environment as `RAILWAY_TOKEN=<insert token>` and you're all aboard!", ui.RedText("RAILWAY_TOKEN environment variable not set."), ui.Bold("railway open"))
EnvironmentNotFound RailwayError = fmt.Errorf("%s", ui.RedText("No active environment found. Please select one"))
EnvironmentNotSet RailwayError = fmt.Errorf("%s", ui.RedText("No active environment found. Please select one"))
EnvironmentNotFound RailwayError = fmt.Errorf("%s", ui.RedText("Environment does not exist on project. Specify an existing environment"))
NoGitHubScopesFound RailwayError = fmt.Errorf("%s", ui.RedText("No GitHub organizations found. Please link your GitHub account to Railway and try again."))
CommandNotSpecified RailwayError = fmt.Errorf("%s\nRun %s", ui.RedText("Specify a command to run inside the railway environment. Not providing a command will build and run the Dockerfile in the current directory."), ui.Bold("railway run [cmd]"))
LoginFailed RailwayError = fmt.Errorf("%s", ui.RedText("Login failed"))
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,13 @@ func init() {
RunE: contextualize(handler.Version, handler.Panic),
})

addRootCmd(&cobra.Command{
upCmd := addRootCmd(&cobra.Command{
Use: "up",
Short: "Upload and deploy project from the current directory",
RunE: contextualize(handler.Up, handler.Panic),
}).Flags().BoolP("detach", "d", false, "Detach from cloud build/deploy logs")
})
upCmd.Flags().BoolP("detach", "d", false, "Detach from cloud build/deploy logs")
upCmd.Flags().StringP("environment", "e", "", "Specify an environment o up onto")

addRootCmd(&cobra.Command{
Use: "logs",
Expand Down