-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to create remote repository before first push
fix lint
- Loading branch information
Showing
11 changed files
with
505 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2024 tsuru authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package fake | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
type FakeRepository struct { | ||
CreatedRepos map[string]bool | ||
RepoExists map[string]bool | ||
AuthSuccess bool | ||
} | ||
|
||
func (f *FakeRepository) Auth(ctx context.Context) error { | ||
if f.AuthSuccess { | ||
return nil | ||
} | ||
return errors.New("auth repository failed") | ||
} | ||
|
||
func (f *FakeRepository) Create(ctx context.Context, name string) error { | ||
if _, exists := f.RepoExists[name]; exists { | ||
return errors.New("repository already exists") | ||
} | ||
if f.CreatedRepos == nil { | ||
f.CreatedRepos = make(map[string]bool) | ||
} | ||
if f.RepoExists == nil { | ||
f.RepoExists = make(map[string]bool) | ||
} | ||
f.CreatedRepos[name] = true | ||
f.RepoExists[name] = true | ||
return nil | ||
} | ||
|
||
func (f *FakeRepository) Exists(ctx context.Context, name string) (bool, error) { | ||
if f.RepoExists == nil { | ||
f.RepoExists = make(map[string]bool) | ||
} | ||
exists := f.RepoExists[name] | ||
return exists, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2024 tsuru authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package oci | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/oracle/oci-go-sdk/v65/artifacts" | ||
"github.com/oracle/oci-go-sdk/v65/common" | ||
) | ||
|
||
type OCIRequiredMethods interface { | ||
CreateContainerRepository(ctx context.Context, request artifacts.CreateContainerRepositoryRequest) (response artifacts.CreateContainerRepositoryResponse, err error) | ||
ListContainerRepositories(ctx context.Context, request artifacts.ListContainerRepositoriesRequest) (response artifacts.ListContainerRepositoriesResponse, err error) | ||
} | ||
|
||
type OCI struct { | ||
client OCIRequiredMethods | ||
CompartmentID string | ||
Profile string | ||
ConfigPath string | ||
} | ||
|
||
func NewOCI(data map[string]string) *OCI { | ||
return &OCI{ | ||
CompartmentID: data["compartmentID"], | ||
Profile: data["profile"], | ||
ConfigPath: data["configPath"], | ||
client: &artifacts.ArtifactsClient{}, | ||
} | ||
} | ||
|
||
func (r *OCI) Auth(ctx context.Context) error { | ||
if r.client != nil { | ||
return nil | ||
} | ||
configProvider := common.CustomProfileConfigProvider(r.ConfigPath, r.Profile) | ||
client, err := artifacts.NewArtifactsClientWithConfigurationProvider(configProvider) | ||
r.client = &client | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
} | ||
|
||
func (r *OCI) Create(ctx context.Context, name string) error { | ||
name, err := parserRegistryRepository(name) | ||
if err != nil { | ||
return err | ||
} | ||
request := artifacts.CreateContainerRepositoryRequest{ | ||
CreateContainerRepositoryDetails: artifacts.CreateContainerRepositoryDetails{ | ||
CompartmentId: &r.CompartmentID, | ||
DisplayName: common.String(name), | ||
}, | ||
} | ||
_, err = r.client.CreateContainerRepository(ctx, request) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (r *OCI) Exists(ctx context.Context, name string) (bool, error) { | ||
name, err := parserRegistryRepository(name) | ||
if err != nil { | ||
return false, err | ||
} | ||
request := artifacts.ListContainerRepositoriesRequest{ | ||
CompartmentId: &r.CompartmentID, | ||
DisplayName: common.String(name), | ||
} | ||
response, err := r.client.ListContainerRepositories(ctx, request) | ||
if err != nil { | ||
return false, err | ||
} | ||
if len(response.ContainerRepositoryCollection.Items) == 0 { | ||
return false, nil | ||
} | ||
return true, nil | ||
} | ||
|
||
func parserRegistryRepository(image string) (string, error) { | ||
parts := strings.Split(image, "/") | ||
if len(parts) < 3 { | ||
return "", fmt.Errorf("invalid image format %s", image) | ||
} | ||
repoWithTag := strings.Join(parts[2:], "/") | ||
repoParts := strings.Split(repoWithTag, ":") | ||
repoWithoutTag := repoParts[0] | ||
|
||
return repoWithoutTag, nil | ||
} |
Oops, something went wrong.