summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2021-05-11 15:20:00 -0600
committernytpu <alex@nytpu.com>2021-05-11 15:20:00 -0600
commit27e9066f6fdfed6ee1fbe525fc0713aae85bed39 (patch)
tree0515623aedb14b2bff47bb2c7252a04df76f6dbb
parent880051075869d12836caaf00cfc41d8cfc0cfe53 (diff)
translate errors in comitium.go
-rw-r--r--comitium.go62
-rw-r--r--flags.go2
2 files changed, 41 insertions, 23 deletions
diff --git a/comitium.go b/comitium.go
index 5756098..4e18a04 100644
--- a/comitium.go
+++ b/comitium.go
@@ -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")
}
diff --git a/flags.go b/flags.go
index d27fea7..b634a41 100644
--- a/flags.go
+++ b/flags.go
@@ -35,6 +35,8 @@ var commands = map[string]*flag.FlagSet{
"regenerate": flag.NewFlagSet("regenerate", flag.ExitOnError),
"add": flag.NewFlagSet("add", flag.ExitOnError),
"remove": flag.NewFlagSet("remove", flag.ExitOnError),
+ "help": flag.NewFlagSet("help", flag.ExitOnError),
+ "version": flag.NewFlagSet("version", flag.ExitOnError),
}
// set up various flags for each individual command