translate errors in comitium.go

This commit is contained in:
nytpu
2021-05-11 15:20:00 -06:00
parent 8800510758
commit 27e9066f6f
2 changed files with 41 additions and 23 deletions
+39 -23
View File
@@ -28,21 +28,15 @@ var supportedLangs = []language.Tag{
}
func main() {
argCheck(os.Args, 2, true, "Please provide a command.")
// figure out what language to use (just getting $LANG if set since flags
// aren't parsed yet)
localizer := getLang()
// these are special cases where we shouldn't bother to do anything else
if os.Args[1] == "help" {
usage()
os.Exit(0)
}
if os.Args[1] == "version" {
fmt.Printf("comitium v%v, commit %v\n", core.Version, core.Commit)
os.Exit(0)
}
argCheck(os.Args, 2, true, localizer.Get("errors.no_command"))
c, ok := commands[os.Args[1]]
if !ok {
fmt.Fprint(os.Stderr, "Unrecognized Command.\n\n")
fmt.Fprint(os.Stderr, core.TranslateStringLocalizer(localizer, "%s\n\n", "errors.bad_command", nil))
usage()
os.Exit(1)
}
@@ -50,34 +44,53 @@ func main() {
c.AddFlagSet(globalFs)
c.Parse(os.Args[2:])
// figure out what language to use
localizer := getLang()
// figure out what language to use now that flags are parsed
localizer = getLang()
// these are special cases where we shouldn't try to touch data paths
if os.Args[1] == "help" {
usage()
os.Exit(0)
}
if os.Args[1] == "version" {
fmt.Printf(core.TranslateStringLocalizer(
localizer, "%s\n", "errors.version",
&localizations.Replacements{"version": core.Version, "commit": core.Commit},
))
os.Exit(0)
}
// dataPath is the location where all the data (list of subs, cached hashes,
// etcetera) are stored
dataPath := getDataPath()
dataPath := getDataPath(localizer)
// make sure it exists so we can start working with files in it
os.MkdirAll(dataPath, os.ModePerm)
err := core.Init(dataPath, localizer)
if err != nil {
fmt.Fprintf(os.Stderr, "Error in initialization: %v\n", err)
fmt.Fprintf(os.Stderr, core.TranslateStringLocalizer(
localizer, "%s\n",
"errors.core_init", &localizations.Replacements{"error": err},
))
os.Exit(1)
}
switch os.Args[1] {
case "refresh":
argCheck(c.Args(), 0, false, "'refresh' doesn't take an argument!")
argCheck(c.Args(), 0, false, core.TranslateString("'refresh' %s", "errors.no_argument", nil))
core.Timeout = *refreshTimeoutFlag
fetch.RefreshAll(&core.Data, *refreshWorkersFlag)
case "regenerate":
argCheck(c.Args(), 0, false, "'regenerate' doesn't take an argument!")
argCheck(c.Args(), 0, false, core.TranslateString("'regenerate' %s", "errors.no_argument", nil))
case "add":
argCheck(c.Args(), 1, true, "Please provide a URL to add.")
argCheck(c.Args(), 1, true, localizer.Get("errors.no_url_add"))
remote, err := url.Parse(c.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing URL: %v\n", err)
fmt.Fprintf(os.Stderr, core.TranslateString(
"%s\n", "errors.url_parse",
&localizations.Replacements{"error": err},
))
os.Exit(1)
}
@@ -87,11 +100,14 @@ func main() {
err = fetch.Feed(&core.Data, remote, c.Arg(1))
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error adding URL: %v\n", err)
fmt.Fprintf(os.Stderr, core.TranslateString(
"%s\n", "errors.url_add",
&localizations.Replacements{"error": err},
))
os.Exit(1)
}
case "remove":
argCheck(c.Args(), 1, false, "Please provide a URL to remove.")
argCheck(c.Args(), 1, false, localizer.Get("errors.no_url_remove"))
if (!*removeFeedsFlag && !*removePagesFlag) || *removeFeedsFlag {
delete(core.Data.Feeds, c.Arg(0))
}
@@ -140,7 +156,7 @@ func verifyPath(p string) string {
// the first that exists:
// --data flag, $COMITIUM_DATA, $XDG_DATA_HOME/comitium,
// $HOME/.local/share/comitium, $HOME/.comitium, $PWD (last resort)
func getDataPath() string {
func getDataPath(localizer *localizations.Localizer) string {
if *dataFlag != "" {
return verifyPath(*dataFlag)
}
@@ -160,7 +176,7 @@ func getDataPath() string {
return verifyPath(home + "/.comitium")
}
fmt.Fprintln(os.Stderr, "Error finding a suitable directory! Falling back to the current working directory. See \"comitium help\" or comitium(1) for more information.")
fmt.Fprintln(os.Stderr, localizer.Get("errors.no_data_dir"))
return verifyPath("comitium")
}