From b92adfcb5f79f2b32c3dafb0fc3e7f1b753b6197 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 8 Dec 2019 21:15:06 +0100 Subject: bridge: huge refactor to accept multiple kind of credentials --- repository/config_mem.go | 26 ++++++++++++++------------ repository/mock_repo.go | 23 ++++++++++++----------- repository/repo.go | 16 ++++++++++------ 3 files changed, 36 insertions(+), 29 deletions(-) (limited to 'repository') diff --git a/repository/config_mem.go b/repository/config_mem.go index bd680d03..5ce577ac 100644 --- a/repository/config_mem.go +++ b/repository/config_mem.go @@ -6,30 +6,32 @@ import ( "time" ) -var _ Config = &memConfig{} +var _ Config = &MemConfig{} -type memConfig struct { +type MemConfig struct { config map[string]string } -func newMemConfig(config map[string]string) *memConfig { - return &memConfig{config: config} +func NewMemConfig() *MemConfig { + return &MemConfig{ + config: make(map[string]string), + } } -func (mc *memConfig) StoreString(key, value string) error { +func (mc *MemConfig) StoreString(key, value string) error { mc.config[key] = value return nil } -func (mc *memConfig) StoreBool(key string, value bool) error { +func (mc *MemConfig) StoreBool(key string, value bool) error { return mc.StoreString(key, strconv.FormatBool(value)) } -func (mc *memConfig) StoreTimestamp(key string, value time.Time) error { +func (mc *MemConfig) StoreTimestamp(key string, value time.Time) error { return mc.StoreString(key, strconv.Itoa(int(value.Unix()))) } -func (mc *memConfig) ReadAll(keyPrefix string) (map[string]string, error) { +func (mc *MemConfig) ReadAll(keyPrefix string) (map[string]string, error) { result := make(map[string]string) for key, val := range mc.config { if strings.HasPrefix(key, keyPrefix) { @@ -39,7 +41,7 @@ func (mc *memConfig) ReadAll(keyPrefix string) (map[string]string, error) { return result, nil } -func (mc *memConfig) ReadString(key string) (string, error) { +func (mc *MemConfig) ReadString(key string) (string, error) { // unlike git, the mock can only store one value for the same key val, ok := mc.config[key] if !ok { @@ -49,7 +51,7 @@ func (mc *memConfig) ReadString(key string) (string, error) { return val, nil } -func (mc *memConfig) ReadBool(key string) (bool, error) { +func (mc *MemConfig) ReadBool(key string) (bool, error) { // unlike git, the mock can only store one value for the same key val, ok := mc.config[key] if !ok { @@ -59,7 +61,7 @@ func (mc *memConfig) ReadBool(key string) (bool, error) { return strconv.ParseBool(val) } -func (mc *memConfig) ReadTimestamp(key string) (time.Time, error) { +func (mc *MemConfig) ReadTimestamp(key string) (time.Time, error) { value, err := mc.ReadString(key) if err != nil { return time.Time{}, err @@ -74,7 +76,7 @@ func (mc *memConfig) ReadTimestamp(key string) (time.Time, error) { } // RmConfigs remove all key/value pair matching the key prefix -func (mc *memConfig) RemoveAll(keyPrefix string) error { +func (mc *MemConfig) RemoveAll(keyPrefix string) error { for key := range mc.config { if strings.HasPrefix(key, keyPrefix) { delete(mc.config, key) diff --git a/repository/mock_repo.go b/repository/mock_repo.go index 88c5a132..89d0f395 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -12,8 +12,8 @@ var _ ClockedRepo = &mockRepoForTest{} // mockRepoForTest defines an instance of Repo that can be used for testing. type mockRepoForTest struct { - config map[string]string - globalConfig map[string]string + config *MemConfig + globalConfig *MemConfig blobs map[git.Hash][]byte trees map[git.Hash]string commits map[git.Hash]commit @@ -29,24 +29,25 @@ type commit struct { func NewMockRepoForTest() *mockRepoForTest { return &mockRepoForTest{ - config: make(map[string]string), - blobs: make(map[git.Hash][]byte), - trees: make(map[git.Hash]string), - commits: make(map[git.Hash]commit), - refs: make(map[string]git.Hash), - createClock: lamport.NewClock(), - editClock: lamport.NewClock(), + config: NewMemConfig(), + globalConfig: NewMemConfig(), + blobs: make(map[git.Hash][]byte), + trees: make(map[git.Hash]string), + commits: make(map[git.Hash]commit), + refs: make(map[string]git.Hash), + createClock: lamport.NewClock(), + editClock: lamport.NewClock(), } } // LocalConfig give access to the repository scoped configuration func (r *mockRepoForTest) LocalConfig() Config { - return newMemConfig(r.config) + return r.config } // GlobalConfig give access to the git global configuration func (r *mockRepoForTest) GlobalConfig() Config { - return newMemConfig(r.globalConfig) + return r.globalConfig } // GetPath returns the path to the repo. diff --git a/repository/repo.go b/repository/repo.go index 71bd7a8e..e8517508 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -15,6 +15,15 @@ var ( ErrMultipleConfigEntry = errors.New("multiple config entry for the given key") ) +// RepoConfig access the configuration of a repository +type RepoConfig interface { + // LocalConfig give access to the repository scoped configuration + LocalConfig() Config + + // GlobalConfig give access to the git global configuration + GlobalConfig() Config +} + // RepoCommon represent the common function the we want all the repo to implement type RepoCommon interface { // GetPath returns the path to the repo. @@ -31,16 +40,11 @@ type RepoCommon interface { // GetRemotes returns the configured remotes repositories. GetRemotes() (map[string]string, error) - - // LocalConfig give access to the repository scoped configuration - LocalConfig() Config - - // GlobalConfig give access to the git global configuration - GlobalConfig() Config } // Repo represents a source code repository. type Repo interface { + RepoConfig RepoCommon // FetchRefs fetch git refs from a remote -- cgit