Skip to content

Commit 638eb75

Browse files
authored
[alias] Add no-prefix flag (#2012)
## Summary Adds `--no-prefix` flag to generate aliases for scripts without a prefix. ## How was it tested? ```bash devbox gen alias --no-prefix build ```
1 parent 935f8ee commit 638eb75

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

internal/boxcli/generate.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"regexp"
1010

1111
"github.com/pkg/errors"
12+
"github.com/samber/lo"
1213
"github.com/spf13/cobra"
1314

1415
"go.jetpack.io/devbox/internal/boxcli/usererr"
@@ -34,8 +35,9 @@ type GenerateReadmeCmdFlags struct {
3435
}
3536

3637
type GenerateAliasCmdFlags struct {
37-
config configFlags
38-
prefix string
38+
config configFlags
39+
prefix string
40+
noPrefix bool
3941
}
4042

4143
func generateCmd() *cobra.Command {
@@ -201,6 +203,10 @@ func genAliasCmd() *cobra.Command {
201203
"Usage is typically `eval \"$(devbox gen alias)\"`.",
202204
Args: cobra.ExactArgs(0),
203205
RunE: func(cmd *cobra.Command, args []string) error {
206+
if flags.prefix != "" && flags.noPrefix {
207+
return usererr.New(
208+
"Cannot use both --prefix and --no-prefix flags together")
209+
}
204210
box, err := devbox.Open(&devopt.Opts{
205211
Dir: flags.config.path,
206212
Stderr: cmd.ErrOrStderr(),
@@ -210,17 +216,17 @@ func genAliasCmd() *cobra.Command {
210216
}
211217
re := regexp.MustCompile("[^a-zA-Z0-9_-]+")
212218
prefix := cmp.Or(flags.prefix, box.Config().Root.Name)
213-
if prefix == "" {
219+
if prefix == "" && !flags.noPrefix {
214220
return usererr.New(
215-
"To generate aliases, you must specify a prefix or set a name " +
216-
"in devbox.json")
221+
"To generate aliases, you must specify a prefix, set a name " +
222+
"in devbox.json, or use the --no-prefix flag.")
217223
}
218224
prefix = re.ReplaceAllString(prefix, "-")
219225
for _, script := range box.ListScripts() {
220226
fmt.Fprintf(
221227
cmd.OutOrStdout(),
222-
"alias %s-%s='devbox -c \"%s\" run %s'\n",
223-
prefix,
228+
"alias %s%s='devbox -c \"%s\" run %s'\n",
229+
lo.Ternary(flags.noPrefix, "", prefix+"-"),
224230
script,
225231
box.ProjectDir(),
226232
script,
@@ -232,6 +238,9 @@ func genAliasCmd() *cobra.Command {
232238
flags.config.register(command)
233239
command.Flags().StringVarP(
234240
&flags.prefix, "prefix", "p", "", "Prefix for the generated aliases")
241+
command.Flags().BoolVar(
242+
&flags.noPrefix, "no-prefix", false,
243+
"Do not use a prefix for the generated aliases")
235244

236245
return command
237246
}

0 commit comments

Comments
 (0)