WIP: feat: translation support
Some checks failed
continuous-integration/drone/push Build is failing

See #483
This commit is contained in:
2025-08-19 11:22:52 +02:00
parent 5cf6048ecb
commit d9cffbe6d1
68 changed files with 735 additions and 729 deletions

View File

@ -14,14 +14,15 @@ import (
"coopcloud.tech/abra/pkg/autocomplete"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/log"
"github.com/leonelquinteros/gotext"
"github.com/spf13/cobra"
)
var AppCmdCommand = &cobra.Command{
Use: "command <domain> [service | --local] <cmd> [[args] [flags] | [flags] -- [args]]",
Use: gotext.Get("command <domain> [service | --local] <cmd> [[args] [flags] | [flags] -- [args]]"),
Aliases: []string{"cmd"},
Short: "Run app commands",
Long: `Run an app specific command.
Short: gotext.Get("Run app commands"),
Long: gotext.Get(`Run an app specific command.
These commands are bash functions, defined in the abra.sh of the recipe itself.
They can be run within the context of a service (e.g. app) or locally on your
@ -30,24 +31,24 @@ work station by passing "--local/-l".
N.B. If using the "--" style to pass arguments, flags (e.g. "--local/-l") must
be passed *before* the "--". It is possible to pass arguments without the "--"
as long as no dashes are present (i.e. "foo" works without "--", "-foo"
does not).`,
Example: ` # pass <cmd> args/flags without "--"
does not).`),
Example: gotext.Get(` # pass <cmd> args/flags without "--"
abra app cmd 1312.net app my_cmd_arg foo --user bar
# pass <cmd> args/flags with "--"
abra app cmd 1312.net app my_cmd_args --user bar -- foo -vvv
# drop the [service] arg if using "--local/-l"
abra app cmd 1312.net my_cmd --local`,
abra app cmd 1312.net my_cmd --local`),
Args: func(cmd *cobra.Command, args []string) error {
if local {
if !(len(args) >= 2) {
return errors.New("requires at least 2 arguments with --local/-l")
return errors.New(gotext.Get("requires at least 2 arguments with --local/-l"))
}
if slices.Contains(os.Args, "--") {
if cmd.ArgsLenAtDash() > 2 {
return errors.New("accepts at most 2 args with --local/-l")
return errors.New(gotext.Get("accepts at most 2 args with --local/-l"))
}
}
@ -63,7 +64,7 @@ does not).`,
}
if !(len(args) >= 3) {
return errors.New("requires at least 3 arguments")
return errors.New(gotext.Get("requires at least 3 arguments"))
}
return nil
@ -97,14 +98,14 @@ does not).`,
}
if local && remoteUser != "" {
log.Fatal("cannot use --local & --user together")
log.Fatal(gotext.Get("cannot use --local & --user together"))
}
hasCmdArgs, parsedCmdArgs := parseCmdArgs(args, local)
if _, err := os.Stat(app.Recipe.AbraShPath); err != nil {
if os.IsNotExist(err) {
log.Fatalf("%s does not exist for %s?", app.Recipe.AbraShPath, app.Name)
log.Fatal(gotext.Get("%s does not exist for %s?", app.Recipe.AbraShPath, app.Name))
}
log.Fatal(err)
}
@ -115,7 +116,7 @@ does not).`,
log.Fatal(err)
}
log.Debugf("--local detected, running %s on local work station", cmdName)
log.Debug(gotext.Get("--local detected, running %s on local work station", cmdName))
var exportEnv string
for k, v := range app.Env {
@ -124,16 +125,16 @@ does not).`,
var sourceAndExec string
if hasCmdArgs {
log.Debugf("parsed following command arguments: %s", parsedCmdArgs)
log.Debug(gotext.Get("parsed following command arguments: %s", parsedCmdArgs))
sourceAndExec = fmt.Sprintf("TARGET=local; APP_NAME=%s; STACK_NAME=%s; %s . %s; %s %s", app.Name, app.StackName(), exportEnv, app.Recipe.AbraShPath, cmdName, parsedCmdArgs)
} else {
log.Debug("did not detect any command arguments")
log.Debug(gotext.Get("did not detect any command arguments"))
sourceAndExec = fmt.Sprintf("TARGET=local; APP_NAME=%s; STACK_NAME=%s; %s . %s; %s", app.Name, app.StackName(), exportEnv, app.Recipe.AbraShPath, cmdName)
}
shell := "/bin/bash"
if _, err := os.Stat(shell); errors.Is(err, os.ErrNotExist) {
log.Debugf("%s does not exist locally, use /bin/sh as fallback", shell)
log.Debug(gotext.Get("%s does not exist locally, use /bin/sh as fallback", shell))
shell = "/bin/sh"
}
cmd := exec.Command(shell, "-c", sourceAndExec)
@ -164,15 +165,15 @@ does not).`,
}
if !matchingServiceName {
log.Fatalf("no service %s for %s?", targetServiceName, app.Name)
log.Fatal(gotext.Get("no service %s for %s?", targetServiceName, app.Name))
}
log.Debugf("running command %s within the context of %s_%s", cmdName, app.StackName(), targetServiceName)
log.Debug(gotext.Get("running command %s within the context of %s_%s", cmdName, app.StackName(), targetServiceName))
if hasCmdArgs {
log.Debugf("parsed following command arguments: %s", parsedCmdArgs)
log.Debug(gotext.Get("parsed following command arguments: %s", parsedCmdArgs))
} else {
log.Debug("did not detect any command arguments")
log.Debug(gotext.Get("did not detect any command arguments"))
}
cl, err := client.New(app.Server)
@ -192,9 +193,9 @@ does not).`,
}
var AppCmdListCommand = &cobra.Command{
Use: "list <domain> [flags]",
Use: gotext.Get("list <domain> [flags]"),
Aliases: []string{"ls"},
Short: "List all available commands",
Short: gotext.Get("List all available commands"),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
app := internal.ValidateApp(args)
@ -247,7 +248,7 @@ func init() {
"local",
"l",
false,
"run command locally",
gotext.Get("run command locally"),
)
AppCmdCommand.Flags().StringVarP(
@ -255,7 +256,7 @@ func init() {
"user",
"u",
"",
"request remote user",
gotext.Get("request remote user"),
)
AppCmdCommand.Flags().BoolVarP(
@ -263,7 +264,7 @@ func init() {
"tty",
"T",
false,
"disable remote TTY",
gotext.Get("disable remote TTY"),
)
AppCmdCommand.Flags().BoolVarP(
@ -271,6 +272,6 @@ func init() {
"chaos",
"C",
false,
"ignore uncommitted recipes changes",
gotext.Get("ignore uncommitted recipes changes"),
)
}