Skip to content

Commit

Permalink
Merge pull request #26 from mstruebing/reduceComplexity
Browse files Browse the repository at this point in the history
refactor(complexity): remove complexity by using own functions
  • Loading branch information
mstruebing authored Oct 21, 2017
2 parents 3587e77 + 327eba2 commit 3e523ee
Showing 1 changed file with 93 additions and 71 deletions.
164 changes: 93 additions & 71 deletions cmd/tldr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,99 @@ func printVersion() {
fmt.Println("Source available at https://github.com")
}

func main() {
func listAllPages() {
repository, err := cache.NewRepository(remoteURL, ttl)
pages, err := repository.Pages(tldr.CurrentPlatform())
if err != nil {
log.Fatalf("ERROR: getting pages: %s", err)
}

for _, page := range pages {
fmt.Println(page)
}
}

func printSpecificPage(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatal("ERROR: page doesn't exist")
}

page, err := os.Open(path)
if err != nil {
log.Fatal("ERROR: opening the page")
}
defer page.Close()

err = tldr.Write(page, os.Stdout)
if err != nil {
log.Fatalf("ERROR: rendering the page: %s", err)
}
}

func printPage(page string) {
if page == "" {
flag.PrintDefaults()
os.Exit(0)
}

repository, err := cache.NewRepository(remoteURL, ttl)
platform := tldr.CurrentPlatform()
markdown, err := repository.Markdown(platform, page)
if err != nil {
var platforms []string
platforms, err = tldr.AvailablePlatforms(repository)
if err != nil {
log.Fatalf("ERROR: getting available platforms: %s", err)
}

for _, platform = range platforms {
markdown, err = repository.Markdown(platform, page)
if err == nil {
break
}
}
if err != nil {
log.Fatalf("ERROR: no page found for '%s' in any available platform", page)
}
}
defer markdown.Close()

err = tldr.Write(markdown, os.Stdout)
if err != nil {
log.Fatalf("ERROR: writing markdown: %s", err)
}
}

func printPageForPlatform(page string, platform string) {
if page == "" {
log.Fatal("ERROR: no page provided")
}

repository, err := cache.NewRepository(remoteURL, ttl)
markdown, err := repository.Markdown(platform, page)
if err != nil {
log.Fatalf("ERROR: getting markdown for '%s/%s': %s", platform, page, err)
}
defer markdown.Close()

err = tldr.Write(markdown, os.Stdout)
if err != nil {
log.Fatalf("ERROR: writing markdown: %s", err)
}
}

func updatePages() {
repository, err := cache.NewRepository(remoteURL, ttl)
if err != nil {
log.Fatalf("ERROR: creating cache repository: %s", err)
}
err = repository.Reload()
if err != nil {
log.Fatalf("ERROR: updating cache: %s", err)
}
}

func main() {
version := flag.Bool("version", false, versionUsage)
flag.BoolVar(version, "v", false, versionUsage)

Expand All @@ -58,81 +145,16 @@ func main() {
if *version {
printVersion()
} else if *update {
err = repository.Reload()
if err != nil {
log.Fatalf("ERROR: updating cache: %s", err)
}
updatePages()
} else if *render != "" {
if _, err := os.Stat(*render); os.IsNotExist(err) {
log.Fatal("ERROR: page doesn't exist")
}

page, err := os.Open(*render)
if err != nil {
log.Fatal("ERROR: opening the page")
}
defer page.Close()

err = tldr.Write(page, os.Stdout)
if err != nil {
log.Fatalf("ERROR: rendering the page: %s", err)
}
printSpecificPage(*render)
} else if *listAll {
pages, err := repository.Pages(tldr.CurrentPlatform())
if err != nil {
log.Fatalf("ERROR: getting pages: %s", err)
}

for _, page := range pages {
fmt.Println(page)
}
listAllPages()
} else if *platform != "" {
page := flag.Arg(0)
if page == "" {
log.Fatal("ERROR: no page provided")
}

markdown, err := repository.Markdown(*platform, page)
if err != nil {
log.Fatalf("ERROR: getting markdown for '%s/%s': %s", *platform, page, err)
}
defer markdown.Close()

err = tldr.Write(markdown, os.Stdout)
if err != nil {
log.Fatalf("ERROR: writing markdown: %s", err)
}
printPageForPlatform(page, *platform)
} else {
page := flag.Arg(0)
if page == "" {
flag.PrintDefaults()
os.Exit(0)
}

platform := tldr.CurrentPlatform()
markdown, err := repository.Markdown(platform, page)
if err != nil {
var platforms []string
platforms, err = tldr.AvailablePlatforms(repository)
if err != nil {
log.Fatalf("ERROR: getting available platforms: %s", err)
}

for _, platform = range platforms {
markdown, err = repository.Markdown(platform, page)
if err == nil {
break
}
}
if err != nil {
log.Fatalf("ERROR: no page found for '%s' in any available platform", page)
}
}
defer markdown.Close()

err = tldr.Write(markdown, os.Stdout)
if err != nil {
log.Fatalf("ERROR: writing markdown: %s", err)
}
printPage(page)
}
}

0 comments on commit 3e523ee

Please sign in to comment.