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

#5:init release #7

Merged
merged 1 commit into from
Dec 16, 2023
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
29 changes: 26 additions & 3 deletions cmd/util.go → cmd/action/action.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
package cmd
package action

import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/kcmvp/gob/internal"
"github.com/samber/lo"
"github.com/spf13/cobra"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)

func streamOutput(cmd *exec.Cmd, file string, errWords ...string) error {
type Execution func(cmd *cobra.Command, args ...string) error

type CmdAction lo.Tuple2[string, Execution]

func PrintCmd(cmd *cobra.Command, msg string) error {
if ok, file := internal.TestEnv(); ok {
// Get the call stack
outputFile, err := os.Create(filepath.Join(internal.CurProject().Target(), file))
if err != nil {
return err
}
defer outputFile.Close()
writer := io.MultiWriter(os.Stdout, outputFile)
fmt.Fprintln(writer, msg)
} else {
cmd.Println(msg)
}
return nil
}

func StreamExtCmdOutput(cmd *exec.Cmd, file string, errWords ...string) error {
// Create a pipe to capture the command's combined output
stdout, err := cmd.StdoutPipe()
if err != nil {
Expand All @@ -35,7 +58,7 @@ func streamOutput(cmd *exec.Cmd, file string, errWords ...string) error {
if lo.SomeBy(errWords, func(item string) bool {
return strings.Contains(line, item)
}) {
internal.Red.Fprintln(os.Stdout, line)
color.Red(line)
fmt.Fprintln(outputFile, line)
} else {
fmt.Fprintln(writer, line)
Expand Down
5 changes: 0 additions & 5 deletions cmd/common/cmd.go

This file was deleted.

28 changes: 0 additions & 28 deletions cmd/gen.go

This file was deleted.

58 changes: 0 additions & 58 deletions cmd/git/git_hook.go

This file was deleted.

10 changes: 0 additions & 10 deletions cmd/git/git_hook_test.go

This file was deleted.

50 changes: 50 additions & 0 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"github.com/fatih/color"
"github.com/kcmvp/gob/cmd/plugin"
"github.com/spf13/cobra"
)

// pluginCmd represents the plugin command
var pluginCmd = &cobra.Command{
Use: "plugin",
Short: "List all configured plugins",
Long: `List all configured plugins`,
RunE: func(cmd *cobra.Command, args []string) error {
// run 'gob plugin' will list all the configured plugins
// run 'gob plugin -u' will list all the configured plugins and install the uninstalled tools.
return plugin.List(cmd)
},
}

// installPluginCmd represents the plugin install command
var installPluginCmd = &cobra.Command{
Use: "install",
Short: "Install a tool as gob plugin",
Long: `Install a tool as gob plugin`,
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return fmt.Errorf(color.RedString(err.Error()))
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return plugin.Install(cmd, args...)
},
}

func init() {
// init pluginCmd
rootCmd.AddCommand(pluginCmd)
pluginCmd.Flags().BoolVarP(&plugin.UpdateList, "update", "u", false, "update configured plugins")

// init installPluginCmd
pluginCmd.AddCommand(installPluginCmd)
installPluginCmd.Flags().StringVarP(&plugin.ToolAlias, "alias", "a", "", "alias of the tool")
installPluginCmd.Flags().StringVarP(&plugin.ToolCommand, "command", "c", "", "default command of this tool")
}
68 changes: 68 additions & 0 deletions cmd/plugin/plugin_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package plugin

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/cmd/action"
"github.com/kcmvp/gob/internal"
"github.com/samber/lo"
"github.com/spf13/cobra"
"os"
"path/filepath"
"strings"
)

// ToolAlias is the tool alias, for the convenience of run 'gob alias'
var ToolAlias string

// ToolCommand is the tool command, it's the default command when run 'gob alias'
var ToolCommand string

// Install the specified tool as gob plugin
var Install action.Execution = func(cmd *cobra.Command, args ...string) error {
if strings.HasSuffix(args[0], "@master") || strings.HasSuffix(args[0], "@latest") {
return fmt.Errorf("please use specific version instead of 'master' or 'latest'")
}
err := internal.CurProject().InstallPlugin(args[0], ToolAlias, ToolCommand)
if errors.Is(err, internal.PluginExists) {
color.Yellow("Plugin %s exists", args[0])
err = nil
}
return err
}

var UpdateList bool
var List action.Execution = func(cmd *cobra.Command, _ ...string) error {
plugins := internal.CurProject().Plugins()
ct := table.Table{}
ct.SetTitle("Installed Plugins")
ct.AppendRow(table.Row{"Command", "Alias", "Method", "URL"})
style := table.StyleDefault
style.Options.DrawBorder = true
style.Options.SeparateRows = true
style.Options.SeparateColumns = true
style.Title.Align = text.AlignCenter
style.HTML.CSSClass = table.DefaultHTMLCSSClass
ct.SetStyle(style)
rows := lo.Map(plugins, func(item lo.Tuple4[string, string, string, string], index int) table.Row {
return table.Row{item.A, item.B, item.C, item.D}
})
ct.AppendRows(rows)
action.PrintCmd(cmd, ct.Render())
if UpdateList {
for _, plugin := range plugins {
_, name := internal.NormalizePlugin(plugin.D)
if _, err := os.Stat(filepath.Join(os.Getenv("GOPATH"), "bin", name)); err != nil {
if err = internal.CurProject().InstallPlugin(plugin.D, plugin.A, plugin.C); err != nil {
color.Yellow("Waring: failed to install %s", plugin.D)
}
} else {
fmt.Printf("%s exists on the system \n", plugin.D)
}
}
}
return nil
}
76 changes: 76 additions & 0 deletions cmd/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"bytes"
"github.com/kcmvp/gob/internal"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

var v6 = "github.com/ofabry/go-callvis@v0.6.1"
var v7 = "github.com/ofabry/go-callvis@v0.7.0"
var golanglint = "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2"

func TestInstallPlugin(t *testing.T) {
internal.CurProject().LoadSettings()
cfg := internal.CurProject().Config()
defer func() {
os.Remove(cfg)
}()
os.Chdir(internal.CurProject().Root())
b := bytes.NewBufferString("")
rootCmd.SetOut(b)
rootCmd.SetArgs([]string{"plugin", "install", v6, "-a=callvis", "-c=run"})
err := rootCmd.Execute()
assert.NoError(t, err)
plugin, ok := lo.Find(internal.CurProject().Plugins(), func(item lo.Tuple4[string, string, string, string]) bool {
return item.D == v6
})
assert.Truef(t, ok, "%s should be installed successsfully", v6)
assert.Equal(t, "go-callvis", plugin.A)
assert.Equal(t, "callvis", plugin.B)
assert.Equal(t, "run", plugin.C)
assert.Equal(t, v6, plugin.D)
// install same plugin again
rootCmd.SetArgs([]string{"plugin", "install", v6, "-a=callvis", "-c=run"})
err = rootCmd.Execute()
assert.NoError(t, err)
plugin, ok = lo.Find(internal.CurProject().Plugins(), func(item lo.Tuple4[string, string, string, string]) bool {
return item.D == v6
})
assert.Truef(t, ok, "%s should be installed successsfully", v6)
assert.Equal(t, "go-callvis", plugin.A)
assert.Equal(t, "callvis", plugin.B)
assert.Equal(t, "run", plugin.C)
assert.Equal(t, v6, plugin.D)
// install same plugin with different version
rootCmd.SetArgs([]string{"plugin", "install", v7, "-a=callvis7", "-c=run7"})
err = rootCmd.Execute()
assert.NoError(t, err)
plugin, ok = lo.Find(internal.CurProject().Plugins(), func(item lo.Tuple4[string, string, string, string]) bool {
return item.D == v7
})
assert.Truef(t, ok, "%s should be installed successsfully", v7)
assert.Equal(t, "go-callvis", plugin.A)
assert.Equal(t, "callvis7", plugin.B)
assert.Equal(t, "run7", plugin.C)
assert.Equal(t, v7, plugin.D)

// install another plugin
rootCmd.SetArgs([]string{"plugin", "install", golanglint, "-a=lint", "-c=lint-run"})
err = rootCmd.Execute()
assert.NoError(t, err)
rootCmd.SetArgs([]string{"plugin"})
err = rootCmd.Execute()
assert.NoError(t, err)
plugin, ok = lo.Find(internal.CurProject().Plugins(), func(item lo.Tuple4[string, string, string, string]) bool {
return item.D == golanglint
})
assert.Equal(t, "golangci-lint", plugin.A)
assert.Equal(t, "lint", plugin.B)
assert.Equal(t, "lint-run", plugin.C)
assert.Equal(t, golanglint, plugin.D)
assert.Equal(t, 2, len(internal.CurProject().Plugins()))
}
File renamed without changes.
5 changes: 0 additions & 5 deletions cmd/setup.json → cmd/resources/setup.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
[
{
"name": "lint",
"url": "github.com/golangci/golangci-lint/cmd/golangci-lint",
"desc": "setup golangci-lint"
},
{
"name": "githook",
"desc": "setup git hook"
Expand Down
Loading