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

#47: remove command section of plugin #48

Merged
merged 1 commit into from
Jan 11, 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
4 changes: 3 additions & 1 deletion cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"os"
)

//go:embed resources
//go:embed resources/*
var resources embed.FS

const resourceDir = "resources"

// builderCmd represents the base command when called without any subcommands
var builderCmd = &cobra.Command{
Use: "gob",
Expand Down
2 changes: 0 additions & 2 deletions cmd/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func (suite *BuilderTestSuit) TestBuiltinPlugins() {
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)
assert.Equal(suite.T(), "run ./...", plugin.Command)
plugin, ok = lo.Find(plugins, func(plugin internal.Plugin) bool {
return plugin.Url == "gotest.tools/gotestsum"
})
Expand All @@ -71,6 +70,5 @@ func (suite *BuilderTestSuit) TestBuiltinPlugins() {
assert.Equal(suite.T(), "gotestsum", plugin.Name())
assert.Equal(suite.T(), "gotest.tools/gotestsum", plugin.Module())
assert.Equal(suite.T(), "test", plugin.Alias)
assert.Equal(suite.T(), "", plugin.Command)

}
12 changes: 7 additions & 5 deletions cmd/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func initBuildVersion() {
}
ver := filepath.Join(infra, "version.go")
if _, err := os.Stat(ver); err != nil {
data, _ := resources.ReadFile("resources/version.tmpl")
data, _ := resources.ReadFile(filepath.Join(resourceDir, "version.tmpl"))
os.WriteFile(ver, data, 0666) //nolint
}
}
Expand All @@ -47,10 +47,12 @@ func initializerFunc(_ *cobra.Command, _ []string) {
internal.CurProject().SetupPlugin(plugin)
if len(plugin.Config) > 0 {
if _, err := os.Stat(filepath.Join(internal.CurProject().Root(), plugin.Config)); err != nil {
data, _ := resources.ReadFile(filepath.Join("resource", plugin.Config))
err = os.WriteFile(filepath.Join(internal.CurProject().Root(), plugin.Config), data, os.ModePerm)
if err != nil {
color.Red("failed to create configuration %s", plugin.Config)
if data, err := resources.ReadFile(filepath.Join(resourceDir, plugin.Config)); err == nil {
if err = os.WriteFile(filepath.Join(internal.CurProject().Root(), plugin.Config), data, os.ModePerm); err != nil {
color.Red("failed to create configuration %s", plugin.Config)
}
} else {
color.Red("can not find the configuration %s", plugin.Config)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func list(_ *cobra.Command, _ ...string) error {
style.HTML.CSSClass = table.DefaultHTMLCSSClass
ct.SetStyle(style)
rows := lo.Map(plugins, func(plugin internal.Plugin, index int) table.Row {
return table.Row{plugin.Name(), plugin.Command, plugin.Args, plugin.Url}
return table.Row{plugin.Name(), plugin.Alias, plugin.Args, plugin.Url}
})
ct.AppendRows(rows)
fmt.Println(ct.Render())
Expand Down
2 changes: 1 addition & 1 deletion cmd/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"plugins": [
{
"alias": "lint",
"command": "run ./...",
"args": "run ./...",
"url": "github.com/golangci/golangci-lint/cmd/golangci-lint",
"config": ".golangci.yaml"
},
Expand Down
11 changes: 3 additions & 8 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const modulePattern = `^[^@]+@?[^@\s]+$`

type Plugin struct {
Alias string `json:"alias" mapstructure:"alias"`
Command string `json:"command" mapstructure:"command"`
Args string `json:"args" mapstructure:"args"`
Url string `json:"url" mapstructure:"url"` //nolint
Config string `json:"config" mapstructure:"config"`
Expand Down Expand Up @@ -78,13 +77,9 @@ func (plugin *Plugin) UnmarshalJSON(data []byte) error {
}

func NewPlugin(url string, options ...string) (Plugin, error) {
plugin := Plugin{Url: url}
for i, option := range options {
if i == 2 {
plugin.Command = option
} else if i == 3 {
plugin.Args = option
}
plugin := Plugin{
Url: url,
Args: strings.Join(options, " "),
}
if err := plugin.init(); err != nil {
return Plugin{}, err
Expand Down
3 changes: 1 addition & 2 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestUnmarshalJSON(t *testing.T) {
assert.Equal(t, "golangci-lint", plugin.Name())
assert.Equal(t, "github.com/golangci/golangci-lint", plugin.Module())
assert.Equal(t, "lint", plugin.Alias)
assert.Equal(t, "run ./...", plugin.Command)
// no command
plugin, ok = lo.Find(plugins, func(plugin Plugin) bool {
return plugin.Url == "gotest.tools/gotestsum"
})
Expand All @@ -91,6 +91,5 @@ func TestUnmarshalJSON(t *testing.T) {
assert.Equal(t, "gotestsum", plugin.Name())
assert.Equal(t, "gotest.tools/gotestsum", plugin.Module())
assert.Equal(t, "test", plugin.Alias)
assert.Equal(t, "", plugin.Command)

}
7 changes: 3 additions & 4 deletions internal/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,9 @@ func (project *Project) Plugins() []Plugin {
func (project *Project) SetupPlugin(plugin Plugin) {
if !project.isSetup(plugin) {
values := lo.MapEntries(map[string]string{
"alias": plugin.Alias,
"command": plugin.Command,
"args": plugin.Args,
"url": fmt.Sprintf("%s@%s", plugin.Url, plugin.Version()),
"alias": plugin.Alias,
"args": plugin.Args,
"url": fmt.Sprintf("%s@%s", plugin.Url, plugin.Version()),
}, func(key string, value string) (string, any) {
return fmt.Sprintf("%s.%s.%s", pluginCfgKey, plugin.Name(), key), value
})
Expand Down