WIP: feat: translation support
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
See #483
This commit is contained in:
91
cli/run.go
91
cli/run.go
@ -1,6 +1,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@ -10,27 +11,31 @@ import (
|
||||
"coopcloud.tech/abra/cli/recipe"
|
||||
"coopcloud.tech/abra/cli/server"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
"coopcloud.tech/abra/pkg/lang"
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
charmLog "github.com/charmbracelet/log"
|
||||
"github.com/leonelquinteros/gotext"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/cobra/doc"
|
||||
)
|
||||
|
||||
func Run(version, commit string) {
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "abra [cmd] [args] [flags]",
|
||||
Short: "The Co-op Cloud command-line utility belt 🎩🐇",
|
||||
Use: gotext.Get("abra [cmd] [args] [flags]"),
|
||||
Short: gotext.Get("The Co-op Cloud command-line utility belt 🎩🐇"),
|
||||
Version: fmt.Sprintf("%s-%s", version, commit[:7]),
|
||||
ValidArgs: []string{
|
||||
"app",
|
||||
"autocomplete",
|
||||
"catalogue",
|
||||
"man",
|
||||
"recipe",
|
||||
"server",
|
||||
"upgrade",
|
||||
gotext.Get("app"),
|
||||
gotext.Get("autocomplete"),
|
||||
gotext.Get("catalogue"),
|
||||
gotext.Get("man"),
|
||||
gotext.Get("recipe"),
|
||||
gotext.Get("server"),
|
||||
gotext.Get("upgrade"),
|
||||
},
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
dirs := []map[string]os.FileMode{
|
||||
{config.ABRA_DIR: 0764},
|
||||
{config.SERVERS_DIR: 0700},
|
||||
@ -42,7 +47,7 @@ func Run(version, commit string) {
|
||||
for path, perm := range dir {
|
||||
if err := os.Mkdir(path, perm); err != nil {
|
||||
if !os.IsExist(err) {
|
||||
log.Fatal(err)
|
||||
return errors.New(gotext.Get("unable to create %s: %s", path, err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
@ -62,24 +67,57 @@ func Run(version, commit string) {
|
||||
log.SetReportCaller(true)
|
||||
}
|
||||
|
||||
log.Debugf("abra version %s, commit %s", version, commit)
|
||||
if internal.Locale != "" {
|
||||
// FIXME(d1): get list of valid locales from pkg/locales/*
|
||||
switch internal.Locale {
|
||||
case "es":
|
||||
default:
|
||||
return errors.New(gotext.Get("unsupported --lang: %s (want: es)", internal.Locale))
|
||||
}
|
||||
}
|
||||
|
||||
systemLocale := lang.GetLocale()
|
||||
if internal.Locale == "" && systemLocale != "" {
|
||||
// FIXME(d1): validate system locale also (as above FIXME)
|
||||
internal.Locale = systemLocale
|
||||
}
|
||||
|
||||
if internal.Locale == "" {
|
||||
// NOTE(d1): fallback when no --lang=<ll> / LANG=<ll_CC>
|
||||
internal.Locale = "en"
|
||||
}
|
||||
|
||||
// NOTE(d1): only required to load locale when not using en_<CC>
|
||||
// N.B we don't even bother with a _<CC> (country code) atm for en because fuck it
|
||||
if internal.Locale != "en" {
|
||||
if err := lang.LoadLocale(internal.Locale); err != nil {
|
||||
return errors.New(gotext.Get("unable to load supported language %s: %s", internal.Locale, err))
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug(gotext.Get(
|
||||
"abra version: %s, commit: %s, lang: %s",
|
||||
version, formatter.SmallSHA(commit), internal.Locale,
|
||||
))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
|
||||
manCommand := &cobra.Command{
|
||||
Use: "man [flags]",
|
||||
Use: gotext.Get("man [flags]"),
|
||||
Aliases: []string{"m"},
|
||||
Short: "Generate manpage",
|
||||
Example: ` # generate the man pages into /usr/local/share/man/man1
|
||||
Short: gotext.Get("Generate manpage"),
|
||||
Example: gotext.Get(` # generate the man pages into /usr/local/share/man/man1
|
||||
abra_path=$(which abra) # pass abra absolute path to sudo below
|
||||
sudo $abra_path man
|
||||
sudo mandb
|
||||
|
||||
# read the man pages
|
||||
man abra
|
||||
man abra-app-deploy`,
|
||||
man abra-app-deploy`),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
header := &doc.GenManHeader{
|
||||
Title: "ABRA",
|
||||
@ -88,7 +126,7 @@ func Run(version, commit string) {
|
||||
|
||||
manDir := "/usr/local/share/man/man1"
|
||||
if _, err := os.Stat(manDir); os.IsNotExist(err) {
|
||||
log.Fatalf("unable to proceed, '%s' does not exist?")
|
||||
log.Fatal(gotext.Get("unable to proceed, %s does not exist?", manDir))
|
||||
}
|
||||
|
||||
err := doc.GenManTree(rootCmd, header, manDir)
|
||||
@ -96,7 +134,7 @@ func Run(version, commit string) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Info("don't forget to run 'sudo mandb'")
|
||||
log.Info(gotext.Get("don't forget to run 'sudo mandb'"))
|
||||
},
|
||||
}
|
||||
|
||||
@ -105,7 +143,7 @@ func Run(version, commit string) {
|
||||
"debug",
|
||||
"d",
|
||||
false,
|
||||
"show debug messages",
|
||||
gotext.Get("show debug messages"),
|
||||
)
|
||||
|
||||
rootCmd.PersistentFlags().BoolVarP(
|
||||
@ -113,7 +151,7 @@ func Run(version, commit string) {
|
||||
"no-input",
|
||||
"n",
|
||||
false,
|
||||
"toggle non-interactive mode",
|
||||
gotext.Get("toggle non-interactive mode"),
|
||||
)
|
||||
|
||||
rootCmd.PersistentFlags().BoolVarP(
|
||||
@ -121,7 +159,7 @@ func Run(version, commit string) {
|
||||
"offline",
|
||||
"o",
|
||||
false,
|
||||
"prefer offline & filesystem access",
|
||||
gotext.Get("prefer offline & filesystem access"),
|
||||
)
|
||||
|
||||
rootCmd.PersistentFlags().BoolVarP(
|
||||
@ -129,7 +167,16 @@ func Run(version, commit string) {
|
||||
"ignore-env-version",
|
||||
"i",
|
||||
false,
|
||||
"ignore .env version checkout",
|
||||
gotext.Get("ignore .env version checkout"),
|
||||
)
|
||||
|
||||
rootCmd.PersistentFlags().StringVarP(
|
||||
&internal.Locale,
|
||||
"lang",
|
||||
"l",
|
||||
"",
|
||||
// FIXME(d1): provide supported list of language via *.po file listing
|
||||
gotext.Get("force select language (supported: XXX)"),
|
||||
)
|
||||
|
||||
catalogue.CatalogueCommand.AddCommand(
|
||||
|
Reference in New Issue
Block a user