Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
elza2 authored and yuanyou committed Nov 29, 2022
1 parent d4470a1 commit 4c6ca8d
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 47 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE
.idea/
.vscode/
*.swp

# Go
go.work
go.work.sum

# project
*.cert
*.key
*.log
bin/
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
===============
```bash
go install github.com/elza2/go-cyclic@latest
# path 路径要设置为 go.mod 文件所在的全路径.
# path 路径要设置为 go.mod 文件所在的路径.
go-cyclic gocyclic --dir .path
```

运行测试
===============
```bash
git
go run ./cmd/main.go gocyclic --dir .path
git clone https://github.com/elza2/go-cyclic.git
# path 路径要设置为 go.mod 文件所在的路径.
go run ./main.go gocyclic --dir .path
```

运行结果
===============
```bash
# success output.
Success. Not circular dependence.

# failed output.
┌---→ daji.go
Failed. 1 circular dependence chains were found.

┌---→ app.go
┆ ↓
┆ routes.go
┆ ↓
Expand Down
10 changes: 6 additions & 4 deletions cmd/actions/cmd.go → cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package actions
package cmd

import (
"github.com/spf13/cobra"
"go-cyclic/tool"
"log"

"github.com/spf13/cobra"

"github.com/elza2/go-cyclic/tool"
)

func RunCyclic(cmd *cobra.Command, args []string) {
Expand All @@ -12,7 +14,7 @@ func RunCyclic(cmd *cobra.Command, args []string) {
log.Fatalf("get dir params failed: %v\n", err)
}
if err = tool.CheckCycleDepend(dir); err != nil {
log.Fatalf("run failed: %v\n", err)
log.Fatalf("run failed. %v\n", err)
}
}

Expand Down
9 changes: 0 additions & 9 deletions cmd/main.go

This file was deleted.

7 changes: 4 additions & 3 deletions cmd/actions/root.go → cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package actions
package cmd

import (
"github.com/spf13/cobra"
"log"
"os"

"github.com/spf13/cobra"
)

const Service = "go-cyclic"
const Service = "github.com/elza2/go-cyclic"

var (
rootCmd = &cobra.Command{Use: Service}
Expand Down
24 changes: 19 additions & 5 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package errors

import "errors"

var (
GoModNotExist = errors.New("not find go.mod file")
GoModParseFailed = errors.New("go.mod file parse failed")
import (
"errors"
"fmt"
)

func PathNotExist(filepath string) error {
return errors.New(fmt.Sprintf("path: %v, not exist.", filepath))
}

func PathNotIsFile(filepath string) error {
return errors.New(fmt.Sprintf("path: %v, cannot is a file.", filepath))
}

func GoModNotExist(filepath string) error {
return errors.New(fmt.Sprintf("path: %v, not find go.mod file.", filepath))
}

func GoModParseFailed(filepath string) error {
return errors.New(fmt.Sprintf("path: %v/go.mod, go.mod file parse failed.", filepath))
}
3 changes: 2 additions & 1 deletion example_cyclic_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"go-cyclic/tool"
"testing"

"github.com/elza2/go-cyclic/tool"
)

func TestCyclic(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module go-cyclic
module github.com/elza2/go-cyclic

go 1.18

Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/elza2/go-cyclic/cmd"

func main() {
cmd.Execute()
}
36 changes: 24 additions & 12 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package resolver

import (
"fmt"
"go-cyclic/errors"
"go-cyclic/sprite"
"go/parser"
"go/token"
"golang.org/x/mod/modfile"
"io/ioutil"
"os"
"strings"

"golang.org/x/mod/modfile"

"github.com/elza2/go-cyclic/errors"
"github.com/elza2/go-cyclic/sprite"
)

var (
Expand All @@ -18,29 +20,39 @@ var (
)

// ParseDir parse path.
func ParseDir(dir string) string {
func ParseDir(dir string) (path string, err error) {
stat, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
return "", errors.PathNotExist(dir)
}
return "", err
}
if !stat.IsDir() {
return "", errors.PathNotIsFile(dir)
}
idx := strings.LastIndex(dir, "/")
if idx == -1 {
return dir
return dir, nil
}
return dir[idx+1:]
return dir[idx+1:], nil
}

func ParseGoModule(dir string) (moduleName string, err error) {
readFile, err := ioutil.ReadFile(dir + "/" + GoMod)
func ParseGoModule(dir string) (module string, err error) {
readFile, err := os.ReadFile(dir + "/" + GoMod)
if err != nil {
return "", errors.GoModNotExist
return "", errors.GoModNotExist(dir)
}
modFile, err := modfile.Parse(GoMod, readFile, nil)
if err != nil {
return "", errors.GoModParseFailed
return "", errors.GoModParseFailed(dir)
}
return modFile.Module.Mod.Path, nil
}

func ParseNodeSprite(root string, module string, dir string) (nodes []*sprite.NodeSprite, err error) {
nodeSprites := make([]*sprite.NodeSprite, 0)
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
Expand Down
20 changes: 14 additions & 6 deletions tool/tool.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package tool

import (
"go-cyclic/resolver"
"go-cyclic/sprite"
"go-cyclic/topology"
"github.com/elza2/go-cyclic/resolver"
"github.com/elza2/go-cyclic/sprite"
"github.com/elza2/go-cyclic/topology"
"path/filepath"
)

func CheckCycleDepend(dir string) error {
abs, err := filepath.Abs(dir)
if err != nil {
return err
}
// parse root path.
root := resolver.ParseDir(dir)
root, err := resolver.ParseDir(abs)
if err != nil {
return err
}
// parse module name.
module, err := resolver.ParseGoModule(dir)
module, err := resolver.ParseGoModule(abs)
if err != nil {
return err
}
// parse sprite nodes by path.
sprites, err := resolver.ParseNodeSprite(root, module, dir)
sprites, err := resolver.ParseNodeSprite(root, module, abs)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package topology

import (
"fmt"
"github.com/fatih/color"
"go-cyclic/sprite"
"strings"

"github.com/elza2/go-cyclic/sprite"
"github.com/fatih/color"
)

var (
Expand Down

0 comments on commit 4c6ca8d

Please sign in to comment.