Skip to content

Commit

Permalink
chore(config): use vcs instead of git for naming
Browse files Browse the repository at this point in the history
Signed-off-by: kilianpaquier <kilian@kilianpaquier.com>
  • Loading branch information
kilianpaquier committed Jan 9, 2025
1 parent 66effd0 commit ad13de1
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 70 deletions.
4 changes: 2 additions & 2 deletions pkg/configuration/craft/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "slices"
//
// Note that yaml tags are for .craft file property keys and json tags for templating data.
type Config struct {
FilesConfig `yaml:",inline"`
ConfigFiles `yaml:",inline"`

// Bot represents the name of the maintenance bot (renovate, dependabot, etc).
//
Expand Down Expand Up @@ -52,7 +52,7 @@ type Config struct {
// It's because Node projects contain all their scripts in package.json.
NoMakefile bool `json:"-" yaml:"no_makefile,omitempty"`

GitConfig `yaml:",inline"` // put at the end to get sorted properties (Platform especially) in written yaml file.
ConfigVCS `yaml:",inline"` // put at the end to get sorted properties (Platform especially) in written yaml file.
}

// Auth contains all authentication methods related to CI configuration.
Expand Down
18 changes: 9 additions & 9 deletions pkg/configuration/craft/config_files.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package craft

// FilesConfig structure contains all properties related
// ConfigFiles structure contains all properties related
// to files layout in a given project (languages, clis, cronjobs, jobs, workers, etc.).
//
// It's used in a composition with craft.Configuration.
//
// However, in case of a custom configuration (since generate and initialize packages can handle this),
// FilesConfig can be reused in another composition.
type FilesConfig struct {
// ConfigFiles can be reused in another composition.
type ConfigFiles struct {
// Languages is a map of languages name with its specificities.
Languages map[string]any `json:"-" yaml:"-"`

Expand All @@ -25,44 +25,44 @@ type FilesConfig struct {
}

// Binaries returns the sum of all executables (clis, cronjobs, jobs, workers).
func (g FilesConfig) Binaries() int {
func (g ConfigFiles) Binaries() int {
return len(g.Clis) + len(g.Crons) + len(g.Jobs) + len(g.Workers)
}

// SetLanguage sets a language with its specificities.
func (g *FilesConfig) SetLanguage(name string, value any) {
func (g *ConfigFiles) SetLanguage(name string, value any) {
if g.Languages == nil {
g.Languages = map[string]any{}
}
g.Languages[name] = value
}

// SetCLI sets a CLI with its name.
func (g *FilesConfig) SetCLI(name string) {
func (g *ConfigFiles) SetCLI(name string) {
if g.Clis == nil {
g.Clis = map[string]struct{}{}
}
g.Clis[name] = struct{}{}
}

// SetCron sets a cronjob with its name.
func (g *FilesConfig) SetCron(name string) {
func (g *ConfigFiles) SetCron(name string) {
if g.Crons == nil {
g.Crons = map[string]struct{}{}
}
g.Crons[name] = struct{}{}
}

// SetJob sets a job with its name.
func (g *FilesConfig) SetJob(name string) {
func (g *ConfigFiles) SetJob(name string) {
if g.Jobs == nil {
g.Jobs = map[string]struct{}{}
}
g.Jobs[name] = struct{}{}
}

// SetWorker sets a worker with its name.
func (g *FilesConfig) SetWorker(name string) {
func (g *ConfigFiles) SetWorker(name string) {
if g.Workers == nil {
g.Workers = map[string]struct{}{}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package craft

// GitConfig structure contains all properties related
// to how git configuration is read in a given project.
// ConfigVCS structure contains all properties related
// to how vcs configuration is read in a given project.
//
// It's used in a composition with craft.Configuration.
//
// However, in case of a custom configuration (since generate and initialize packages can handle this),
// GitConfig can be reused in another composition.
type GitConfig struct {
// Platform represents the git platform hosting the project.
// ConfigVCS can be reused in another composition.
type ConfigVCS struct {
// Platform represents the vcs platform hosting the project.
//
// On the first generation run (with parser.Git), it will be set.
// However, it's possible to override it manually in the .craft file.
Platform string `json:"-" yaml:"platform,omitempty"`

// ProjectHost represents the host where the project is hosted.
//
// As craft only handles git, it would be an host like github.com, gitlab.com, bitbucket.org, etc.
//
// Of course it can also be a private host like github.company.com.
//
// It will depend on the git origin URL or for golang the host of go.mod module name.
// It will depend on the vcs origin URL
// or for golang the host of go.mod module name.
ProjectHost string `json:"projectHost,omitempty" yaml:"-"`

// ProjectName is the project name being generated.
Expand Down
6 changes: 3 additions & 3 deletions pkg/generate/handler/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestGitHub(t *testing.T) {
require.True(t, ok)

config := craft.Config{
FilesConfig: craft.FilesConfig{Languages: map[string]any{"golang": nil}},
ConfigFiles: craft.ConfigFiles{Languages: map[string]any{"golang": nil}},
CI: &craft.CI{Name: craft.GitHub},
}

Expand All @@ -97,7 +97,7 @@ func TestGitHub(t *testing.T) {
{CI: &craft.CI{Name: craft.GitHub, Release: &craft.Release{}}},
{
CI: &craft.CI{Name: craft.GitHub},
FilesConfig: craft.FilesConfig{Languages: map[string]any{"go": nil}},
ConfigFiles: craft.ConfigFiles{Languages: map[string]any{"go": nil}},
},
}
for i, config := range configs {
Expand All @@ -120,7 +120,7 @@ func TestGitHub(t *testing.T) {
result, ok := handler.GitHub(".github/file.yml", "", "file.yml")
require.True(t, ok)

config := craft.Config{GitConfig: craft.GitConfig{Platform: craft.GitHub}}
config := craft.Config{ConfigVCS: craft.ConfigVCS{Platform: craft.GitHub}}

// Act
ok = result.ShouldRemove(config)
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/handler/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestGitLab(t *testing.T) {
result, ok := handler.GitLab(".gitlab/config.yml", "", "config.yml")
require.True(t, ok)

config := craft.Config{GitConfig: craft.GitConfig{Platform: craft.GitLab}}
config := craft.Config{ConfigVCS: craft.ConfigVCS{Platform: craft.GitLab}}

// Act
ok = result.ShouldRemove(config)
Expand Down
6 changes: 3 additions & 3 deletions pkg/generate/handler/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGolang(t *testing.T) {
cases := []craft.Config{
{},
{NoGoreleaser: true},
{FilesConfig: craft.FilesConfig{Clis: map[string]struct{}{"name": {}}}},
{ConfigFiles: craft.ConfigFiles{Clis: map[string]struct{}{"name": {}}}},
}
for _, config := range cases {
t.Run("", func(t *testing.T) {
Expand All @@ -44,7 +44,7 @@ func TestGolang(t *testing.T) {
t.Run("success_golang_gobuild_remove", func(t *testing.T) {
cases := []craft.Config{
{},
{FilesConfig: craft.FilesConfig{Languages: map[string]any{"golang": nil}}},
{ConfigFiles: craft.ConfigFiles{Languages: map[string]any{"golang": nil}}},
}
for _, config := range cases {
t.Run("", func(t *testing.T) {
Expand All @@ -69,7 +69,7 @@ func TestGolang(t *testing.T) {
require.True(t, ok)

config := craft.Config{
FilesConfig: craft.FilesConfig{
ConfigFiles: craft.ConfigFiles{
Clis: map[string]struct{}{"name": {}},
Languages: map[string]any{"golang": nil},
},
Expand Down
16 changes: 8 additions & 8 deletions pkg/generate/handler/standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestDependabot(t *testing.T) {

t.Run("success_dependabot_remove_option", func(t *testing.T) {
// Arrange
config := craft.Config{GitConfig: craft.GitConfig{Platform: craft.GitHub}}
config := craft.Config{ConfigVCS: craft.ConfigVCS{Platform: craft.GitHub}}

// Act
ok := result.ShouldRemove(config)
Expand All @@ -109,7 +109,7 @@ func TestDependabot(t *testing.T) {
// Arrange
config := craft.Config{
Bot: helpers.ToPtr(craft.Dependabot),
GitConfig: craft.GitConfig{Platform: craft.GitHub},
ConfigVCS: craft.ConfigVCS{Platform: craft.GitHub},
}

// Act
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestDocker(t *testing.T) {

globs := []string{"path/to/Dockerfile.tmpl", "path/to/Dockerfile-*.part.tmpl"}
config := craft.Config{
FilesConfig: craft.FilesConfig{Clis: map[string]struct{}{"cli": {}}},
ConfigFiles: craft.ConfigFiles{Clis: map[string]struct{}{"cli": {}}},
Docker: &craft.Docker{},
}

Expand All @@ -167,7 +167,7 @@ func TestDocker(t *testing.T) {
require.True(t, ok)

config := craft.Config{
FilesConfig: craft.FilesConfig{Clis: map[string]struct{}{"cli": {}}},
ConfigFiles: craft.ConfigFiles{Clis: map[string]struct{}{"cli": {}}},
Docker: &craft.Docker{},
}

Expand All @@ -184,7 +184,7 @@ func TestDocker(t *testing.T) {
require.True(t, ok)

config := craft.Config{
FilesConfig: craft.FilesConfig{Workers: map[string]struct{}{"worker": {}}},
ConfigFiles: craft.ConfigFiles{Workers: map[string]struct{}{"worker": {}}},
Docker: &craft.Docker{},
}

Expand All @@ -201,7 +201,7 @@ func TestDocker(t *testing.T) {
require.True(t, ok)

config := craft.Config{
FilesConfig: craft.FilesConfig{
ConfigFiles: craft.ConfigFiles{
Crons: map[string]struct{}{"cron": {}},
Workers: map[string]struct{}{"worker": {}},
},
Expand All @@ -221,7 +221,7 @@ func TestDocker(t *testing.T) {
require.True(t, ok)

config := craft.Config{
FilesConfig: craft.FilesConfig{
ConfigFiles: craft.ConfigFiles{
Clis: map[string]struct{}{"cli": {}},
Jobs: map[string]struct{}{"job": {}},
Languages: map[string]any{"golang": nil},
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestMakefile(t *testing.T) {
result, ok := handler.Makefile("Makefile", "", "Makefile")
require.True(t, ok)

config := craft.Config{FilesConfig: craft.FilesConfig{Languages: map[string]any{"node": nil}}}
config := craft.Config{ConfigFiles: craft.ConfigFiles{Languages: map[string]any{"node": nil}}}

// Act
ok = result.ShouldRemove(config)
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/parser/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGit(t *testing.T) {
// Arrange
config := &craft.Config{}
expected := &craft.Config{
GitConfig: craft.GitConfig{
ConfigVCS: craft.ConfigVCS{
ProjectHost: "github.com",
ProjectName: "craft",
ProjectPath: "kilianpaquier/craft",
Expand Down
8 changes: 4 additions & 4 deletions pkg/generate/parser/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Golang(ctx context.Context, destdir string, config *craft.Config) error {
}
return nil
}
config.GitConfig = statements.GitConfig() // replace all git properties with golang parsed ones
config.ConfigVCS = statements.ConfigVCS() // replace all git properties with golang parsed ones

// check hugo repository
if ok := isHugo(ctx, destdir, config); ok {
Expand Down Expand Up @@ -106,8 +106,8 @@ type Gomod struct {
ModulePath string
}

// GitConfig returns the git configuration associated to module statement in go.mod.
func (g Gomod) GitConfig() craft.GitConfig {
// ConfigVCS returns the vcs configuration associated to module statement in go.mod.
func (g Gomod) ConfigVCS() craft.ConfigVCS {
sections := strings.Split(g.ModulePath, "/")
projectPath := func() string {
if versionRegexp.MatchString(sections[len(sections)-1]) {
Expand All @@ -116,7 +116,7 @@ func (g Gomod) GitConfig() craft.GitConfig {
return strings.Join(sections[1:], "/") // retrieve all sections
}()

return craft.GitConfig{
return craft.ConfigVCS{
Platform: func() string { p, _ := parsePlatform(sections[0]); return p }(),
ProjectHost: sections[0],
ProjectPath: projectPath,
Expand Down
18 changes: 9 additions & 9 deletions pkg/generate/parser/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ func TestGolang(t *testing.T) {
), cfs.RwRR)
require.NoError(t, err)

config := craft.Config{FilesConfig: craft.FilesConfig{Languages: map[string]any{}}}
config := craft.Config{ConfigFiles: craft.ConfigFiles{Languages: map[string]any{}}}
expected := craft.Config{
FilesConfig: craft.FilesConfig{
ConfigFiles: craft.ConfigFiles{
Languages: map[string]any{
"golang": parser.Gomod{
LangVersion: "1.22",
ModulePath: "github.com/kilianpaquier/craft",
},
},
},
GitConfig: craft.GitConfig{
ConfigVCS: craft.ConfigVCS{
ProjectHost: "github.com",
ProjectName: "craft",
ProjectPath: "kilianpaquier/craft",
Expand Down Expand Up @@ -121,11 +121,11 @@ func TestGolang(t *testing.T) {
t.Cleanup(func() { assert.NoError(t, hugo.Close()) })

config := craft.Config{
FilesConfig: craft.FilesConfig{Languages: map[string]any{}},
ConfigFiles: craft.ConfigFiles{Languages: map[string]any{}},
}
expected := craft.Config{
FilesConfig: craft.FilesConfig{Languages: map[string]any{"hugo": nil}},
GitConfig: craft.GitConfig{
ConfigFiles: craft.ConfigFiles{Languages: map[string]any{"hugo": nil}},
ConfigVCS: craft.ConfigVCS{
ProjectHost: "github.com",
ProjectName: "craft",
ProjectPath: "kilianpaquier/craft",
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestGolang(t *testing.T) {
}

config := craft.Config{
FilesConfig: craft.FilesConfig{
ConfigFiles: craft.ConfigFiles{
Languages: map[string]any{},
Clis: map[string]struct{}{},
Crons: map[string]struct{}{},
Expand All @@ -176,7 +176,7 @@ func TestGolang(t *testing.T) {
},
}
expected := craft.Config{
FilesConfig: craft.FilesConfig{
ConfigFiles: craft.ConfigFiles{
Clis: map[string]struct{}{"cli-name": {}},
Crons: map[string]struct{}{"cron-name": {}},
Jobs: map[string]struct{}{"job-name": {}},
Expand All @@ -188,7 +188,7 @@ func TestGolang(t *testing.T) {
},
Workers: map[string]struct{}{"worker-name": {}},
},
GitConfig: craft.GitConfig{
ConfigVCS: craft.ConfigVCS{
ProjectHost: "github.com",
ProjectName: "craft",
ProjectPath: "kilianpaquier/craft",
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/parser/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestHelm(t *testing.T) {
require.NoError(t, err)

config := craft.Config{
FilesConfig: craft.FilesConfig{
ConfigFiles: craft.ConfigFiles{
Languages: map[string]any{},
Clis: map[string]struct{}{"cli-name": {}},
Crons: map[string]struct{}{"cron-name": {}},
Expand Down
6 changes: 3 additions & 3 deletions pkg/generate/parser/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestLicense(t *testing.T) {

destdir := t.TempDir()
config := craft.Config{
GitConfig: craft.GitConfig{ProjectName: "craft"},
ConfigVCS: craft.ConfigVCS{ProjectName: "craft"},
License: helpers.ToPtr("mit"),
Maintainers: []*craft.Maintainer{{Name: "name"}},
}
Expand All @@ -100,7 +100,7 @@ func TestLicense(t *testing.T) {
dest := filepath.Join(destdir, parser.FileLicense)

config := craft.Config{
GitConfig: craft.GitConfig{ProjectName: "craft"},
ConfigVCS: craft.ConfigVCS{ProjectName: "craft"},
License: helpers.ToPtr("mit"),
Maintainers: []*craft.Maintainer{{Name: "name"}},
}
Expand All @@ -125,7 +125,7 @@ func TestLicense(t *testing.T) {
require.NoError(t, file.Close())

config := craft.Config{
GitConfig: craft.GitConfig{ProjectName: "craft"},
ConfigVCS: craft.ConfigVCS{ProjectName: "craft"},
License: helpers.ToPtr("mit"),
Maintainers: []*craft.Maintainer{{Name: "name"}},
}
Expand Down
Loading

0 comments on commit ad13de1

Please sign in to comment.