@@ -10,15 +10,15 @@ import (
1010
1111func NewJsonMetaRepo (repositoryPath string ) * JsonMetaRepo {
1212 return & JsonMetaRepo {
13- localDataFile : localDataFile (repositoryPath ),
14- metaDataFile : metaDataFile (repositoryPath ),
13+ localDataFile : localDataFile (repositoryPath ),
14+ sharedDataFile : sharedDataFile (repositoryPath ),
1515 }
1616}
1717
1818// A meta repo that stores meta data in JSON files in the specified directory.
1919type JsonMetaRepo struct {
20- localDataFile string
21- metaDataFile string
20+ localDataFile string
21+ sharedDataFile string
2222}
2323
2424/* Local repositories */
@@ -43,18 +43,27 @@ func (repo *JsonMetaRepo) AddLocals(localPaths []string) error {
4343}
4444
4545func (repo * JsonMetaRepo ) addLocal (localPath string ) error {
46- return updateFile (repo .localDataFile , func (rootObject * rootObjectData ) {
47- rootObject .MetaRepo .AppendLocalRepository (localRepositoryData {Path : localPath })
48- })
46+ return updateFile (
47+ repo .localDataFile ,
48+ ReadLocalMetaRepoFile ,
49+ func (rootObject * localRootObjectData ) {
50+ rootObject .MetaRepo .AppendLocalRepository (localRepositoryData {Path : localPath })
51+ },
52+ func (rootObject * localRootObjectData , dataFile string ) error {
53+ return rootObject .WriteTo (dataFile )
54+ },
55+ )
4956}
5057
5158func (repo * JsonMetaRepo ) ListLocal () (core.Repositories , error ) {
5259 return queryFile (
5360 repo .localDataFile ,
5461 core .NoRepositories (),
55- func (rootObject * rootObjectData ) (core.Repositories , error ) {
62+ ReadLocalMetaRepoFile ,
63+ func (rootObject * localRootObjectData ) (core.Repositories , error ) {
5664 return rootObject .MetaRepo .MapLocalRepositories ()
57- })
65+ },
66+ )
5867}
5968
6069/* Remote repositories */
@@ -79,28 +88,37 @@ func (repo *JsonMetaRepo) AddRemotes(hostUrls []*url.URL) error {
7988}
8089
8190func (repo * JsonMetaRepo ) addRemote (hostUrl * url.URL ) error {
82- return updateFile (repo .metaDataFile , func (rootObject * rootObjectData ) {
83- rootObject .MetaRepo .AppendRemoteRepository (remoteRepositoryData {Url : hostUrl .String ()})
84- })
91+ return updateFile (
92+ repo .sharedDataFile ,
93+ ReadSharedMetaRepoFile ,
94+ func (rootObject * sharedRootObjectData ) {
95+ rootObject .MetaRepo .AppendRemoteRepository (remoteRepositoryData {Url : hostUrl .String ()})
96+ },
97+ func (rootObject * sharedRootObjectData , dataFile string ) error {
98+ return rootObject .WriteTo (dataFile )
99+ },
100+ )
85101}
86102
87103func (repo * JsonMetaRepo ) ListRemote () (core.Repositories , error ) {
88104 return queryFile (
89- repo .metaDataFile ,
105+ repo .sharedDataFile ,
90106 core .NoRepositories (),
91- func (rootObject * rootObjectData ) (core.Repositories , error ) {
107+ ReadSharedMetaRepoFile ,
108+ func (rootObject * sharedRootObjectData ) (core.Repositories , error ) {
92109 return rootObject .MetaRepo .MapRemoteRepositories ()
93110 })
94111}
95112
96113/* I/O */
97114
98- func queryFile [V any ](
115+ func queryFile [RootObject any , Result any ](
99116 dataFile string ,
100- defaultValue V ,
101- queryData func (* rootObjectData ) (V , error ),
102- ) (V , error ) {
103- if rootObject , readErr := ReadMetaRepoFile (dataFile ); readErr != nil {
117+ defaultValue Result ,
118+ fetchData func (string ) (RootObject , error ),
119+ queryData func (RootObject ) (Result , error ),
120+ ) (Result , error ) {
121+ if rootObject , readErr := fetchData (dataFile ); readErr != nil {
104122 return defaultValue , fmt .Errorf ("failed to read file %s; %w" , dataFile , readErr )
105123 } else if result , queryErr := queryData (rootObject ); queryErr != nil {
106124 return defaultValue , fmt .Errorf ("failed to query data; %w" , queryErr )
@@ -109,17 +127,20 @@ func queryFile[V any](
109127 }
110128}
111129
112- type updateDataFn = func (* rootObjectData )
113-
114- func updateFile (dataFile string , updateData updateDataFn ) error {
115- var rootObject * rootObjectData
116- rootObject , readErr := ReadMetaRepoFile (dataFile )
130+ func updateFile [RootObject any ](
131+ dataFile string ,
132+ fetchData func (string ) (RootObject , error ),
133+ updateData func (RootObject ),
134+ writeData func (RootObject , string ) error ,
135+ ) error {
136+ var rootObject RootObject
137+ rootObject , readErr := fetchData (dataFile )
117138 if readErr != nil {
118139 return fmt .Errorf ("failed to read file %s; %w" , dataFile , readErr )
119140 }
120141
121142 updateData (rootObject )
122- if writeErr := rootObject . WriteTo ( dataFile ); writeErr != nil {
143+ if writeErr := writeData ( rootObject , dataFile ); writeErr != nil {
123144 return fmt .Errorf ("failed to write file %s; %w" , dataFile , writeErr )
124145 } else {
125146 return nil
0 commit comments