diff --git a/main.go b/main.go index 1f81f2e..28fda66 100644 --- a/main.go +++ b/main.go @@ -8,14 +8,28 @@ import ( ) func main() { - source, language, err := handleFlags() + source, language, global, err := handleFlags() if err != nil { return } + // Get command name command := flag.Args()[0] - page, err := checkLocal(source, language, command) + + if global == false { + // Get page from local folder + page, err := checkLocal(source, language, command) + if err == nil { + fmt.Println("local") + fmt.Println(output(page)) + return + } + } + + // Try to find page in official repository + page, err := checkRemote(language, command) if err == nil { + fmt.Println("global") fmt.Println(output(page)) return } @@ -23,13 +37,14 @@ func main() { fmt.Printf("`%s` documentation is not available. Consider contributing Pull Request to https://github.com/tldr-pages/tldr\n", command) } -func handleFlags() (source, language string, err error) { +func handleFlags() (source, language string, global bool, err error) { fVersion := flag.Bool("version", false, "show program's version number and exit") fUpdateCache := flag.Bool("update_cache", false, "Update the local cache of pages and exit") fPlatform := flag.String("platform", "linux", "Override the operating system [linux, osx, sunos, windows, common]") fList := flag.Bool("list", false, "List all available commands for operating system") fSource := flag.String("source", getLocalPath(), "Override the default page source") fLanguage := flag.String("language", "en", "Override the default language") + fGlobal := flag.Bool("global", false, "Force to get info from official repository") flag.Usage = func() { fmt.Printf("usage: %s [options] command\n\n", os.Args[0]) fmt.Println("Go command line client for tldr\n") @@ -53,6 +68,7 @@ func handleFlags() (source, language string, err error) { source = *fSource language = *fLanguage + global = *fGlobal if len(flag.Args()) != 1 { flag.Usage() err = errors.New("not enough arguments") diff --git a/remote.go b/remote.go new file mode 100644 index 0000000..ef6f6dd --- /dev/null +++ b/remote.go @@ -0,0 +1,51 @@ +package main + +import ( + "errors" + "io/ioutil" + "log" + "net/http" + "strings" +) + +var ( + RemoteBaseURL = "https://raw.githubusercontent.com/tldr-pages/tldr/master/" +) + +func buildRemotePath(language string) string { + folder := "pages" + if language != "en" { + folder += "." + language + } + return RemoteBaseURL + folder +} + +func checkCategory(path, category, name string) (page []string, err error) { + res, err := http.Get(path + "/" + category + "/" + name + ".md") + if err != nil { + return + } + if res.StatusCode == 404 { + err = errors.New("file not found") + return + } + data, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err.Error()) + } + page = strings.Split(string(data), "\n") + return +} + +func checkRemote(language, name string) (page []string, err error) { + path := buildRemotePath(language) + if page, err = checkCategory(path, "common", name); err == nil { + return + } + if page, err = checkCategory(path, "linux", name); err == nil { + return + } + + err = errors.New("no required command") + return +}