Skip to content

Commit 201056c

Browse files
committed
update deps + minor formatting improvements, fixes, features
deps --------------- * support golang v1.13 * migrate to Go Modules * replace github.com/codegangsta/cli with github.com/urfave/cli formatting --------------- * reformat using `go fmt` * address `golint` warnings * address `shellcheck` warnings fixes --------------- * update `version` const in `joe.go` to `1.0.3` * add `./build` prefix to executable path in `tool.sh run` features --------------- * set executable path based on host environment in `tool.sh run` * forward remaining arguments to `joe` in `tool.sh run` * redirect output to `stderr` in `usage` function of `tool.sh` fixes karan#91 closes karan#104 closes karan#100
1 parent f5f03b8 commit 201056c

File tree

5 files changed

+181
-126
lines changed

5 files changed

+181
-126
lines changed

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/karan/joe
2+
3+
go 1.13
4+
5+
require (
6+
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae
7+
github.com/urfave/cli v1.22.4
8+
)

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
7+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
9+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
10+
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae h1:vgGSvdW5Lqg+I1aZOlG32uyE6xHpLdKhZzcTEktz5wM=
11+
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae/go.mod h1:quDq6Se6jlGwiIKia/itDZxqC5rj6/8OdFyMMAwTxCs=
12+
github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA=
13+
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

joe.go

Lines changed: 120 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package main
22

33
import (
4-
"fmt"
5-
"github.com/codegangsta/cli"
6-
"io/ioutil"
7-
"log"
8-
"os"
9-
"path"
10-
"path/filepath"
11-
"sort"
12-
"strings"
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
"path"
9+
"path/filepath"
10+
"sort"
11+
"strings"
12+
13+
"github.com/urfave/cli"
1314
)
1415

