Skip to content

Commit

Permalink
can now parse -ll or --list-languages
Browse files Browse the repository at this point in the history
  • Loading branch information
gernotstarke committed Dec 8, 2024
1 parent c381b5e commit dd3a4fa
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 41 deletions.
51 changes: 10 additions & 41 deletions go-app/internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cli
import (
"flag"
"fmt"
"io/ioutil"
"github.com/rs/zerolog/log"
"os"
"path/filepath"
"pdfminion/internal/domain"
Expand Down Expand Up @@ -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()

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions go-app/internal/domain/language.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package domain

import (
"fmt"
"golang.org/x/text/language"
"golang.org/x/text/language/display"
"os"
Expand Down Expand Up @@ -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])
}

}
44 changes: 44 additions & 0 deletions go-app/internal/domain/minionTypes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package domain

import (
"fmt"
"golang.org/x/text/language"
"io/ioutil"
"os"
)

// MinionConfig holds configuration items
Expand Down Expand Up @@ -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
}

0 comments on commit dd3a4fa

Please sign in to comment.