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