Skip to content

Commit

Permalink
Check common platform by default
Browse files Browse the repository at this point in the history
  • Loading branch information
HardDie committed Apr 27, 2021
1 parent 32b1481 commit c7a4f0b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 34 deletions.
31 changes: 21 additions & 10 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func printLocalList(cfg *Config) (commands []string) {
path := buildLocalPath(cfg)
path := buildLocalPath(cfg, *cfg.Platform)
if !isFileExists(path) {
return
}
Expand Down Expand Up @@ -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
Expand Down
31 changes: 18 additions & 13 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 18 additions & 8 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

var (
VERSION = "0.5"
VERSION = "0.6"
)

func getVersion() string {
Expand Down

0 comments on commit c7a4f0b

Please sign in to comment.