Skip to content

Commit b063990

Browse files
Anoop KunjuramanAnoop Kunjuraman
Anoop Kunjuraman
authored and
Anoop Kunjuraman
committed
Initial commit
0 parents  commit b063990

File tree

277 files changed

+108649
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

277 files changed

+108649
-0
lines changed

Godeps/Godeps.json

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/Readme

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
# How to build
4+
5+
Enable vendor experiment in Go:
6+
7+
```export GO15VENDOREXPERIMENT=1```
8+
9+
To build:
10+
11+
```go build```
12+
13+
# Adding Dependencies
14+
15+
First get the godep package
16+
17+
```go get github.com/tools/godep```
18+
19+
Use go get as usual to get the dependency. And then from the root project folder, run:
20+
21+
```export GO15VENDOREXPERIMENT=1```
22+
23+
And
24+
25+
```godep save```
26+
27+
# TODO
28+
- Auto detection of platform
29+
- Check in common if platform command not found
30+
- Check /usr/share/ location by default and switch back to ~/.tldr if not found
31+
- RPM, DEB packages
32+
- Windows support
33+
34+
- better error handling
35+
- Proxy support

app/cache.go

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package app
2+
3+
import (
4+
"archive/zip"
5+
"io"
6+
"net/http"
7+
"os"
8+
"os/user"
9+
"path/filepath"
10+
11+
"github.com/Sirupsen/logrus"
12+
)
13+
14+
func UpdateCache(config Config) error {
15+
LOG.Info("Downloading fresh tldr's from " + config.SourceURL)
16+
cacheLocation := getCacheLocation()
17+
if err := os.RemoveAll(cacheLocation); err != nil {
18+
LOG.Fatal(err)
19+
}
20+
LOG.Debug("Deleted cache location, if it had existed ")
21+
if err := os.MkdirAll(cacheLocation, 0755); err != nil {
22+
LOG.Fatal(err)
23+
}
24+
LOG.Debug("Created cache location")
25+
26+
zipLocation := filepath.Join(cacheLocation, "master.zip")
27+
out, err := os.Create(zipLocation)
28+
if err != nil {
29+
LOG.Fatal(err)
30+
}
31+
defer out.Close()
32+
resp, err := http.Get(config.SourceURL)
33+
if err != nil {
34+
LOG.Fatal(err)
35+
}
36+
defer resp.Body.Close()
37+
io.Copy(out, resp.Body)
38+
LOG.Debug("Download complete")
39+
if err := Unzip(zipLocation, cacheLocation); err != nil {
40+
LOG.Fatal(err)
41+
}
42+
os.Remove(zipLocation)
43+
LOG.Debug("Unzip completed")
44+
return nil
45+
}
46+
47+
func LocalCacheAvailable() bool {
48+
LOG.Debug("In localCacheAvailable()")
49+
path := filepath.Join(getCacheLocation(), "tldr-master", "pages", "index.json")
50+
51+
LOG.WithFields(logrus.Fields{
52+
"index.json.path": path,
53+
}).Debug("Printing index.json.path")
54+
55+
if _, err := os.Stat(path); err == nil {
56+
LOG.Debug("Local cache exists. Returning true")
57+
return true
58+
}
59+
LOG.Info("Local cached tldr's doesn't exist. Will have to download now.")
60+
return false
61+
}
62+
63+
func getCacheLocation() string {
64+
usr, err := user.Current()
65+
if err != nil {
66+
LOG.Fatal(err)
67+
}
68+
LOG.WithFields(logrus.Fields{
69+
"usr.HomeDir": usr.HomeDir,
70+
}).Debug("Printing usr.HomeDir")
71+
72+
path := filepath.Join(usr.HomeDir, ".tldr", "cache")
73+
return path
74+
}
75+
76+
func getPageLocation(command, platform string) (string, error) {
77+
path := filepath.Join(getCacheLocation(), "tldr-master", "pages", platform, command+".md")
78+
if _, err := os.Stat(path); err == nil {
79+
LOG.WithFields(logrus.Fields{
80+
"path": path,
81+
}).Debug("Page available")
82+
return path, nil
83+
}
84+
return "", COMMAND_NOT_FOUND
85+
}
86+
87+
func Unzip(src, dest string) error {
88+
r, err := zip.OpenReader(src)
89+
if err != nil {
90+
return err
91+
}
92+
defer func() {
93+
if err := r.Close(); err != nil {
94+
panic(err)
95+
}
96+
}()
97+
98+
os.MkdirAll(dest, 0755)
99+
100+
// Closure to address file descriptors issue with all the deferred .Close() methods
101+
extractAndWriteFile := func(f *zip.File) error {
102+
rc, err := f.Open()
103+
if err != nil {
104+
return err
105+
}
106+
defer func() {
107+
if err := rc.Close(); err != nil {
108+
panic(err)
109+
}
110+
}()
111+
112+
path := filepath.Join(dest, f.Name)
113+
114+
if f.FileInfo().IsDir() {
115+
os.MkdirAll(path, f.Mode())
116+
} else {
117+
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
118+
if err != nil {
119+
return err
120+
}
121+
defer func() {
122+
if err := f.Close(); err != nil {
123+
panic(err)
124+
}
125+
}()
126+
127+
_, err = io.Copy(f, rc)
128+
if err != nil {
129+
return err
130+
}
131+
}
132+
return nil
133+
}
134+
135+
for _, f := range r.File {
136+
err := extractAndWriteFile(f)
137+
if err != nil {
138+
return err
139+
}
140+
}
141+
142+
return nil
143+
}

