Skip to content

Commit

Permalink
Merge pull request #124 from kcmvp/scaffold
Browse files Browse the repository at this point in the history
code refactor
  • Loading branch information
kcmvp authored Aug 16, 2024
2 parents f4ed1cc + 93862a0 commit df7525d
Show file tree
Hide file tree
Showing 33 changed files with 24 additions and 51 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# Go workspace file
go.work
.idea
gob
11.json
target
*.sql
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions cmd/gbc/artifact/plugin.go → cmd/gob/artifact/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func (plugin *Plugin) init() error {
plugin.version = "latest"
plugin.Url = strings.TrimSpace(plugin.Url)
reg := regexp.MustCompile(`@\S*`)
matches := reg.FindAllString(plugin.Url, -1)
if len(matches) > 0 {
if matches := reg.FindAllString(plugin.Url, -1); len(matches) > 0 {
plugin.version = strings.Trim(matches[0], "@")
}
plugin.Url = reg.ReplaceAllString(plugin.Url, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type InternalPluginTestSuit struct {
suite.Suite
lintLatestVersion string
//lintLatestVersion string
gotestsumLatestVersion string
}

Expand All @@ -27,7 +27,6 @@ func (suite *InternalPluginTestSuit) TearDownSuite() {

func TestInternalPluginSuite(t *testing.T) {
suite.Run(t, &InternalPluginTestSuit{
lintLatestVersion: LatestVersion(false, "github.com/golangci/golangci-lint")[0].B,
gotestsumLatestVersion: LatestVersion(false, "gotest.tools/gotestsum")[0].B,
})
}
Expand All @@ -45,22 +44,6 @@ func (suite *InternalPluginTestSuit) TestNewPlugin() {
binary string
wantErr bool
}{
{
name: "without version",
url: "github.com/golangci/golangci-lint/cmd/golangci-lint",
module: "github.com/golangci/golangci-lint",
logName: "golangci-lint",
binary: fmt.Sprintf("%s-%s", "golangci-lint", suite.lintLatestVersion),
wantErr: false,
},
{
name: "latest version",
url: "github.com/golangci/golangci-lint/cmd/golangci-lint@latest",
module: "github.com/golangci/golangci-lint",
logName: "golangci-lint",
binary: fmt.Sprintf("%s-%s", "golangci-lint", suite.lintLatestVersion),
wantErr: false,
},
{
name: "specific version",
url: "github.com/golangci/golangci-lint/cmd/[email protected]",
Expand Down Expand Up @@ -120,23 +103,15 @@ func (suite *InternalPluginTestSuit) TestUnmarshalJSON() {
defer func() {
os.RemoveAll(gopath)
}()
data, _ := os.ReadFile(filepath.Join(CurProject().Root(), "cmd", "gbc", "command", "resources", "config.json"))
data, _ := os.ReadFile(filepath.Join(CurProject().Root(), "cmd", "gob", "command", "resources", "config.json"))
v := gjson.GetBytes(data, "gbc_init.plugins")
var plugins []Plugin
err := json.Unmarshal([]byte(v.Raw), &plugins)
t := suite.T()
assert.NoError(t, err)
assert.Equal(t, 2, len(plugins))
plugin, ok := lo.Find(plugins, func(plugin Plugin) bool {
return plugin.Url == "github.com/golangci/golangci-lint/cmd/golangci-lint"
})
assert.True(t, ok)
assert.Equal(t, suite.lintLatestVersion, 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)
// no command
plugin, ok = lo.Find(plugins, func(plugin Plugin) bool {
plugin, ok := lo.Find(plugins, func(plugin Plugin) bool {
return plugin.Url == "gotest.tools/gotestsum"
})
assert.True(t, ok)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ProjectTestSuite struct {
}

func (suite *ProjectTestSuite) BeforeTest(_, testName string) {
s, _ := os.Open(filepath.Join(CurProject().Root(), "cmd", "gbc", "testdata", "gob.yaml"))
s, _ := os.Open(filepath.Join(CurProject().Root(), "cmd", "gob", "testdata", "gob.yaml"))
root := filepath.Join(CurProject().Root(), "target", fmt.Sprintf("artifact_ProjectTestSuite_%s", testName))
os.MkdirAll(root, os.ModePerm)
t, _ := os.Create(filepath.Join(root, "gob.yaml"))
Expand Down Expand Up @@ -126,7 +126,7 @@ func (suite *ProjectTestSuite) TestValidate() {
func (suite *ProjectTestSuite) TestMainFiles() {
mainFiles := CurProject().MainFiles()
assert.Equal(suite.T(), 1, len(mainFiles))
assert.True(suite.T(), lo.Contains(mainFiles, filepath.Join(CurProject().Root(), "cmd", "gbc", "gbc.go")))
assert.True(suite.T(), lo.Contains(mainFiles, filepath.Join(CurProject().Root(), "cmd", "gob", "gob.go")))
}

func (suite *ProjectTestSuite) TestVersion() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/kcmvp/gob/cmd/gbc/artifact" //nolint
"github.com/kcmvp/gob/cmd/gob/artifact" //nolint
"os"
"os/exec"
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package command

import (
"github.com/fatih/color"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/kcmvp/gob/utils"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -30,7 +30,7 @@ func (suite *ActionTestSuite) TearDownSuite() {
func (suite *ActionTestSuite) TestActionBuild() {
err := buildAction(nil)
assert.NoError(suite.T(), err)
binary := filepath.Join(artifact.CurProject().Target(), lo.If(artifact.Windows(), "gbc.exe").Else("gbc"))
binary := filepath.Join(artifact.CurProject().Target(), lo.If(artifact.Windows(), "gob.exe").Else("gob"))
_, err = os.Stat(binary)
assert.NoError(suite.T(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gbc/command/deps.go → cmd/gob/command/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"strings"

"github.com/kcmvp/gob/cmd/gbc/artifact" //nolint
"github.com/kcmvp/gob/cmd/gob/artifact" //nolint

"github.com/fatih/color"
"github.com/samber/lo"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package command

import (
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/stretchr/testify/assert"
"os"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion cmd/gbc/command/exec.go → cmd/gob/command/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"

"github.com/fatih/color"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/samber/lo"
"github.com/spf13/cobra"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/gbc/command/exec_test.go → cmd/gob/command/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package command

import (
"fmt"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/kcmvp/gob/utils"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand All @@ -20,7 +20,7 @@ type ExecTestSuite struct {

func (suite *ExecTestSuite) BeforeTest(_, testName string) {
os.Chdir(artifact.CurProject().Root())
s, _ := os.Open(filepath.Join(artifact.CurProject().Root(), "cmd", "gbc", "testdata", "gob.yaml"))
s, _ := os.Open(filepath.Join(artifact.CurProject().Root(), "cmd", "gob", "testdata", "gob.yaml"))
_, method := utils.TestCaller()
root := filepath.Join(artifact.CurProject().Root(), "target", strings.ReplaceAll(method, "_BeforeTest", fmt.Sprintf("_%s", testName)))
os.MkdirAll(root, os.ModePerm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package command

import (
"fmt"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/spf13/cobra"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package command
import (
"encoding/json"
"fmt"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/kcmvp/gob/utils"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand All @@ -29,7 +29,7 @@ func TestInitializeTestSuit(t *testing.T) {
func (suite *InitializeTestSuite) BeforeTest(_, testName string) {
os.Chdir(artifact.CurProject().Root())
var s, t *os.File
s, _ = os.Open(filepath.Join(artifact.CurProject().Root(), "cmd", "gbc", "testdata", "config.json"))
s, _ = os.Open(filepath.Join(artifact.CurProject().Root(), "cmd", "gob", "testdata", "config.json"))
_, method := utils.TestCaller()
root := filepath.Join(artifact.CurProject().Root(), "target", strings.ReplaceAll(method, "_BeforeTest", fmt.Sprintf("_%s", testName)))
os.MkdirAll(root, os.ModePerm)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gbc/command/plugin.go → cmd/gob/command/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/samber/lo"
"github.com/spf13/cobra"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package command

import (
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/kcmvp/gob/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/gbc/command/root.go → cmd/gob/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"fmt"
"github.com/fatih/color"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/kcmvp/gob/utils"
"github.com/samber/lo"
"github.com/spf13/cobra"
Expand Down
6 changes: 3 additions & 3 deletions cmd/gbc/command/root_test.go → cmd/gob/command/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package command
import (
"fmt"
"github.com/fatih/color"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/cmd/gob/artifact"
"github.com/kcmvp/gob/utils"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +26,7 @@ func TestRootTestSuit(t *testing.T) {

func (suite *RootTestSuit) BeforeTest(_, testName string) {
os.Chdir(artifact.CurProject().Root())
s, _ := os.Open(filepath.Join(artifact.CurProject().Root(), "cmd", "gbc", "testdata", "config.json"))
s, _ := os.Open(filepath.Join(artifact.CurProject().Root(), "cmd", "gob", "testdata", "config.json"))
_, method := utils.TestCaller()
root := filepath.Join(artifact.CurProject().Root(), "target", strings.ReplaceAll(method, "_BeforeTest", fmt.Sprintf("_%s", testName)))
os.MkdirAll(root, os.ModePerm)
Expand Down Expand Up @@ -149,7 +149,7 @@ func (suite *RootTestSuit) TestRunE() {
target := artifact.CurProject().Target()
err := rootCmd.RunE(rootCmd, []string{"build"})
assert.NoError(suite.T(), err)
_, err = os.Stat(filepath.Join(target, lo.If(artifact.Windows(), "gbc.exe").Else("gbc")))
_, err = os.Stat(filepath.Join(target, lo.If(artifact.Windows(), "gob.exe").Else("gob")))
assert.NoError(suite.T(), err, "binary should be generated")
err = rootCmd.RunE(rootCmd, []string{"build", "clean"})
assert.NoError(suite.T(), err)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/gbc/gbc.go → cmd/gob/gob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright © 2023 [email protected]
package main

import (
"github.com/kcmvp/gob/cmd/gbc/command"
"github.com/kcmvp/gob/cmd/gob/command"
"os" //nolint
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit df7525d

Please sign in to comment.