|
| 1 | +package prune |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "github.com/Driver-C/tryssh/config" |
| 7 | + "github.com/Driver-C/tryssh/launcher" |
| 8 | + "github.com/Driver-C/tryssh/launcher/ssh" |
| 9 | + "github.com/Driver-C/tryssh/utils" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type Controller struct { |
| 17 | + configuration *config.MainConfig |
| 18 | + auto bool |
| 19 | + sshTimeout time.Duration |
| 20 | + concurrency int |
| 21 | +} |
| 22 | + |
| 23 | +func (pc *Controller) PruneCaches() { |
| 24 | + newServerList := make([]config.ServerListConfig, 0) |
| 25 | + if pc.auto { |
| 26 | + newServerList = pc.concurrencyDeleteCache() |
| 27 | + } else { |
| 28 | + for _, server := range pc.configuration.ServerLists { |
| 29 | + lan := &ssh.Launcher{SshConnector: *launcher.GetSshConnectorFromConfig(&server)} |
| 30 | + // Set timeout |
| 31 | + lan.SshTimeout = pc.sshTimeout |
| 32 | + // Determine if connection is possible |
| 33 | + if err := lan.TryToConnect(); err != nil { |
| 34 | + if !pc.interactiveDeleteCache(server) { |
| 35 | + newServerList = append(newServerList, server) |
| 36 | + } |
| 37 | + } else { |
| 38 | + utils.Logger.Infof("Cache %v is still available.", server) |
| 39 | + newServerList = append(newServerList, server) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + pc.configuration.ServerLists = newServerList |
| 44 | + if config.UpdateConfig(pc.configuration) { |
| 45 | + utils.Logger.Infoln("Update config successful.") |
| 46 | + } else { |
| 47 | + utils.Logger.Errorln("Update config failed.") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (pc *Controller) interactiveDeleteCache(server config.ServerListConfig) bool { |
| 52 | + reader := bufio.NewReader(os.Stdin) |
| 53 | + for { |
| 54 | + fmt.Printf("Are you sure you want to delete this cache? "+ |
| 55 | + "Please enter \"yes\" to confirm deletion, or \"no\" to cancel. %s\n"+ |
| 56 | + "(yes/no): ", server) |
| 57 | + stdin, _ := reader.ReadString('\n') |
| 58 | + // Delete space |
| 59 | + stdin = strings.TrimSpace(stdin) |
| 60 | + switch stdin { |
| 61 | + case "yes": |
| 62 | + utils.Logger.Infof("The cache %v has been marked for deletion.", server) |
| 63 | + return true |
| 64 | + case "no": |
| 65 | + utils.Logger.Infof("Cache %v skipped.", server) |
| 66 | + return false |
| 67 | + default: |
| 68 | + utils.Logger.Errorln("Input error:", stdin) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func (pc *Controller) concurrencyDeleteCache() []config.ServerListConfig { |
| 74 | + newServerList := make([]config.ServerListConfig, 0) |
| 75 | + launchersChan := make(chan *ssh.Launcher) |
| 76 | + var mutex sync.Mutex |
| 77 | + var wg sync.WaitGroup |
| 78 | + |
| 79 | + go func(launchersChan chan<- *ssh.Launcher) { |
| 80 | + for _, server := range pc.configuration.ServerLists { |
| 81 | + lan := &ssh.Launcher{SshConnector: *launcher.GetSshConnectorFromConfig(&server)} |
| 82 | + launchersChan <- lan |
| 83 | + } |
| 84 | + close(launchersChan) |
| 85 | + }(launchersChan) |
| 86 | + |
| 87 | + for i := 0; i < pc.concurrency; i++ { |
| 88 | + wg.Add(1) |
| 89 | + go func(launchersChan <-chan *ssh.Launcher, wg *sync.WaitGroup) { |
| 90 | + defer wg.Done() |
| 91 | + for { |
| 92 | + launcherP, ok := <-launchersChan |
| 93 | + if !ok { |
| 94 | + break |
| 95 | + } |
| 96 | + server := launcher.GetConfigFromSshConnector(&launcherP.SshConnector) |
| 97 | + launcherP.SshTimeout = pc.sshTimeout |
| 98 | + if err := launcherP.TryToConnect(); err == nil { |
| 99 | + utils.Logger.Infof("Cache %v is still available.", server) |
| 100 | + mutex.Lock() |
| 101 | + newServerList = append(newServerList, *server) |
| 102 | + mutex.Unlock() |
| 103 | + } else { |
| 104 | + utils.Logger.Infof("The cache %v has been marked for deletion.", server) |
| 105 | + } |
| 106 | + } |
| 107 | + }(launchersChan, &wg) |
| 108 | + } |
| 109 | + wg.Wait() |
| 110 | + return newServerList |
| 111 | +} |
| 112 | + |
| 113 | +func NewPruneController(configuration *config.MainConfig, auto bool, timeout time.Duration, |
| 114 | + concurrency int) *Controller { |
| 115 | + return &Controller{ |
| 116 | + configuration: configuration, |
| 117 | + auto: auto, |
| 118 | + sshTimeout: timeout, |
| 119 | + concurrency: concurrency, |
| 120 | + } |
| 121 | +} |
0 commit comments