Skip to content

Commit

Permalink
Merge pull request #104 from kcmvp/doc-fix
Browse files Browse the repository at this point in the history
#103: fix go:embed path on windows system
  • Loading branch information
kcmvp authored May 18, 2024
2 parents 305127a + e9a2ea1 commit b5688b3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
16 changes: 9 additions & 7 deletions cmd/gbc/artifact/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type GitHook struct {

func (project *Project) GitHook() GitHook {
var hook GitHook
project.config().UnmarshalKey(execCfgKey, &hook) //nolint
project.load().UnmarshalKey(execCfgKey, &hook) //nolint
return hook
}

Expand All @@ -44,8 +44,11 @@ type Execution struct {
}

func (project *Project) Executions() []Execution {
values := project.config().Get(execCfgKey).(map[string]any)
return lo.MapToSlice(values, func(key string, v any) Execution {
values := project.load().Get(execCfgKey)
if values == nil {
return []Execution{}
}
return lo.MapToSlice(values.(map[string]any), func(key string, v any) Execution {
var actions []string
if _, ok := v.(string); ok {
actions = append(actions, v.(string))
Expand Down Expand Up @@ -74,9 +77,8 @@ func (project *Project) SetupHooks(force bool) error {
color.Yellow("project is not in the source control")
return nil
}
if err := project.config().ReadInConfig(); err != nil {
return err
}
// force load configuration again for testing
_ = project.load().ReadInConfig()
gitHook := CurProject().GitHook()
var hooks []string
if len(gitHook.CommitMsg) > 0 {
Expand All @@ -89,7 +91,7 @@ func (project *Project) SetupHooks(force bool) error {
hooks = append(hooks, PrePush)
}
shell := lo.IfF(Windows(), func() string {
return "#!/usr/bin/env pwsh\n"
return "#!/usr/bin/env bash\n"
}).Else("#!/bin/sh\n")
hookDir := CurProject().HookDir()
for name, script := range HookScripts() {
Expand Down
12 changes: 7 additions & 5 deletions cmd/gbc/artifact/internal_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type InternalPluginTestSuit struct {
suite.Suite
lintLatestVersion string
}

func (suite *InternalPluginTestSuit) TearDownSuite() {
Expand All @@ -24,15 +25,16 @@ func (suite *InternalPluginTestSuit) TearDownSuite() {
}

func TestInternalPluginSuite(t *testing.T) {
suite.Run(t, &InternalPluginTestSuit{})
suite.Run(t, &InternalPluginTestSuit{
lintLatestVersion: LatestVersion("github.com/golangci/golangci-lint")[0].B,
})
}

func (suite *InternalPluginTestSuit) TestNewPlugin() {
gopath := GoPath()
defer func() {
os.RemoveAll(gopath)
}()
version := LatestVersion("github.com/golangci/golangci-lint")
tests := []struct {
name string
url string
Expand All @@ -46,15 +48,15 @@ func (suite *InternalPluginTestSuit) TestNewPlugin() {
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", version[0].B),
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", version[0].B),
binary: fmt.Sprintf("%s-%s", "golangci-lint", suite.lintLatestVersion),
wantErr: false,
},
{
Expand Down Expand Up @@ -128,7 +130,7 @@ func (suite *InternalPluginTestSuit) TestUnmarshalJSON() {
return plugin.Url == "github.com/golangci/golangci-lint/cmd/golangci-lint"
})
assert.True(t, ok)
assert.Equal(t, "v1.57.2", plugin.Version())
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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gbc/artifact/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Plugin struct {
Alias string `json:"alias" mapstructure:"alias"`
Args string `json:"args" mapstructure:"args"`
Url string `json:"url" mapstructure:"url"` //nolint
Config string `json:"config" mapstructure:"config"`
Config string `json:"load" mapstructure:"load"`
Description string `json:"description" mapstructure:"description"`
version string
name string
Expand Down
12 changes: 6 additions & 6 deletions cmd/gbc/artifact/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Project struct {
cfgs sync.Map // store all the configuration
}

func (project *Project) config() *viper.Viper {
func (project *Project) load() *viper.Viper {
testEnv, file := utils.TestCaller()
key := lo.If(testEnv, file).Else(defaultCfgKey)
obj, ok := project.cfgs.Load(key)
Expand All @@ -49,15 +49,15 @@ func (project *Project) config() *viper.Viper {
if err := v.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
if errors.As(err, &configFileNotFoundError) {
color.Yellow("Warning: can not find configuration gob.yaml")
log.Fatal(color.RedString("error: can not find configuration gob.yaml"))
}
}
project.cfgs.Store(key, v)
return v
}

func (project *Project) mergeConfig(cfg map[string]any) error {
viper := project.config()
viper := project.load()
err := viper.MergeConfigMap(cfg)
if err != nil {
return err
Expand Down Expand Up @@ -182,7 +182,7 @@ func (project *Project) MainFiles() []string {
}

func (project *Project) Plugins() []Plugin {
viper := project.config()
viper := project.load()
if v := viper.Get(pluginCfgKey); v != nil {
plugins := v.(map[string]any)
return lo.MapToSlice(plugins, func(key string, _ any) Plugin {
Expand Down Expand Up @@ -225,14 +225,14 @@ func (project *Project) InstallPlugin(plugin Plugin) error {
if err = project.mergeConfig(values); err != nil {
return err
}
_ = project.config().ReadInConfig()
_ = project.load().ReadInConfig()
}
_, err = plugin.install()
return err
}

func (project *Project) settled(plugin Plugin) bool {
return project.config().Get(fmt.Sprintf("plugins.%s.url", plugin.name)) != nil
return project.load().Get(fmt.Sprintf("plugins.%s.url", plugin.name)) != nil
}

func (project *Project) Validate() error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/gbc/command/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (suite *InitializeTestSuite) TearDownSuite() {
}

func (suite *RootTestSuit) TestParsePlugins() {
result, err := parseArtefacts(initializerCmd, []string{""}, "plugins")
result, err := parseArtifacts(initializerCmd, []string{""}, "plugins")
assert.NoError(suite.T(), err)
assert.True(suite.T(), result.Exists())
var plugins []artifact.Plugin
Expand All @@ -58,7 +58,7 @@ func (suite *RootTestSuit) TestParsePlugins() {
}

func (suite *RootTestSuit) TestParseDeps() {
result, err := parseArtefacts(initializerCmd, []string{""}, "deps")
result, err := parseArtifacts(initializerCmd, []string{""}, "deps")
assert.NoError(suite.T(), err)
assert.True(suite.T(), result.Exists())
var deps []string
Expand Down
4 changes: 2 additions & 2 deletions cmd/gbc/command/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"alias": "lint",
"args": "run ./...",
"url": "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2",
"url": "github.com/golangci/golangci-lint/cmd/golangci-lint",
"config": ".golangci.yaml"
},
{
Expand All @@ -15,7 +15,7 @@
],
"deps": [
"github.com/kcmvp/gob",
"github.com/stretchr/testify",
"github.com/stretchr/testify"
]
}
}
12 changes: 6 additions & 6 deletions cmd/gbc/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ var (

func usageTemplate() string {
once.Do(func() {
bytes, _ := templates.ReadFile(filepath.Join(tmplDir, "usage.tmpl"))
bytes, _ := templates.ReadFile(fmt.Sprintf("%s/usage.tmpl", tmplDir))
usage = color.YellowString(string(bytes))
})
return usage
}

func parseArtefacts(cmd *cobra.Command, args []string, name string) (gjson.Result, error) {
func parseArtifacts(cmd *cobra.Command, args []string, name string) (gjson.Result, error) {
var result gjson.Result
var data []byte
var err error
if test, uqf := utils.TestCaller(); test {
data, err = os.ReadFile(filepath.Join(artifact.CurProject().Root(), "target", uqf, "config.json"))
} else {
data, err = resources.ReadFile(filepath.Join(resourceDir, "config.json"))
data, err = resources.ReadFile(fmt.Sprintf("%s/config.json", resourceDir))
}
if err != nil {
return result, err
Expand All @@ -62,7 +62,7 @@ func parseArtefacts(cmd *cobra.Command, args []string, name string) (gjson.Resul
}

func installPlugins(cmd *cobra.Command, args []string) error {
result, err := parseArtefacts(cmd, args, "plugins")
result, err := parseArtifacts(cmd, args, "plugins")
if result.Exists() {
var data []byte
var plugins []artifact.Plugin
Expand All @@ -73,7 +73,7 @@ func installPlugins(cmd *cobra.Command, args []string) error {
}
if len(plugin.Config) > 0 {
if _, err = os.Stat(filepath.Join(artifact.CurProject().Root(), plugin.Config)); err != nil {
if data, err = resources.ReadFile(filepath.Join(resourceDir, plugin.Config)); err == nil {
if data, err = resources.ReadFile(fmt.Sprintf("%s/%s", resourceDir, plugin.Config)); err == nil {
if err = os.WriteFile(filepath.Join(artifact.CurProject().Root(), plugin.Config), data, os.ModePerm); err != nil {
break
}
Expand All @@ -91,7 +91,7 @@ func installPlugins(cmd *cobra.Command, args []string) error {
}

func installDeps(cmd *cobra.Command, args []string) error {
result, err := parseArtefacts(cmd, args, "deps")
result, err := parseArtifacts(cmd, args, "deps")
if result.Exists() {
var cfgDeps []string
err = json.Unmarshal([]byte(result.Raw), &cfgDeps)
Expand Down

0 comments on commit b5688b3

Please sign in to comment.