diff --git a/go-app/internal/cli/cli.go b/go-app/internal/cli/cli.go index 59cf12f..a79a513 100644 --- a/go-app/internal/cli/cli.go +++ b/go-app/internal/cli/cli.go @@ -3,7 +3,7 @@ package cli import ( "flag" "fmt" - "io/ioutil" + "github.com/rs/zerolog/log" "os" "path/filepath" "pdfminion/internal/domain" @@ -38,6 +38,9 @@ func ParseOptions() (*domain.MinionConfig, error) { flag.BoolVar(&opts.Version, "version", false, domain.Flags["version"].Help) flag.BoolVar(&opts.Version, "v", false, "") + flag.BoolVar(&opts.ListLanguages, "list-languages", false, domain.Flags["list-languages"].Help) + flag.BoolVar(&opts.ListLanguages, "ll", false, "") + flag.Usage = printHelp flag.Parse() @@ -52,8 +55,13 @@ func ParseOptions() (*domain.MinionConfig, error) { os.Exit(0) } + if opts.ListLanguages { + log.Info().Msg("List available languages") + domain.PrintLanguages() + os.Exit(0) + } // Only validate directories if we're actually going to process files - if err := opts.validate(); err != nil { + if err := opts.Validate(); err != nil { return nil, err } @@ -135,42 +143,3 @@ func getDaySuffix(day string) string { return "th" } } - -func (o *domain.MinionConfig) validate() error { - if err := o.validateSourceDir(); err != nil { - return err - } - return o.validateTargetDir() -} - -func (o *domain.MinionConfig) validateSourceDir() error { - if _, err := os.Stat(o.SourceDir); os.IsNotExist(err) { - return fmt.Errorf("source directory %q does not exist", o.SourceDir) - } - return nil -} - -func (o *domain.MinionConfig) validateTargetDir() error { - if _, err := os.Stat(o.TargetDir); os.IsNotExist(err) { - fmt.Printf("Target directory '%s' does not exist. Creating it...\n", o.TargetDir) - if err := os.MkdirAll(o.TargetDir, os.ModePerm); err != nil { - return fmt.Errorf("Failed to create directory '%s': %v", o.TargetDir, err) - } - return nil - } - - if o.Force { - return nil - } - - files, err := ioutil.ReadDir(o.TargetDir) - if err != nil { - return fmt.Errorf("Cannot read directory '%s': %v", o.TargetDir, err) - } - - if len(files) > 0 { - return fmt.Errorf("Target directory '%s' is not empty. Use --force to override", o.TargetDir) - } - - return nil -} diff --git a/go-app/internal/domain/language.go b/go-app/internal/domain/language.go index 126c48b..3944ec5 100644 --- a/go-app/internal/domain/language.go +++ b/go-app/internal/domain/language.go @@ -1,6 +1,7 @@ package domain import ( + "fmt" "golang.org/x/text/language" "golang.org/x/text/language/display" "os" @@ -48,3 +49,33 @@ func ValidateLanguage(lang string) (language.Tag, error) { tag, _, _ = matcher.Match(tag) return tag, nil } + +func ListAvailableLanguages() [][]string { + languages := [][]string{} + for _, tag := range supportedLanguages { + // Get language code (two-letter code) + langCode := tag.String() + + // Get language name in the language itself + nameInLanguage := GetLanguageName(tag) + + // Get language name in English + nameInEnglish := GetLanguageNameInEnglish(tag) + + languages = append(languages, []string{ + langCode, // Language code + nameInLanguage, // Name in native language + nameInEnglish, // Name in English + }) + } + return languages +} + +func PrintLanguages() { + languages := ListAvailableLanguages() + for _, lang := range languages { + // Each `lang` is a []string with the format [langCode, nameInLanguage, nameInEnglish] + fmt.Printf("Code: %s, Native: %s, English: %s\n", lang[0], lang[1], lang[2]) + } + +} diff --git a/go-app/internal/domain/minionTypes.go b/go-app/internal/domain/minionTypes.go index a50e868..2e4045f 100644 --- a/go-app/internal/domain/minionTypes.go +++ b/go-app/internal/domain/minionTypes.go @@ -1,7 +1,10 @@ package domain import ( + "fmt" "golang.org/x/text/language" + "io/ioutil" + "os" ) // MinionConfig holds configuration items @@ -155,3 +158,44 @@ var AppVersion string func SetAppVersion(version string) { AppVersion = version } + +func (conf *MinionConfig) Validate() error { + if err := conf.validateSourceDir(); err != nil { + return err + } + return conf.validateTargetDir() +} + +func (conf *MinionConfig) validateSourceDir() error { + if _, err := os.Stat(conf.SourceDir); os.IsNotExist(err) { + return fmt.Errorf("source directory %q does not exist", conf.SourceDir) + } + return nil +} + +func (conf *MinionConfig) validateTargetDir() error { + if _, err := os.Stat(conf.TargetDir); os.IsNotExist(err) { + fmt.Printf("Target directory '%s' does not exist. Creating it...\n", conf.TargetDir) + if err := os.MkdirAll(conf.TargetDir, os.ModePerm); err != nil { + return fmt.Errorf("Failed to create directory '%s': %v", conf.TargetDir, err) + } + return nil + } + + if conf.Force { + return nil + } + + // TODO: replace deprecated ReadDir() with suggested replacement + files, err := ioutil.ReadDir(conf.TargetDir) + + if err != nil { + return fmt.Errorf("Cannot read directory '%s': %v", conf.TargetDir, err) + } + + if len(files) > 0 { + return fmt.Errorf("Target directory '%s' is not empty. Use --force to override", conf.TargetDir) + } + + return nil +}