Skip to content

Commit

Permalink
feat: support multiple naming-styles, default to slug
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Jan 15, 2024
1 parent 9578ab8 commit ad3391c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ servers:
mirror:
dir: /tmp/github
default-action: exclude
naming-style: slug
rules:
- rule: group == "my-org"
action: include
Expand Down
9 changes: 9 additions & 0 deletions configschema/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@
],
"default": "https"
},
"naming-style": {
"type": "string",
"enum": [
"name",
"lowercase",
"slug"
],
"default": "slug"
},
"rules": {
"type": "array",
"items": {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.21

require (
github.com/cidverse/go-rules v0.0.0-20231112122021-075e5e6f8abc
github.com/cidverse/go-vcsapp v0.0.0-20240112214915-b44d4bf1c2aa
github.com/cidverse/go-vcsapp v0.0.0-20240114201213-aab0a1901330
github.com/go-git/go-git/v5 v5.11.0
github.com/gosimple/slug v1.13.1
github.com/mattn/go-colorable v0.1.13
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.8.0
Expand All @@ -30,6 +31,7 @@ require (
github.com/google/go-github/v57 v57.0.0 // indirect
github.com/google/go-github/v58 v58.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ github.com/bradleyfalzon/ghinstallation/v2 v2.9.0/go.mod h1:wmkTDJf8CmVypxE8ijIS
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cidverse/go-rules v0.0.0-20231112122021-075e5e6f8abc h1:eVuHW4U9BoiqjIwWPATHRm6oiphVlm3Jn7QgHGgeVq8=
github.com/cidverse/go-rules v0.0.0-20231112122021-075e5e6f8abc/go.mod h1:1E+4h0XkcYJVq3RHoYQradnCt7wk3JgnuRRnfeiwAzE=
github.com/cidverse/go-vcsapp v0.0.0-20240112214915-b44d4bf1c2aa h1:coN2Bjea0TORAJd93iMpigpcFZ8HeJiiOhhNYUHtoZs=
github.com/cidverse/go-vcsapp v0.0.0-20240112214915-b44d4bf1c2aa/go.mod h1:YRShsu8hUFBITTCrD1Re+/V4x+4o4PF1GCcQx1hlI7w=
github.com/cidverse/go-vcsapp v0.0.0-20240114201213-aab0a1901330 h1:20wLdKxIr17kV/I5ByuG3yQSECRCfrxA8jSzBGLJ6B0=
github.com/cidverse/go-vcsapp v0.0.0-20240114201213-aab0a1901330/go.mod h1:YRShsu8hUFBITTCrD1Re+/V4x+4o4PF1GCcQx1hlI7w=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
Expand Down Expand Up @@ -63,6 +63,10 @@ github.com/google/go-github/v58 v58.0.0 h1:Una7GGERlF/37XfkPwpzYJe0Vp4dt2k1kCjlx
github.com/google/go-github/v58 v58.0.0/go.mod h1:k4hxDKEfoWpSqFlc8LTpGd9fu2KrV1YAa6Hi6FmDNY4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func cloneCmd() *cobra.Command {
Namespace: r.Namespace,
Name: r.Name,
Remote: remote,
Directory: filepath.Join(util.ResolvePath(s.Mirror.LocalDir), r.Namespace, r.Name),
Directory: filepath.Join(util.ResolvePath(s.Mirror.LocalDir), util.Slugify(r.Namespace, string(s.Mirror.NamingStyle)), util.Slugify(r.Name, string(s.Mirror.NamingStyle))),
LastSync: time.Now(),
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type RepoBundleOptions struct {
type MirrorOpts struct {
LocalDir string `yaml:"dir"`
CloneMethod CloneMethod `yaml:"clone-method"`
NamingStyle NamingStyle `yaml:"naming-style"`
Rules []MirrorRule `yaml:"rules"`
DefaultAction RuleAction `yaml:"default-action"`
}
Expand All @@ -79,6 +80,14 @@ const (
RuleActionExclude RuleAction = "exclude"
)

type NamingStyle string

const (
NamingSchemeName NamingStyle = "name"
NamingSchemeLowercase NamingStyle = "lowercase"
NamingSchemeSlug NamingStyle = "slug"
)

func AuthToPlatformConfig(serverType string, serverUrl string, auth RepoSyncAuth) (vcsapp.PlatformConfig, error) {
// password file
if auth.PasswordFile != "" {
Expand Down
23 changes: 23 additions & 0 deletions pkg/util/slug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package util

import (
"strings"

"github.com/gosimple/slug"
"github.com/rs/zerolog/log"
)

// Slugify transforms a string for generate better directory names
func Slugify(input string, namingStyle string) string {
if namingStyle == "lowercase" {
return strings.ToLower(input)
} else if namingStyle == "slug" || namingStyle == "" {
s := slug.Make(input)
return strings.ToLower(s)
} else if namingStyle == "name" {
return input
}

log.Fatal().Msg("Invalid naming-scheme, must be one of: lowercase, slug, name")
return input
}

0 comments on commit ad3391c

Please sign in to comment.