8000 feat: Deps mirrors by hound672 · Pull Request #143 · easyp-tech/easyp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Deps mirrors #143

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ for example:
insteadOf = https://github.com/
```

### Use mirrors

For use your company private mirrors (e.g artifactory) you can use `mirrors` settings.

```yaml
mirrors:
- origin: onprem-vcs.loc
use: github.com
```

In that case dependency from your config with host `onprem-vcs.loc` will be replaced with `github.com`.

So you are able to download deps from your mirror without modificate easyp config file.

In case when you have to use private

## Auto-completion

### zsh auto-completion
Expand Down
9 changes: 6 additions & 3 deletions example.easyp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ lint:
- COMMENT_SERVICE
- IMPORT_USED
deps:
- github.com/googleapis/googleapis
- github.com/bufbuild/protovalidate@v0.3.1
- github.com/grpc-ecosystem/grpc-gateway@v2.19.1
- onprem-vcs.loc/googleapis/googleapis
# - github.com/bufbuild/protovalidate@v0.3.1
# - github.com/grpc-ecosystem/grpc-gateway@v2.19.1

breaking:
ignore:
Expand Down Expand Up @@ -45,3 +45,6 @@ generate:
opts:
paths: source_relative

mirrors:
- origin: onprem-vcs.loc
use: github.com
7 changes: 5 additions & 2 deletions internal/adapters/repository/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"

Expand Down Expand Up @@ -39,13 +40,15 @@ type Console interface {

// New returns gitRepo instance
// remote: full remoteURL address without schema
func New(ctx context.Context, remote string, cacheDir string, console Console) (repository.Repo, error) {
func New(ctx context.Context, remoteURL string, cacheDir string, console Console) (repository.Repo, error) {
r := &gitRepo{
remoteURL: getRemote(remote),
remoteURL: getRemote(remoteURL),
cacheDir: cacheDir,
console: console,
}

slog.Debug("clone", "remoteURL", remoteURL, "cacheDir", cacheDir)

if _, err := os.Stat(filepath.Join(r.cacheDir, "objects")); err == nil {
// repo is already exists
return r, nil
Expand Down
9 changes: 9 additions & 0 deletions internal/api/temporaly_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func buildCore(_ context.Context, cfg config.Config, dirWalker core.DirWalker) (
AgainstGitRef: cfg.BreakingCheck.AgainstGitRef,
}

mirrors := lo.Map(cfg.Mirrors, func(item config.Mirror, _ int) core.MirrorConfig {
return core.MirrorConfig{
Origin: item.Origin,
Use: item.Use,
}
})
depAddressResolver := core.NewDepAddressResolver(mirrors)

app := core.New(
lintRules,
cfg.Lint.Ignore,
Expand Down Expand Up @@ -117,6 +125,7 @@ func buildCore(_ context.Context, cfg config.Config, dirWalker core.DirWalker) (
lockFile,
currentProjectGitWalker,
breakingCheckConfig,
depAddressResolver,
)

return app, nil
Expand Down
9 changes: 9 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type InputFilesDir struct {
Root string `yaml:"root"`
}

// Mirror is the mirror setup for dependency
type Mirror struct {
Origin string `yaml:"origin"`
Use string `yaml:"use"`
}

// Config is the configuration of easyp.
type Config struct {
// LintConfig is the lint configuration.
Expand All @@ -61,6 +67,9 @@ type Config struct {

// BreakingCheck `breaking` command's configuration
BreakingCheck BreakingCheck `json:"breaking" yaml:"breaking"`

// Mirrors configs
Mirrors []Mirror `json:"mirrors" yaml:"mirrors"`
}

var errFileNotFound = errors.New("config file not found")
Expand Down
4 changes: 4 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Core struct {

breakingCheckConfig BreakingCheckConfig
currentProjectGitWalker CurrentProjectGitWalker

depAddressResolver DepAddressResolver
}

var (
Expand All @@ -43,6 +45,7 @@ func New(
lockFile LockFile,
currentProjectGitWalker CurrentProjectGitWalker,
breakingCheckConfig BreakingCheckConfig,
depAddressResolver DepAddressResolver,
) *Core {
return &Core{
rules: rules,
Expand All @@ -58,5 +61,6 @@ func New(
lockFile: lockFile,
currentProjectGitWalker: currentProjectGitWalker,
breakingCheckConfig: breakingCheckConfig,
depAddressResolver: depAddressResolver,
}
}
33 changes: 33 additions & 0 deletions internal/core/dep_address_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core

import (
"strings"
)

type MirrorConfig struct {
Origin string
Use string
}

type DepAddressResolver struct {
mirrors []MirrorConfig
}

func NewDepAddressResolver(mirrors []MirrorConfig) DepAddressResolver {
return DepAddressResolver{
mirrors: mirrors,
}
}

func (r DepAddressResolver) Resolve(requestedModule string) string {
requestedModule = r.useMirrors(requestedModule)
return requestedModule
}

func (r DepAddressResolver) useMirrors(requestedModule string) string {
for _, mirror := range r.mirrors {
requestedModule = strings.Replace(requestedModule, mirror.Origin, mirror.Use, 1)
}

return requestedModule
}
39 changes: 39 additions & 0 deletions internal/core/dep_address_resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package core

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_DepAddressResolver_Resolve(t *testing.T) {
mirrors := []MirrorConfig{
{
Origin: "github.com",
Use: "gitlab.com",
},
}

resolver := NewDepAddressResolver(mirrors)

tests := map[string]struct {
requestedModule string
expected string
}{
"not replaced": {
requestedModule: "vcs.com/github/octocat.git",
expected: "vcs.com/github/octocat.git",
},
"replaced": {
requestedModule: "github.com/github/octocat.git",
expected: "gitlab.com/github/octocat.git",
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := resolver.Resolve(test.requestedModule)
require.Equal(t, test.expected, res)
})
}
}
4 changes: 2 additions & 2 deletions internal/core/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (c *Core) Get(ctx context.Context, requestedModule models.Module) error {
return fmt.Errorf("c.storage.CreateCacheRepositoryDir: %w", err)
}

// TODO: use factory (git, svn etc)
repo, err := git.New(ctx, requestedModule.Name, cacheRepositoryDir, c.console)
remoteURL := c.depAddressResolver.Resolve(requestedModule.Name)
repo, err := git.New(ctx, remoteURL, cacheRepositoryDir, c.console)
if err != nil {
return fmt.Errorf("git.New: %w", err)
}
Expand Down
0