Skip to content

Commit

Permalink
feat: finish command
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakiyo committed Aug 28, 2023
1 parent 2873d51 commit a5fa34d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
93 changes: 93 additions & 0 deletions cache/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cache

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/Yakiyo/tilde/utils"
"github.com/Yakiyo/tilde/where"
"github.com/charmbracelet/log"
"github.com/samber/lo"
"github.com/spf13/viper"
)

// // find a command
// func Find(command string) Command {
// platform := viper.GetString("platform")
// language := viper.GetString("language")
// c, err := ReadIndex()
// if err != "" {
// log.Error(err)
// os.Exit(1)
// }
// // t := Target{
// // Os: platform,
// // Language: language,
// // }
// f, b := lo.Find[Command](c.Commands, func(i Command) bool {
// return i.Name == command
// })
// if !b {
// fmt.Fprintf(
// os.Stderr,
// "Page `%v` not found in cache\nUpdate the cache with `tldr -u` or submit a pr via the following link:\n%v%v",
// command,
// `https://github.com/tldr-pages/tldr/issues/new?title=page%20request:%20`,
// command,
// )
// }
// if !lo.ContainsBy(f.Targets, func(t Target) bool { return t.Os == platform }) {
// log.Infof("Page %v not found for platform %v", command, platform)
// }
// // if platform was not the user platform, we try it first, else we skip too common
// if platform != utils.Platform() {}

// return f
// }

func Find(command string) string {
language := viper.GetString("language")
platform := utils.SafePlatform(viper.GetString("platform"))
if !utils.IsValidPlatform(platform) {
log.Error("Invalid value for platform. Must be one of "+strings.Join(utils.VALID_PLATFORMS, ", "), "platform", platform)
os.Exit(1)
}
var lang_dir string = "pages"
if language != "en" {
lang_dir += "." + language
}
// TODO: check for custom pages dir here

// create a sorted slice of platforms, based on priority
// first comes user provided platform, then the default system platform
// then common, and then the rest
// we use a set, so that dupes get removed
platforms := []string{platform}
if platform != utils.Platform() {
platforms = append(platforms, utils.Platform())
}
platforms = append(platforms, "common")
for _, p := range utils.VALID_PLATFORMS {
if !lo.Contains(platforms, p) {
platforms = append(platforms, p)
}
}
log.Debug("platform hierarchy determined", "slice", platforms)
pages := filepath.Join(where.Cache(), lang_dir)
log.Debug("Using pages dir", "dir", pages)

cmd_file := fmt.Sprintf("%v.md", command)

var path string
for _, platform := range platforms {
log.Info("Searching for page", "platform", platform)
path = filepath.Join(pages, platform, cmd_file)
// if file exists, use it
if utils.FsExists(path) {
return path
}
}
return ""
}
31 changes: 27 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/Yakiyo/tilde/cache"
"github.com/Yakiyo/tilde/config"
Expand All @@ -15,6 +15,7 @@ import (
"github.com/charmbracelet/log"
"github.com/fatih/color"
cc "github.com/ivanpirog/coloredcobra"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ View community driven and simplified man pages in your terminal`,
},
Args: cobra.MaximumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
log.Debug(viper.AllSettings())
log.Debug("Viper settings", "config", viper.AllSettings())

if len(os.Args[1:]) < 1 {
cmd.Help()
Expand All @@ -55,6 +56,7 @@ View community driven and simplified man pages in your terminal`,
log.Fatal("Error downloading cache", "error", err)
}
fmt.Println("Successfully downloaded local cache")
return
}

if clear_cache := utils.Must(cmd.Flags().GetBool("clear-cache")); clear_cache {
Expand All @@ -74,7 +76,7 @@ View community driven and simplified man pages in your terminal`,
}

if seed := utils.Must(cmd.Flags().GetBool("seed-config")); seed {
dir := filepath.Dir(where.Config())
dir := where.Dir()
if !utils.FsExists(dir) {
os.MkdirAll(dir, os.ModePerm)
}
Expand All @@ -84,10 +86,31 @@ View community driven and simplified man pages in your terminal`,
fmt.Println("Successfully seeded config at", where.Config())
}

raw := utils.Must(cmd.Flags().GetBool("raw"))
if rnd := utils.Must(cmd.Flags().GetString("render")); rnd != "" {
render.Render(rnd, utils.Must(cmd.Flags().GetBool("raw")))
rnd = utils.Must(homedir.Expand(rnd))
render.Render(rnd, raw)
return
}

if len(args) < 1 {
log.Error("No args provided. Must provided at least 1 argument")
os.Exit(1)
}

c := strings.ToLower(strings.Join(args, "-"))
f := cache.Find(c)
if f == "" {
fmt.Fprintf(
os.Stderr,
"%v Page `%v` not found in cache\nUpdate the cache with `tldr -u` or submit a pr via the following link:\n%v\n",
color.RedString("ERROR:"),
color.CyanString(c),
color.HiCyanString(`https://github.com/tldr-pages/tldr/issues/new?title=page%20request:%20`+c),
)
os.Exit(1)
}
render.Render(f, raw)
},
}

Expand Down

0 comments on commit a5fa34d

Please # to comment.