@@ -2,12 +2,14 @@ package cmd
22
33import  (
44	"fmt" 
5+ 	"io/ioutil" 
56	"log" 
67	"net/url" 
78	"os" 
89	"path/filepath" 
910	"strings" 
1011
12+ 	"github.com/manifoldco/promptui" 
1113	"github.com/spf13/cobra" 
1214	"github.com/uyuni-project/minima/get" 
1315	yaml "gopkg.in/yaml.v2" 
@@ -138,6 +140,8 @@ func syncersFromConfig(configString string) (result []*get.Syncer, err error) {
138140		}
139141	}
140142
143+ 
144+ 
141145	for  _ , httpRepo  :=  range  config .HTTP  {
142146		repoURL , err  :=  url .Parse (httpRepo .URL )
143147		if  err  !=  nil  {
@@ -166,7 +170,9 @@ func syncersFromConfig(configString string) (result []*get.Syncer, err error) {
166170		}
167171		result  =  append (result , get .NewSyncer (* repoURL , archs , storage ))
168172	}
169- 
173+ 	if  cleanup  {
174+ 		RemoveOldChannelsFromFileStorage (config )
175+ 	}
170176	return 
171177}
172178
@@ -176,3 +182,66 @@ func init() {
176182	RootCmd .PersistentFlags ().StringVarP (& archs , "arch" , "a" , "" , "flag that specifies covered archs in the given repo" )
177183	RootCmd .PersistentFlags ().BoolVarP (& syncLegacyPackages , "legacypackages" , "l" , false , "flag that triggers mirroring of i586 pkgs in x86_64 repos" )
178184}
185+ 
186+ func  RemoveOldChannelsFromFileStorage  (config  Config ) (err  error ) {
187+ 		// DO CLEANUP - TO BE IMPLEMENTED 
188+ 		log .Println ("searching for outdated repositories..." )
189+ 		//fmt.Printf("List of Repos from config: %s  ---> %s\n", config.SCC.RepoNames, config.HTTP) 
190+ 		mappedRepos  :=  make (map [string ]bool )
191+ 		var  urlink  * url.URL 
192+ 		for  _ , elem  :=  range  config .HTTP  {
193+ 			urlink , err  =  url .Parse (elem .URL )
194+ 			if  err  !=  nil  {
195+ 				panic (err )
196+ 			}
197+ 			mappedRepos [filepath .Join (config .Storage .Path , urlink .Path )] =  true 
198+ 		}
199+ 			//fmt.Printf("MAPPED REPOS: %v\n", mappedRepos) 
200+ 			path  :=  config .Storage .Path 
201+ 			muChannelList  :=  make (map [string ]bool )
202+ 			filepath .Walk (path , func (path  string , info  os.FileInfo , err  error ) error  {
203+ 				if  info .IsDir () {
204+ 					if  info .Name () ==  "repodata"  {
205+ 						muChannelList [strings .Replace (path , "/repodata" , "" , 1 )] =  true 
206+ 						return  nil 
207+ 					}
208+ 					if  files , err  :=  ioutil .ReadDir (path ); len (files ) ==  0  &&  path  !=  config .Storage .Path  {
209+ 						if  err  !=  nil  {
210+ 							log .Fatal (err )
211+ 						}
212+ 						log .Printf ("Removing unused empty folders: %s\n " , path )
213+ 						os .RemoveAll (path )
214+ 					}
215+ 				}
216+ 				return  nil 
217+ 			})
218+ 			//fmt.Printf("CHANNEL LIST: %v\n", muChannelList) 
219+ 			for  ind , _  :=  range  muChannelList  {
220+ 				if  mappedRepos [ind ] {
221+ 					log .Printf ("Repo %s is registered...\n " , ind )
222+ 				} else  {
223+ 					log .Printf ("Repo %s is not registered in the yaml file..." , ind )
224+ 					if  autoApprove  {
225+ 						log .Printf ("Removing repo %s ...\n " , ind )
226+ 						os .RemoveAll (ind )
227+ 					} else  {
228+ 						prompt  :=  promptui.Select {
229+ 							Label : fmt .Sprintf ("Delete repo: %s ??? [Yes/No]" , ind ),
230+ 							Items : []string {"Yes" , "No" },
231+ 						}
232+ 						_ , result , err  :=  prompt .Run ()
233+ 						if  err  !=  nil  {
234+ 							log .Fatalf ("Prompt failed %v\n " , err )
235+ 						}
236+ 						if  result  ==  "Yes"  {
237+ 							log .Printf ("Removing repo: %s ...\n " , ind )
238+ 							os .RemoveAll (ind )
239+ 						} else  {
240+ 							log .Printf ("Keeping repo: %s ...\n " , ind )
241+ 						}
242+ 					}
243+ 				}
244+ 			}
245+ 		log .Println ("...done!" )
246+ 	return 
247+ }
0 commit comments