Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add alias and command support #89

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (suite *BuilderTestSuit) TestBuiltinPlugins() {
return plugin.Url == "github.com/golangci/golangci-lint/cmd/golangci-lint"
})
assert.True(suite.T(), ok)
assert.Equal(suite.T(), "v1.55.2", plugin.Version())
assert.Equal(suite.T(), "v1.56.2", plugin.Version())
assert.Equal(suite.T(), "golangci-lint", plugin.Name())
assert.Equal(suite.T(), "github.com/golangci/golangci-lint", plugin.Module())
assert.Equal(suite.T(), "lint", plugin.Alias)
Expand Down
32 changes: 18 additions & 14 deletions cmd/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,46 @@ var (
// parseMod return a tuple which the fourth element is the indicator of direct or indirect reference
func parseMod(mod *os.File) (string, string, []*lo.Tuple4[string, string, string, int], error) {
scanner := bufio.NewScanner(mod)
start := false
var deps []*lo.Tuple4[string, string, string, int]
var module, version string
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line == ")" || line == "//" || strings.HasPrefix(line, "require") {
continue
}
if strings.HasPrefix(line, "module ") {
module = strings.Split(line, " ")[1]
} else if strings.HasPrefix(line, "go ") {
version = strings.Split(line, " ")[1]
}
start = start && line != ")"
if start && len(line) > 0 {
} else {
entry := strings.Split(line, " ")
m := strings.TrimSpace(entry[0])
v := strings.TrimSpace(entry[1])
dep := lo.T4(m, v, v, lo.If(len(entry) > 2, 0).Else(1))
deps = append(deps, &dep)
}
start = start || strings.HasPrefix(line, "require")
}
return module, version, deps, scanner.Err()
}

// dependency build dependency tree of the project, an empty tree returns when runs into error
func dependency() (treeprint.Tree, error) {
tree := treeprint.New()
// dependencyTree build dependency tree of the project, an empty tree returns when runs into error
func dependencyTree() (treeprint.Tree, error) {
mod, err := os.Open(filepath.Join(internal.CurProject().Root(), "go.mod"))
if err != nil {
return tree, fmt.Errorf(color.RedString(err.Error()))
return nil, fmt.Errorf(color.RedString(err.Error()))
}
exec.Command("go", "mod", "tidy").CombinedOutput() //nolint
if _, err = exec.Command("go", "build", "./...").CombinedOutput(); err != nil {
return tree, fmt.Errorf(color.RedString(err.Error()))
if output, err := exec.Command("go", "build", "./...").CombinedOutput(); err != nil {
return nil, fmt.Errorf(color.RedString(string(output)))
}

module, _, dependencies, err := parseMod(mod)
if len(dependencies) < 1 {
return nil, nil
}
if err != nil {
return tree, fmt.Errorf(err.Error())
return nil, fmt.Errorf(err.Error())
}
tree := treeprint.New()
tree.SetValue(bold.Sprintf("%s", module))
direct := lo.FilterMap(dependencies, func(item *lo.Tuple4[string, string, string, int], _ int) (string, bool) {
return fmt.Sprintf("%s@latest", item.A), item.D == 1
Expand Down Expand Up @@ -118,9 +119,12 @@ var depCmd = &cobra.Command{
Long: `Show the dependency tree of the project
and indicate available updates which take an green * indicator`,
RunE: func(cmd *cobra.Command, args []string) error {
tree, err := dependency()
tree, err := dependencyTree()
if err != nil {
return err
} else if tree == nil {
yellow.Println("No dependencies !")
return nil
}
bold.Print("\nDependencies of the projects:\n")
yellow.Print("* indicates new versions available\n")
Expand Down
2 changes: 1 addition & 1 deletion cmd/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDependency(t *testing.T) {
os.Chdir(internal.CurProject().Root())
mod, _ := os.Open(filepath.Join(internal.CurProject().Root(), "go.mod"))
_, _, deps, _ := parseMod(mod)
tree, err := dependency()
tree, err := dependencyTree()
assert.NoError(t, err)
tree.VisitAll(func(item *treeprint.Node) {
contains := lo.ContainsBy(deps, func(dep *lo.Tuple4[string, string, string, int]) bool {
Expand Down
5 changes: 4 additions & 1 deletion cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd
import (
"errors"
"fmt"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/kcmvp/gob/internal"
Expand All @@ -27,6 +28,8 @@ func install(_ *cobra.Command, args ...string) error {
if err != nil {
return err
}
plugin.Alias = alias
plugin.Alias = command
internal.CurProject().SetupPlugin(plugin)
return nil
}
Expand Down Expand Up @@ -79,7 +82,7 @@ you can update the plugin by edit gob.yaml directly
return fmt.Errorf("invalid argument %s", args[0])
}
if "install" == args[0] && (len(args) < 2 || strings.TrimSpace(args[1]) == "") {
return errors.New("miss the plugin url")
return errors.New(color.RedString("miss the plugin url"))
}
return nil
},
Expand Down
5 changes: 5 additions & 0 deletions docs/todo.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Dependency diagram
- https://github.com/ondrajz/go-callvis
- modgraphviz
- https://pkg.go.dev/golang.org/x/exp/cmd/modgraphviz

## d2 support
- Generate call diagram with d2
- generate schema from d2-sqltable
5 changes: 2 additions & 3 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ func (plugin *Plugin) UnmarshalJSON(data []byte) error {
return (*Plugin)(aux.Embedded).init()
}

func NewPlugin(url string, options ...string) (Plugin, error) {
func NewPlugin(url string) (Plugin, error) {
plugin := Plugin{
Url: url,
Args: strings.Join(options, " "),
Url: url,
}
if err := plugin.init(); err != nil {
return Plugin{}, err
Expand Down
10 changes: 5 additions & 5 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ func (suite *InternalPluginTestSuit) TestNewPlugin() {
url: "github.com/golangci/golangci-lint/cmd/golangci-lint",
module: "github.com/golangci/golangci-lint",
logName: "golangci-lint",
binary: "golangci-lint-v1.55.2",
binary: "golangci-lint-v1.56.2",
wantErr: false,
},
{
name: "laatest version",
name: "latest version",
url: "github.com/golangci/golangci-lint/cmd/golangci-lint@latest",
module: "github.com/golangci/golangci-lint",
logName: "golangci-lint",
binary: "golangci-lint-v1.55.2",
binary: "golangci-lint-v1.56.2",
wantErr: false,
},
{
Expand Down Expand Up @@ -102,7 +102,7 @@ func (suite *InternalPluginTestSuit) TestNewPlugin() {
assert.True(t, test.wantErr == (err != nil))
if !test.wantErr {
assert.Equal(t, test.module, plugin.module)
assert.True(t, lo.Contains([]string{"v1.55.2", "v1.1.1", "v1.11.0"}, plugin.Version()))
assert.True(t, lo.Contains([]string{"v1.56.2", "v1.1.1", "v1.11.0"}, plugin.Version()))
}
})
}
Expand All @@ -124,7 +124,7 @@ func (suite *InternalPluginTestSuit) TestUnmarshalJSON() {
return plugin.Url == "github.com/golangci/golangci-lint/cmd/golangci-lint"
})
assert.True(t, ok)
assert.Equal(t, "v1.55.2", plugin.Version())
assert.Equal(t, "v1.56.2", plugin.Version())
assert.Equal(t, "golangci-lint", plugin.Name())
assert.Equal(t, "github.com/golangci/golangci-lint", plugin.Module())
assert.Equal(t, "lint", plugin.Alias)
Expand Down
Loading