Skip to content

Commit

Permalink
cli: add path and exclude flags
Browse files Browse the repository at this point in the history
  • Loading branch information
z7zmey committed Oct 15, 2017
1 parent 615bba2 commit 2b1a628
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 25 deletions.
51 changes: 51 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright © 2017 Slizov Vadim <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
"flag"
"strings"
)

type ArrayFlags []string

func (i *ArrayFlags) String() string {
return strings.Join(*i, " ")
}

func (i *ArrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}

type CGConfig struct {
path ArrayFlags
exclude ArrayFlags
}

var Config = CGConfig{}

func ParseConfigFlags() {
flag.Var(&Config.path, "path", "path to sources")
flag.Var(&Config.path, "p", "path to sources (shorthand)")

flag.Var(&Config.exclude, "exclude", "exclude path")
flag.Var(&Config.exclude, "e", "exclude path (shorthand)")

flag.Parse()
}
10 changes: 3 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"fmt"
"log"
"os"
"os/signal"
"runtime"
Expand All @@ -31,6 +30,8 @@ import (
var store *cayley.Handle

func main() {
ParseConfigFlags()

// разобраться с ошибкой fatal error: concurrent map writes
runtime.GOMAXPROCS(1)

Expand All @@ -46,13 +47,8 @@ func main() {
go ListenAndServeAPI()
go ListenAndServeSocket()

dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

// TODO: get a dir from cli
ProcessPath(dir)
ProcessPath()

<-sigs
fmt.Println()
Expand Down
62 changes: 44 additions & 18 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"sync"

"github.com/cayleygraph/cayley"
Expand All @@ -31,10 +32,22 @@ import (
"github.com/cayleygraph/cayley/schema"
)

func ProcessPath(dir string) {
func ProcessPath() {
var err error
var dir = Config.path

if len(dir) == 0 {
path, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

dir = ArrayFlags{path}
}

codeGraphDir, err := filepath.Abs(filepath.Dir(os.Args[0]))

if _, err := os.Stat(codeGraphDir+"/php-worker"); os.IsNotExist(err) {
if _, err := os.Stat(codeGraphDir + "/php-worker"); os.IsNotExist(err) {
codeGraphDir = "/usr/local/lib/codegraph"
}

Expand All @@ -50,15 +63,17 @@ func ProcessPath(dir string) {
go processFileAst(codeGraphDir, astFileChan, &wg)
go processFileAst(codeGraphDir, astFileChan, &wg)

err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(path) == ".php" {
wg.Add(1)
astFileChan <- path
for _, path := range dir {
err = filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(path) == ".php" && !inExclude(path) {
wg.Add(1)
astFileChan <- path
}
return nil
})
if err != nil {
log.Fatal(err)
}
return nil
})
if err != nil {
log.Fatal(err)
}

wg.Wait()
Expand All @@ -70,15 +85,16 @@ func ProcessPath(dir string) {
go processFileCfg(codeGraphDir, cfgFileChan)
go processFileCfg(codeGraphDir, cfgFileChan)
go processFileCfg(codeGraphDir, cfgFileChan)

err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(path) == ".php" {
cfgFileChan <- path
for _, path := range dir {
err = filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(path) == ".php" && !inExclude(path) {
cfgFileChan <- path
}
return nil
})
if err != nil {
log.Fatal(err)
}
return nil
})
if err != nil {
log.Fatal(err)
}
}

Expand Down Expand Up @@ -145,3 +161,13 @@ func setMethodImplementations(method AstMethod) {
err = qw.Close()
checkErr(err)
}

func inExclude(path string) bool {
for _, exclude := range Config.exclude {
if strings.HasPrefix(path, exclude) {
return true
}
}

return false
}

0 comments on commit 2b1a628

Please sign in to comment.