diff --git a/cache/find.go b/cache/find.go new file mode 100644 index 0000000..e3c42d3 --- /dev/null +++ b/cache/find.go @@ -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 "" +} diff --git a/cmd/root.go b/cmd/root.go index 7a1f094..472cbf5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,7 +4,7 @@ package cmd import ( "fmt" "os" - "path/filepath" + "strings" "github.com/Yakiyo/tilde/cache" "github.com/Yakiyo/tilde/config" @@ -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" ) @@ -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() @@ -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 { @@ -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) } @@ -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) }, }