-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 31f9e88
Showing
8 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Junpei Kawamoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# | ||
# Makefile | ||
# | ||
# Copyright (c) 2016 Junpei Kawamoto | ||
# | ||
# This software is released under the MIT License. | ||
# | ||
# http://opensource.org/licenses/mit-license.php | ||
# | ||
VERSION = snapshot | ||
|
||
default: build | ||
|
||
.PHONY: build | ||
build: | ||
goxc -d=pkg -pv=$(VERSION) | ||
|
||
.PHONY: release | ||
release: | ||
ghr -u jkawamoto v$(VERSION) pkg/$(VERSION) | ||
|
||
.PHONY: get-deps | ||
get-deps: | ||
go get -u github.com/jteeuwen/go-bindata/... | ||
go get -d -t -v . | ||
|
||
.PHONY: test | ||
test: asset | ||
go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# gii | ||
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) | ||
|
||
Set repositories which doesn't belong golang project to .goimportsignore. | ||
|
||
|
||
|
||
## Usage | ||
~~~ | ||
gii [global options] | ||
GLOBAL OPTIONS: | ||
--gopath GOPATH GOPATH [$GOPATH] | ||
--help, -h show help | ||
--version, -v print the version | ||
~~~ | ||
|
||
License | ||
======= | ||
This software is released under the MIT License, see [LICENSE](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// CmdRun parses options and run cmdRun. | ||
func CmdRun(c *cli.Context) error { | ||
|
||
if err := cmdRun(c.GlobalString("gopath")); err != nil { | ||
return cli.NewExitError(err.Error(), 1) | ||
} | ||
return nil | ||
|
||
} | ||
|
||
func cmdRun(gopath string) (err error) { | ||
|
||
root := filepath.Join(gopath, "src") | ||
serviceInfos, err := ioutil.ReadDir(root) | ||
if err != nil { | ||
return | ||
} | ||
|
||
ignoreCh := make(chan string) | ||
pathCh := make(chan string) | ||
done := make(chan interface{}) | ||
|
||
go SearchParallel(pathCh, ignoreCh, done) | ||
for _, info := range serviceInfos { | ||
if info.IsDir() { | ||
pathCh <- filepath.Join(root, info.Name()) | ||
} | ||
} | ||
close(pathCh) | ||
|
||
goimportsignore := filepath.Join(root, ".goimportsignore") | ||
defined, err := LoadIgnoredPaths(goimportsignore) | ||
if err != nil { | ||
return | ||
} | ||
|
||
fp, err := os.OpenFile(goimportsignore, os.O_WRONLY|os.O_APPEND, 0644) | ||
if err != nil { | ||
return | ||
} | ||
defer fp.Close() | ||
|
||
for { | ||
select { | ||
case path := <-ignoreCh: | ||
if _, exist := defined[path]; !exist { | ||
fmt.Fprintln(fp, path) | ||
fmt.Printf("Append %s\n", path) | ||
} | ||
case <-done: | ||
return nil | ||
} | ||
} | ||
|
||
} | ||
|
||
// SearchParallel is a go-routine searching repositories which doesn't belong to | ||
// golang project. It reads search roots from input channel, and write found | ||
// reporitories to output channel. After putting all search roots to input, | ||
// users must close the input channel. When all search finish, the notification | ||
// will be put in done channel. | ||
func SearchParallel(input <-chan string, output chan<- string, done chan<- interface{}) { | ||
|
||
var wg sync.WaitGroup | ||
for { | ||
path, ok := <-input | ||
if !ok { | ||
break | ||
} | ||
|
||
go func() { | ||
wg.Add(1) | ||
defer wg.Done() | ||
searchDir(path, output) | ||
}() | ||
} | ||
wg.Wait() | ||
done <- struct{}{} | ||
|
||
} | ||
|
||
func searchDir(path string, ch chan<- string) { | ||
|
||
infos, err := ioutil.ReadDir(path) | ||
if err != nil { | ||
return | ||
} | ||
|
||
isRepository := false | ||
isGoRepository := false | ||
for _, info := range infos { | ||
|
||
if info.IsDir() { | ||
|
||
if info.Name() == ".git" { | ||
isRepository = true | ||
} else if !strings.HasPrefix(".", info.Name()) { | ||
|
||
if isRepository { | ||
// checking .go folder. | ||
if searchGoSource(filepath.Join(path, info.Name())) { | ||
isGoRepository = true | ||
break | ||
} | ||
} else { | ||
// recursive call | ||
searchDir(filepath.Join(path, info.Name()), ch) | ||
} | ||
} | ||
|
||
} else if strings.HasSuffix(info.Name(), ".go") { | ||
isGoRepository = true | ||
} | ||
|
||
} | ||
|
||
if isRepository && !isGoRepository { | ||
ch <- path | ||
} | ||
|
||
} | ||
|
||
func searchGoSource(path string) bool { | ||
|
||
infos, err := ioutil.ReadDir(path) | ||
if err != nil { | ||
fmt.Printf("Skip %s (%s)\n", path, err.Error()) | ||
return false | ||
} | ||
|
||
dirs := map[string]bool{} | ||
for _, info := range infos { | ||
|
||
if info.IsDir() { | ||
dirs[info.Name()] = true | ||
} else { | ||
if strings.HasSuffix(info.Name(), ".go") { | ||
return true | ||
} | ||
} | ||
|
||
} | ||
|
||
for sub := range dirs { | ||
if searchGoSource(filepath.Join(path, sub)) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
|
||
} | ||
|
||
// LoadIgnoredPaths reads a .goimportsignore file and returns a set of paths | ||
// included in that file. | ||
func LoadIgnoredPaths(path string) (res map[string]interface{}, err error) { | ||
|
||
raw, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return | ||
} | ||
|
||
res = map[string]interface{}{} | ||
for _, path := range strings.Split(string(raw), "\n") { | ||
res[path] = struct{}{} | ||
} | ||
return | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// GlobalFlags defines global flags. | ||
var GlobalFlags = []cli.Flag{ | ||
cli.StringFlag{ | ||
EnvVar: "GOPATH", | ||
Name: "gopath", | ||
Usage: "`GOPATH`", | ||
}, | ||
} | ||
|
||
// Commands defines sub commands. | ||
var Commands = []cli.Command{} | ||
|
||
// CommandNotFound defines an action when a given command won't be supported. | ||
func CommandNotFound(c *cli.Context, command string) { | ||
fmt.Fprintf( | ||
os.Stderr, "%s: '%s' is not a %s command. See '%s --help'.", | ||
c.App.Name, command, c.App.Name, c.App.Name) | ||
os.Exit(2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/jkawamoto/gii/command" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func main() { | ||
|
||
app := cli.NewApp() | ||
app.Name = Name | ||
app.Version = Version | ||
app.Author = "Junpei Kawamoto" | ||
app.Email = "kawamoto.junpei@gmail.com" | ||
app.Usage = "set repositories which doesn't belong golang project to .goimportsignore." | ||
|
||
app.Flags = GlobalFlags | ||
app.Action = command.CmdRun | ||
app.Commands = Commands | ||
app.CommandNotFound = CommandNotFound | ||
app.EnableBashCompletion = true | ||
app.Copyright = `Copyright (C) 2016 Junpei Kawamoto | ||
This software is released under the MIT License.` | ||
|
||
app.Run(os.Args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package main | ||
|
||
const ( | ||
// Name defines the name of this command. | ||
Name string = "gii" | ||
// Version defines version number. | ||
Version string = "0.1.0" | ||
) |