Skip to content

Commit 13d5ca3

Browse files
authored
Merge pull request #1200 from Scalingo/feat/1083/bash_command
feat(run): add a `bash` alias for us out there often forgetting the `run` in front
2 parents 51e703d + 4768e28 commit 13d5ca3

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* refactor: replace `errgo` and `pkg/errors` with `github.com/Scalingo/go-utils/errors/v3`
77
* refactor: autofix by `go fix` and golangci-lint
88
* fix(error): correctly parse in case of `RequestFailedError`
9+
* feat(run): add a `bash` alias for us out there often forgetting the `run` in front
910

1011
## 1.43.3
1112

cmd/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ var (
168168
&logsCommand,
169169
&logsArchivesCommand,
170170
&runCommand,
171+
&bashCommand,
171172
&oneOffStopCommand,
172173

173174
// Apps Process Actions

cmd/run.go

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
&cli.BoolFlag{Name: "silent", Usage: "Do not output anything on stderr"},
3030
},
3131
Description: `Run command in current app context, a one-off container will be
32-
start with your application environment loaded.
32+
started with your application environment loaded.
3333
3434
Examples
3535
scalingo --app rails-app run bundle exec rails console
@@ -80,29 +80,18 @@ var (
8080
Example
8181
scalingo run --file mysqldump.sql rails dbconsole < /tmp/uploads/mysqldump.sql`,
8282
Action: func(ctx context.Context, c *cli.Command) error {
83-
currentResource := detect.GetCurrentResource(ctx, c)
84-
isDB, err := utils.IsResourceDatabase(ctx, currentResource)
85-
if err != nil && !errors.Is(err, utils.ErrResourceNotFound) {
86-
errorQuit(ctx, err)
87-
}
88-
if isDB {
89-
io.Error("It is currently impossible to use `run` on a database.")
90-
return nil
91-
}
92-
93-
currentApp := currentResource
94-
9583
opts := apps.RunOpts{
96-
App: currentApp,
84+
App: detect.GetCurrentResource(ctx, c),
9785
Cmd: c.Args().Slice(),
98-
Size: c.String("s"),
99-
Type: c.String("t"),
100-
CmdEnv: c.StringSlice("e"),
101-
Files: c.StringSlice("f"),
86+
Size: c.String("size"),
87+
Type: c.String("type"),
88+
CmdEnv: c.StringSlice("env"),
89+
Files: c.StringSlice("file"),
10290
Silent: c.Bool("silent"),
10391
Detached: c.Bool("detached"),
10492
}
105-
if (c.Args().Len() == 0 && c.String("t") == "") || (c.Args().Len() > 0 && c.String("t") != "") {
93+
94+
if (c.Args().Len() == 0 && opts.Type == "") || (c.Args().Len() > 0 && opts.Type != "") {
10695
_ = cli.ShowCommandHelp(ctx, c, "run")
10796
return nil
10897
}
@@ -111,17 +100,59 @@ var (
111100
io.Error("It is currently impossible to use detached one-off with an uploaded file. Please either remove the --detached or --file flags.")
112101
return nil
113102
}
114-
115-
utils.CheckForConsent(ctx, currentApp)
116-
117-
err = apps.Run(ctx, opts)
118-
if err != nil {
119-
errorQuit(ctx, err)
120-
}
121-
return nil
103+
return runOneOffCommand(ctx, opts)
122104
},
123105
ShellComplete: func(_ context.Context, c *cli.Command) {
124106
_ = autocomplete.CmdFlagsAutoComplete(c, "run")
125107
},
126108
}
109+
bashCommand = cli.Command{
110+
Name: "bash",
111+
Category: "App Management",
112+
Usage: "Run bash for your app",
113+
ArgsUsage: "bash-arguments",
114+
Flags: []cli.Flag{&appFlag,
115+
&cli.StringFlag{Name: "size", Aliases: []string{"s"}, Value: "", Usage: "Size of the container"},
116+
&cli.StringSliceFlag{Name: "env", Aliases: []string{"e"}, Usage: "Environment variables"},
117+
&cli.StringSliceFlag{Name: "file", Aliases: []string{"f"}, Usage: "Files to upload"},
118+
&cli.BoolFlag{Name: "silent", Usage: "Do not output anything on stderr"},
119+
},
120+
Description: `Run bash in your current app context, a one-off container will be
121+
started with your application environment loaded.
122+
123+
This command is equivalent to:
124+
scalingo run bash`,
125+
Action: func(ctx context.Context, c *cli.Command) error {
126+
return runOneOffCommand(ctx, apps.RunOpts{
127+
App: detect.GetCurrentResource(ctx, c),
128+
Cmd: append([]string{"bash"}, c.Args().Slice()...),
129+
Size: c.String("size"),
130+
CmdEnv: c.StringSlice("env"),
131+
Files: c.StringSlice("file"),
132+
Silent: c.Bool("silent"),
133+
})
134+
},
135+
ShellComplete: func(_ context.Context, c *cli.Command) {
136+
_ = autocomplete.CmdFlagsAutoComplete(c, "bash")
137+
},
138+
}
127139
)
140+
141+
func runOneOffCommand(ctx context.Context, opts apps.RunOpts) error {
142+
isDB, err := utils.IsResourceDatabase(ctx, opts.App)
143+
if err != nil && !errors.Is(err, utils.ErrResourceNotFound) {
144+
errorQuit(ctx, err)
145+
}
146+
if isDB {
147+
io.Error("It is currently impossible to use `" + opts.Cmd[0] + "` on a database.")
148+
return nil
149+
}
150+
151+
utils.CheckForConsent(ctx, opts.App)
152+
153+
err = apps.Run(ctx, opts)
154+
if err != nil {
155+
errorQuit(ctx, err)
156+
}
157+
return nil
158+
}

0 commit comments

Comments
 (0)