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
+ "github.com/codegangsta/cli"
6
+ "io/ioutil"
7
+ "log"
8
+ "os"
9
+ "path"
10
+ "path/filepath"
11
+ "sort"
12
+ "strings"
13
13
)
14
14
15
15
const joe string = `
@@ -32,119 +32,122 @@ const dataDir string = ".joe-data"
32
32
var dataPath = path .Join (os .Getenv ("HOME" ), dataDir )
33
33
34
34
func findGitignores () (a map [string ]string , err error ) {
35
- _ , err = ioutil .ReadDir (dataPath )
36
- if err != nil {
37
- return nil , err
38
- }
35
+ _ , err = ioutil .ReadDir (dataPath )
36
+ if err != nil {
37
+ return nil , err
38
+ }
39
39
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
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
49
49
}
50
50
51
51
func availableFiles () (a []string , err error ) {
52
- gitignores , err := findGitignores ()
53
- if err != nil {
54
- return nil , err
55
- }
52
+ gitignores , err := findGitignores ()
53
+ if err != nil {
54
+ return nil , err
55
+ }
56
56
57
- availableGitignores := []string {}
58
- for key , _ := range gitignores {
59
- availableGitignores = append (availableGitignores , key )
60
- }
57
+ availableGitignores := []string {}
58
+ for key , _ := range gitignores {
59
+ availableGitignores = append (availableGitignores , key )
60
+ }
61
61
62
- return availableGitignores , nil
62
+ return availableGitignores , nil
63
63
}
64
64
65
65
func generate (args string ) {
66
- names := strings .Split (args , "," )
66
+ names := strings .Split (args , "," )
67
67
68
- gitignores , err := findGitignores ()
69
- if err != nil {
70
- log .Fatal (err )
71
- }
68
+ gitignores , err := findGitignores ()
69
+ if err != nil {
70
+ log .Fatal (err )
71
+ }
72
72
73
- notFound := []string {}
74
- output := ""
75
- for _ , name := range names {
76
- if filepath , ok := gitignores [strings .ToLower (name )]; ok {
77
- bytes , err := ioutil .ReadFile (filepath )
78
- if err == nil {
79
- output += "#### " + name + " ####\n "
80
- output += string (bytes )
81
- continue
82
- }
83
- } else {
84
- notFound = append (notFound , name )
85
- }
86
- }
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
+ }
87
90
88
- if len (notFound ) > 0 {
89
- fmt .Printf ("Unsupported files: %s\n " , strings .Join (notFound , ", " ))
90
- fmt .Println ("Run `joe ls` to see list of available gitignores." )
91
- output = ""
92
- }
93
- if len (output ) > 0 {
94
- output = "#### joe made this: http://goel.io/joe\n " + output
95
- }
96
- fmt .Println (output )
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 )
97
100
}
98
101
99
102
func main () {
100
- app := cli .NewApp ()
101
- app .Name = joe
102
- app .Usage = "generate .gitignore files from the command line"
103
- app .UsageText = "joe command [arguments...]"
104
- app .Version = version
105
- app .Commands = []cli.Command {
106
- {
107
- Name : "ls" ,
108
- Aliases : []string {"list" },
109
- Usage : "list all available files" ,
110
- Action : func (c * cli.Context ) {
111
- availableGitignores , err := availableFiles ()
112
- if err != nil {
113
- log .Fatal (err )
114
- }
115
- fmt .Printf ("%d supported .gitignore files:\n " , len (availableGitignores ))
116
- sort .Strings (availableGitignores )
117
- fmt .Printf ("%s\n " , strings .Join (availableGitignores , ", " ))
118
- },
119
- },
120
- {
121
- Name : "u" ,
122
- Aliases : []string {"update" },
123
- Usage : "update all available gitignore files" ,
124
- Action : func (c * cli.Context ) {
125
- fmt .Println ("Updating gitignore files.." )
126
- err := RemoveContents (dataPath )
127
- if err != nil {
128
- log .Fatal (err )
129
- }
130
- err = DownloadFiles (gitignoreUrl , dataPath )
131
- if err != nil {
132
- log .Fatal (err )
133
- }
134
- },
135
- },
136
- {
137
- Name : "g" ,
138
- Aliases : []string {"generate" },
139
- Usage : "generate gitignore files" ,
140
- Action : func (c * cli.Context ) {
141
- if c .NArg () != 1 {
142
- cli .ShowAppHelp (c )
143
- } else {
144
- generate (c .Args ()[0 ])
145
- }
146
- },
147
- },
148
- }
149
- app .Run (os .Args )
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 ) {
114
+ availableGitignores , err := availableFiles ()
115
+ if err != nil {
116
+ log .Fatal (err )
117
+ }
118
+ fmt .Printf ("%d supported .gitignore files:\n " , len (availableGitignores ))
119
+ sort .Strings (availableGitignores )
120
+ fmt .Printf ("%s\n " , strings .Join (availableGitignores , ", " ))
121
+ },
122
+ },
123
+ {
124
+ Name : "u" ,
125
+ Aliases : []string {"update" },
126
+ Usage : "update all available gitignore files" ,
127
+ Action : func (c * cli.Context ) {
128
+ fmt .Println ("Updating gitignore files.." )
129
+ err := RemoveContents (dataPath )
130
+ if err != nil {
131
+ log .Fatal (err )
132
+ }
133
+ err = DownloadFiles (gitignoreUrl , dataPath )
134
+ if err != nil {
135
+ log .Fatal (err )
136
+ }
137
+ },
138
+ },
139
+ {
140
+ Name : "g" ,
141
+ Aliases : []string {"generate" },
142
+ Usage : "generate gitignore files" ,
143
+ Action : func (c * cli.Context ) {
144
+ if c .NArg () != 1 {
145
+ cli .ShowAppHelp (c )
146
+ } else {
147
+ generate (c .Args ()[0 ])
148
+ }
149
+ },
150
+ },
151
+ }
152
+ app .Run (os .Args )
150
153
}
0 commit comments