1
1
package main
2
2
3
3
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"
13
14
)
14
15
15
16
const joe string = `
@@ -25,134 +26,134 @@ const joe string = `
25
26
▐░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
26
27
▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀
27
28
`
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"
30
31
const dataDir string = ".joe-data"
31
32
32
33
var dataPath = path .Join (os .Getenv ("HOME" ), dataDir )
33
34
34
35
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
+ }
39
40
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
49
50
}
50
51
51
52
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
+ }
56
57
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
+ }
61
62
62
- return availableGitignores , nil
63
+ return availableGitignores , nil
63
64
}
64
65
65
66
func generate (args string ) {
66
- names := strings .Split (args , "," )
67
+ names := strings .Split (args , "," )
67
68
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
+ }
72
73
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
+ }
90
91
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 )
100
101
}
101
102
102
103
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 )
158
159
}
0 commit comments