Skip to content

Commit

Permalink
Added request to remote repository
Browse files Browse the repository at this point in the history
  • Loading branch information
HardDie committed Mar 4, 2021
1 parent 7ad163b commit 414f615
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,43 @@ 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
}

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")
Expand All @@ -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")
Expand Down
51 changes: 51 additions & 0 deletions remote.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 414f615

Please # to comment.