app/config.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package app
2+
3+
import "github.com/codegangsta/cli"
4+
5+
type Config struct {
6+
SourceURL string
7+
Debug bool
8+
NoRender bool
9+
RenderOnTermUI bool
10+
UpdateCache bool
11+
}
12+
13+
func populateConfig(c *cli.Context) (config Config) {
14+
config.Debug = c.Bool("debug")
15+
config.SourceURL = c.String("source")
16+
config.NoRender = c.Bool("no-render")
17+
config.RenderOnTermUI = c.Bool("termui")
18+
config.UpdateCache = c.Bool("update-cache")
19+
20+
return config
21+
}

app/errors.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package app
2+
3+
import "errors"
4+
5+
var COMMAND_NOT_PROVIDED = errors.New("Command not provided in the command line")
6+
var COMMAND_NOT_FOUND = errors.New("Command not found in the cache")

app/execute.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
"strings"
9+
10+
"github.com/Sirupsen/logrus"
11+
"github.com/codegangsta/cli"
12+
)
13+
14+
func Execute(c *cli.Context) {
15+
config := populateConfig(c)
16+
InitializeLogging(config)
17+
LOG.WithFields(logrus.Fields{
18+
"config": config,
19+
}).Debug("Printing config")
20+
command, err := getCommand()
21+
if err != nil {
22+
fmt.Errorf("Command name is missing in the input")
23+
cli.ShowAppHelp(c)
24+
return
25+
}
26+
27+
if config.UpdateCache || !LocalCacheAvailable() {
28+
if err := UpdateCache(config); err != nil {
29+
log.Fatal(err)
30+
}
31+
}
32+
LOG.WithFields(logrus.Fields{
33+
"command": command,
34+
}).Debug("Printing command")
35+
path, err := getPageLocation(command, "common")
36+
LOG.WithFields(logrus.Fields{
37+
"path": path,
38+
}).Debug("Printing path")
39+
40+
content, err := ioutil.ReadFile(path)
41+
if err != nil {
42+
LOG.Fatal(err)
43+
}
44+
45+
if config.NoRender {
46+
fmt.Println(string(content))
47+
} else {
48+
Render(config, content)
49+
}
50+
}
51+
52+
func getCommand() (string, error) {
53+
if len(os.Args) < 2 {
54+
return "", COMMAND_NOT_PROVIDED
55+
}
56+
command := os.Args[len(os.Args)-1]
57+
if strings.HasPrefix(command, "-") {
58+
return "", COMMAND_NOT_PROVIDED
59+
}
60+
return command, nil
61+
}

app/logging.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package app
2+
3+
import "github.com/Sirupsen/logrus"
4+
5+
var LOG = logrus.New()
6+
7+
func InitializeLogging(config Config) {
8+
if config.Debug {
9+
LOG.Level = logrus.DebugLevel
10+
}
11+
}

app/renderer.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package app
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/Sirupsen/logrus"
7+
)
8+
9+
const WIDTH = 80
10+
11+
func Render(config Config, input []byte) error {
12+
tldr := NewTLDR(input)
13+
LOG.WithFields(logrus.Fields{
14+
"tldr": tldr,
15+
}).Debug("Printing tldr")
16+
17+
if config.RenderOnTermUI {
18+
return RenderOnTermUI(tldr)
19+
} else {
20+
return RenderOnTermbox(tldr)
21+
}
22+
}
23+
24+
func getNumberOfLines(input []byte) int {
25+
lineSep := []byte{'\n'}
26+
count := bytes.Count(input, lineSep)
27+
if count == 0 {
28+
return 1
29+
}
30+
return count
31+
}

0 commit comments

Comments
 (0)