1516
const joe string = `
@@ -25,134 +26,134 @@ const joe string = `
2526
▐░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
2627
▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀
2728
`
28-
const version string = "1.0.0"
29-
const gitignoreUrl = "https://github.com/github/gitignore/archive/master.zip"
29+
const version string = "1.0.3"
30+
const gitignoreURL = "https://github.com/github/gitignore/archive/master.zip"
3031
const dataDir string = ".joe-data"
3132

3233
var dataPath = path.Join(os.Getenv("HOME"), dataDir)
3334

3435
func findGitignores() (a map[string]string, err error) {
35-
_, err = ioutil.ReadDir(dataPath)
36-
if err != nil {
37-
return nil, err
38-
}
36+
_, err = ioutil.ReadDir(dataPath)
37+
if err != nil {
38+
return nil, err
39+
}
3940

40-
filelist := make(map[string]string)
41-
filepath.Walk(dataPath, func(filepath string, info os.FileInfo, err error) error {
42-
if strings.HasSuffix(info.Name(), ".gitignore") {
43-
name := strings.ToLower(strings.Replace(info.Name(), ".gitignore", "", 1))
44-
filelist[name] = filepath
45-
}
46-
return nil
47-
})
48-
return filelist, nil
41+
filelist := make(map[string]string)
42+
filepath.Walk(dataPath, func(filepath string, info os.FileInfo, err error) error {
43+
if strings.HasSuffix(info.Name(), ".gitignore") {
44+
name := strings.ToLower(strings.Replace(info.Name(), ".gitignore", "", 1))
45+
filelist[name] = filepath
46+
}
47+
return nil
48+
})
49+
return filelist, nil
4950
}
5051

5152
func availableFiles() (a []string, err error) {
52-
gitignores, err := findGitignores()
53-
if err != nil {
54-
return nil, err
55-
}
53+
gitignores, err := findGitignores()
54+
if err != nil {
55+
return nil, err
56+
}
5657

57-
availableGitignores := []string{}
58-
for key, _ := range gitignores {
59-
availableGitignores = append(availableGitignores, key)
60-
}
58+
availableGitignores := []string{}
59+
for key := range gitignores {
60+
availableGitignores = append(availableGitignores, key)
61+
}
6162

62-
return availableGitignores, nil
63+
return availableGitignores, nil
6364
}
6465

6566
func generate(args string) {
66-
names := strings.Split(args, ",")
67+
names := strings.Split(args, ",")
6768

68-
gitignores, err := findGitignores()
69-
if err != nil {
70-
log.Fatal(err)
71-
}
69+
gitignores, err := findGitignores()
70+
if err != nil {
71+
log.Fatal(err)
72+
}
7273

73-
notFound := []string{}
74-
output := ""
75-
for index, name := range names {
76-
if filepath, ok := gitignores[strings.ToLower(name)]; ok {
77-
bytes, err := ioutil.ReadFile(filepath)
78-
if err == nil {
79-
output += "\n#### " + name + " ####\n"
80-
output += string(bytes)
81-
if index < len(names) - 1 {
82-
output += "\n"
83-
}
84-
continue
85-
}
86-
} else {
87-
notFound = append(notFound, name)
88-
}
89-
}
74+
notFound := []string{}
75+
output := ""
76+
for index, name := range names {
77+
if filepath, ok := gitignores[strings.ToLower(name)]; ok {
78+
bytes, err := ioutil.ReadFile(filepath)
79+
if err == nil {
80+
output += "\n#### " + name + " ####\n"
81+
output += string(bytes)
82+
if index < len(names)-1 {
83+
output += "\n"
84+
}
85+
continue
86+
}
87+
} else {
88+
notFound = append(notFound, name)
89+
}
90+
}
9091

91-
if len(notFound) > 0 {
92-
fmt.Printf("Unsupported files: %s\n", strings.Join(notFound, ", "))
93-
fmt.Println("Run `joe ls` to see list of available gitignores.")
94-
output = ""
95-
}
96-
if len(output) > 0 {
97-
output = "#### joe made this: http://goel.io/joe\n" + output
98-
}
99-
fmt.Print(output)
92+
if len(notFound) > 0 {
93+
fmt.Printf("Unsupported files: %s\n", strings.Join(notFound, ", "))
94+
fmt.Println("Run `joe ls` to see list of available gitignores.")
95+
output = ""
96+
}
97+
if len(output) > 0 {
98+
output = "#### joe made this: http://goel.io/joe\n" + output
99+
}
100+
fmt.Print(output)
100101
}
101102

102103
func main() {
103-
app := cli.NewApp()
104-
app.Name = joe
105-
app.Usage = "generate .gitignore files from the command line"
106-
app.UsageText = "joe command [arguments...]"
107-
app.Version = version
108-
app.Commands = []cli.Command{
109-
{
110-
Name: "ls",
111-
Aliases: []string{"list"},
112-
Usage: "list all available files",
113-
Action: func(c *cli.Context) error {
114-
availableGitignores, err := availableFiles()
115-
if err != nil {
116-
log.Fatal(err)
117-
return err
118-
}
119-
fmt.Printf("%d supported .gitignore files:\n", len(availableGitignores))
120-
sort.Strings(availableGitignores)
121-
fmt.Printf("%s\n", strings.Join(availableGitignores, ", "))
122-
return nil
123-
},
124-
},
125-
{
126-
Name: "u",
127-
Aliases: []string{"update"},
128-
Usage: "update all available gitignore files",
129-
Action: func(c *cli.Context) error {
130-
fmt.Println("Updating gitignore files..")
131-
err := RemoveContents(dataPath)
132-
if err != nil {
133-
log.Fatal(err)
134-
}
135-
err = DownloadFiles(gitignoreUrl, dataPath)
136-
if err != nil {
137-
log.Fatal(err)
138-
return err
139-
}
140-
return nil
141-
},
142-
},
143-
{
144-
Name: "g",
145-
Aliases: []string{"generate"},
146-
Usage: "generate gitignore files",
147-
Action: func(c *cli.Context) error {
148-
if c.NArg() != 1 {
149-
cli.ShowAppHelp(c)
150-
} else {
151-
generate(c.Args()[0])
152-
}
153-
return nil
154-
},
155-
},
156-
}
157-
app.Run(os.Args)
104+
app := cli.NewApp()
105+
app.Name = joe
106+
app.Usage = "generate .gitignore files from the command line"
107+
app.UsageText = "joe command [arguments...]"
108+
app.Version = version
109+
app.Commands = []cli.Command{
110+
{
111+
Name: "ls",
112+
Aliases: []string{"list"},
113+
Usage: "list all available files",
114+
Action: func(c *cli.Context) error {
115+
availableGitignores, err := availableFiles()
116+
if err != nil {
117+
log.Fatal(err)
118+
return err
119+
}
120+
fmt.Printf("%d supported .gitignore files:\n", len(availableGitignores))
121+
sort.Strings(availableGitignores)
122+
fmt.Printf("%s\n", strings.Join(availableGitignores, ", "))
123+
return nil
124+
},
125+
},
126+
{
127+
Name: "u",
128+
Aliases: []string{"update"},
129+
Usage: "update all available gitignore files",
130+
Action: func(c *cli.Context) error {
131+
fmt.Println("Updating gitignore files..")
132+
err := RemoveContents(dataPath)
133+
if err != nil {
134+
log.Fatal(err)
135+
}
136+
err = DownloadFiles(gitignoreURL, dataPath)
137+
if err != nil {
138+
log.Fatal(err)
139+
return err
140+
}
141+
return nil
142+
},
143+
},
144+
{
145+
Name: "g",
146+
Aliases: []string{"generate"},
147+
Usage: "generate gitignore files",
148+
Action: func(c *cli.Context) error {
149+
if c.NArg() != 1 {
150+
cli.ShowAppHelp(c)
151+
} else {
152+
generate(c.Args()[0])
153+
}
154+
return nil
155+
},
156+
},
157+
}
158+
app.Run(os.Args)
158159
}

tool.sh

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33

44
function usage {
5-
local tool=$(basename $0)
6-
cat <<EOF
5+
local tool
6+
tool=$(basename "$0")
7+
8+
cat <<EOF >/dev/stderr
79
810
USAGE:
911
$ $tool [-h|--help] COMMAND
@@ -28,8 +30,8 @@ function build {
2830
}
2931

3032

31-
# total arguments should be 1
32-
if [ $# -ne 1 ]; then
33+
# allow maximum of 1 argument for all subcommands except run
34+
if [ $# -ne 1 ] && [ "$1" != "run" ]; then
3335
usage;
3436
fi
3537

@@ -41,12 +43,38 @@ fi
4143

4244
# show help for no arguments if stdin is a terminal
4345
if [ "$1" == "deps" ]; then
44-
go get github.com/codegangsta/cli
46+
go get github.com/urfave/cli
4547
go get github.com/termie/go-shutil
4648
elif [ "$1" == "build" ]; then
4749
build
4850
elif [ "$1" == "run" ]; then
49-
build && ./joe
51+
# default to linux-amd64
52+
joe_path=joe
53+
target="$(go env GOOS)-$(go env GOARCH)"
54+
55+
# select executable targeted for current host environment
56+
case "${target}" in
57+
windows-386)
58+
joe_path=joe-x86.exe
59+
;;
60+
windows-amd64)
61+
joe_path=joe.exe
62+
;;
63+
linux-386)
64+
joe_path=joe-x86
65+
;;
66+
linux-amd64)
67+
joe_path=joe
68+
;;
69+
darwin-386)
70+
joe_path=joe-darwin-x86
71+
;;
72+
darwin-amd64)
73+
joe_path=joe-darwin
74+
;;
75+
esac
76+
77+
build && ./build/${joe_path} "${@:2}"
5078
else
5179
usage;
5280
fi

utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package main
22

33
import (
44
"archive/zip"
5-
"github.com/termie/go-shutil"
65
"io"
76
"net/http"
87
"os"
98
"path"
109
"path/filepath"
10+
11+
"github.com/termie/go-shutil"
1112
)
1213

1314
func unzip(archive, target string) (err error) {
@@ -47,6 +48,7 @@ func unzip(archive, target string) (err error) {
4748
return nil
4849
}
4950

51+
// DownloadFiles downloads contents of url to a temporary directory within dataPath
5052
func DownloadFiles(url string, dataPath string) (err error) {
5153
archivePath := path.Join("/tmp", "master.zip")
5254

@@ -84,6 +86,7 @@ func DownloadFiles(url string, dataPath string) (err error) {
8486
return nil
8587
}
8688

89+
// RemoveContents deletes dir and it's contents
8790
func RemoveContents(dir string) (err error) {
8891
if _, err := os.Stat(dir); os.IsNotExist(err) {
8992
return nil

0 commit comments

Comments
 (0)