diff --git a/cache.go b/cache.go index e5431a0..6de4cb2 100644 --- a/cache.go +++ b/cache.go @@ -23,16 +23,16 @@ func getDBPath(homeDir string) (path string) { return homeDir + "/" + DBDefaultPath } -func buildBucketName(cfg *Config) string { +func buildBucketName(cfg *Config, platform string) string { if *cfg.Language == "en" { - return *cfg.Platform + return platform } - return *cfg.Platform + "." + *cfg.Language + return platform + "." + *cfg.Language } func checkCache(cfg *Config, name string) (page []string, err error) { // If DB not exist, return - if !isFileExists(*cfg.DBSource) { + if !isFileExists(*cfg.DBSource + "/" + DBDefaultName) { err = ErrorCacheNotExists return } @@ -44,11 +44,22 @@ func checkCache(cfg *Config, name string) (page []string, err error) { } defer func() { _ = bw.Close() }() - // Build bucket name from input values - bucketName := buildBucketName(cfg) - data, err := bw.GetDataFromBucket(bucketName, name) - if err != nil { - return + platforms := []string{PlatformCommon, *cfg.Platform} + + // Check file for all platforms + var data string + for _, platform := range platforms { + // Build bucket name from input values + bucketName := buildBucketName(cfg, platform) + data, err = bw.GetDataFromBucket(bucketName, name) + if err == ErrorInvalidKey || err == ErrorCannotFindBucket { + // If such data not exists just check next platform + continue + } + if err != nil { + return + } + break } // Split data to lines @@ -61,7 +72,7 @@ func openCache(cfg *Config) (bw *BoltWrapper, err error) { } func putCache(cfg *Config, bw *BoltWrapper, name string, data []byte) (err error) { - bucketName := buildBucketName(cfg) + bucketName := buildBucketName(cfg, *cfg.Platform) if err = bw.CreateBucketIfNotExists(bucketName); err != nil { return } diff --git a/common.go b/common.go index 84bb4ac..8ccadf3 100644 --- a/common.go +++ b/common.go @@ -3,6 +3,7 @@ package main import ( "archive/zip" "bytes" + "errors" "fmt" "io/ioutil" "net/http" @@ -11,6 +12,14 @@ import ( "os/user" ) +const ( + PlatformCommon = "common" +) + +var ( + ErrorPageNotExists = errors.New("WEB: Page not exists") +) + func isFileExists(path string) (isExist bool) { _, err := os.Stat(path) return !os.IsNotExist(err) @@ -31,6 +40,7 @@ func httpGet(urlString string) (result []byte, err error) { // If response not OK, it means page not exists if resp.StatusCode != http.StatusOK { + err = ErrorPageNotExists return } diff --git a/list.go b/list.go index ea55dc1..e7161bb 100644 --- a/list.go +++ b/list.go @@ -8,7 +8,7 @@ import ( ) func printLocalList(cfg *Config) (commands []string) { - path := buildLocalPath(cfg) + path := buildLocalPath(cfg, *cfg.Platform) if !isFileExists(path) { return } @@ -36,7 +36,7 @@ func printGlobalList(cfg *Config) (commands []string, err error) { defer func() { _ = bw.Close() }() // Build bucket name from input values - bucketName := buildBucketName(cfg) + bucketName := buildBucketName(cfg, *cfg.Platform) if commands, err = bw.GetKeysFromBucket(bucketName); err != nil { return diff --git a/local.go b/local.go index c08925d..f686f6e 100644 --- a/local.go +++ b/local.go @@ -13,27 +13,32 @@ func getLocalPath(homeDir string) (path string) { return homeDir + "/" + FilesDefaultPath } -func buildLocalPath(cfg *Config) string { +func buildLocalPath(cfg *Config, platform string) string { folder := "pages" if *cfg.Language != "en" { folder += "." + *cfg.Language } - return *cfg.Source + "/" + folder + "/" + *cfg.Platform + return *cfg.Source + "/" + folder + "/" + platform } func checkLocal(cfg *Config, name string) (page []string, err error) { - // Build path to the local pages - fileName := buildLocalPath(cfg) + "/" + name + ".md" - // If page not exist, just return - if isFileExists(fileName) { - var data []byte - // Read page data - data, err = ioutil.ReadFile(fileName) - if err != nil { - return + platforms := []string{PlatformCommon, *cfg.Platform} + + // Check file for all platforms + for _, platform := range platforms { + // Build path to the local pages + fileName := buildLocalPath(cfg, platform) + "/" + name + ".md" + // If page not exist, just return + if isFileExists(fileName) { + var data []byte + // Read page data + data, err = ioutil.ReadFile(fileName) + if err != nil { + return + } + // Split text to lines + page = strings.Split(string(data), "\n") } - // Split text to lines - page = strings.Split(string(data), "\n") } return } diff --git a/remote.go b/remote.go index ef38ee7..6ba7787 100644 --- a/remote.go +++ b/remote.go @@ -8,22 +8,32 @@ const ( RemoteBaseURL = "https://raw.githubusercontent.com/tldr-pages/tldr/master" ) -func buildRemotePath(cfg *Config) string { +func buildRemotePath(cfg *Config, platform string) string { folder := "pages" if *cfg.Language != "en" { folder += "." + *cfg.Language } - return RemoteBaseURL + "/" + folder + "/" + *cfg.Platform + return RemoteBaseURL + "/" + folder + "/" + platform } func checkRemote(cfg *Config, name string) (page []string, err error) { - // Build url to possible tldr page - url := buildRemotePath(cfg) + platforms := []string{PlatformCommon, *cfg.Platform} - // Get page from official repository - data, err := httpGet(url + "/" + name + ".md") - if err != nil { - return + var data []byte + for _, platform := range platforms { + // Build url to possible tldr page + url := buildRemotePath(cfg, platform) + + // Get page from official repository + data, err = httpGet(url + "/" + name + ".md") + if err == ErrorPageNotExists { + // If such page not exists, just check another platform + continue + } + if err != nil { + return + } + break } if len(data) == 0 { return diff --git a/version.go b/version.go index a292029..3a24ad4 100644 --- a/version.go +++ b/version.go @@ -1,7 +1,7 @@ package main var ( - VERSION = "0.5" + VERSION = "0.6" ) func getVersion() string {