()
| 204 | } |
| 205 | |
| 206 | func genAliasCmd() *cobra.Command { |
| 207 | flags := &GenerateAliasCmdFlags{} |
| 208 | |
| 209 | command := &cobra.Command{ |
| 210 | Use: "alias", |
| 211 | Short: "Generate shell script aliases for this project", |
| 212 | Long: "Generate shell script aliases for this project. " + |
| 213 | "Usage is typically `eval \"$(devbox gen alias)\"`.", |
| 214 | Args: cobra.ExactArgs(0), |
| 215 | RunE: func(cmd *cobra.Command, args []string) error { |
| 216 | if flags.prefix != "" && flags.noPrefix { |
| 217 | return usererr.New( |
| 218 | "Cannot use both --prefix and --no-prefix flags together") |
| 219 | } |
| 220 | box, err := devbox.Open(&devopt.Opts{ |
| 221 | Dir: flags.config.path, |
| 222 | Stderr: cmd.ErrOrStderr(), |
| 223 | }) |
| 224 | if err != nil { |
| 225 | return errors.WithStack(err) |
| 226 | } |
| 227 | re := regexp.MustCompile("[^a-zA-Z0-9_-]+") |
| 228 | prefix := cmp.Or(flags.prefix, box.Config().Root.Name) |
| 229 | if prefix == "" && !flags.noPrefix { |
| 230 | return usererr.New( |
| 231 | "To generate aliases, you must specify a prefix, set a name " + |
| 232 | "in devbox.json, or use the --no-prefix flag.") |
| 233 | } |
| 234 | prefix = re.ReplaceAllString(prefix, "-") |
| 235 | for _, script := range box.ListScripts() { |
| 236 | fmt.Fprintf( |
| 237 | cmd.OutOrStdout(), |
| 238 | "alias %s%s='devbox -c \"%s\" run %s'\n", |
| 239 | lo.Ternary(flags.noPrefix, "", prefix+"-"), |
| 240 | script, |
| 241 | box.ProjectDir(), |
| 242 | script, |
| 243 | ) |
| 244 | } |
| 245 | return nil |
| 246 | }, |
| 247 | } |
| 248 | flags.config.register(command) |
| 249 | command.Flags().StringVarP( |
| 250 | &flags.prefix, "prefix", "p", "", "Prefix for the generated aliases") |
| 251 | command.Flags().BoolVar( |
| 252 | &flags.noPrefix, "no-prefix", false, |
| 253 | "Do not use a prefix for the generated aliases") |
| 254 | |
| 255 | return command |
| 256 | } |
| 257 | |
| 258 | func runGenerateCmd(cmd *cobra.Command, flags *generateCmdFlags) error { |
| 259 | // Check the directory exists. |
no test coverage detected