]> git.lizzy.rs Git - micro.git/blob - cmd/micro/clean.go
Merge pull request #1321 from zonuexe/add/php-fn-keyword
[micro.git] / cmd / micro / clean.go
1 package main
2
3 import (
4         "bufio"
5         "encoding/gob"
6         "fmt"
7         "io/ioutil"
8         "os"
9         "path/filepath"
10         "sort"
11         "strings"
12
13         "github.com/zyedidia/micro/internal/buffer"
14         "github.com/zyedidia/micro/internal/config"
15 )
16
17 func shouldContinue() bool {
18         reader := bufio.NewReader(os.Stdin)
19         fmt.Print("Continue [Y/n]: ")
20         text, err := reader.ReadString('\n')
21         if err != nil {
22                 fmt.Println(err)
23                 return false
24         }
25
26         if len(text) <= 1 {
27                 // default continue
28                 return true
29         }
30
31         return strings.ToLower(text)[0] == 'y'
32 }
33
34 // CleanConfig performs cleanup in the user's configuration directory
35 func CleanConfig() {
36         fmt.Println("Cleaning your configuration directory at", config.ConfigDir)
37         fmt.Printf("Please consider backing up %s before continuing\n", config.ConfigDir)
38
39         if !shouldContinue() {
40                 fmt.Println("Stopping early")
41                 return
42         }
43
44         // detect unused options
45         var unusedOptions []string
46         defaultSettings := config.DefaultAllSettings()
47         for k := range config.GlobalSettings {
48                 if _, ok := defaultSettings[k]; !ok {
49                         valid := false
50                         for _, p := range config.Plugins {
51                                 if strings.HasPrefix(k, p.Name+".") || k == p.Name {
52                                         valid = true
53                                 }
54                         }
55                         if !valid {
56                                 unusedOptions = append(unusedOptions, k)
57                         }
58                 }
59         }
60
61         if len(unusedOptions) > 0 {
62                 fmt.Println("The following options are unused:")
63
64                 sort.Strings(unusedOptions)
65
66                 for _, s := range unusedOptions {
67                         fmt.Printf("%s (value: %v)\n", s, config.GlobalSettings[s])
68                 }
69
70                 fmt.Printf("These options will be removed from %s\n", filepath.Join(config.ConfigDir, "settings.json"))
71
72                 if shouldContinue() {
73                         for _, s := range unusedOptions {
74                                 delete(config.GlobalSettings, s)
75                         }
76
77                         err := config.OverwriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
78                         if err != nil {
79                                 fmt.Println("Error writing settings.json file: " + err.Error())
80                         }
81
82                         fmt.Println("Removed unused options")
83                         fmt.Print("\n\n")
84                 }
85         }
86
87         // detect incorrectly formatted buffer/ files
88         files, err := ioutil.ReadDir(filepath.Join(config.ConfigDir, "buffers"))
89         if err == nil {
90                 var badFiles []string
91                 var buffer buffer.SerializedBuffer
92                 for _, f := range files {
93                         fname := filepath.Join(config.ConfigDir, "buffers", f.Name())
94                         file, err := os.Open(fname)
95                         defer file.Close()
96
97                         decoder := gob.NewDecoder(file)
98                         err = decoder.Decode(&buffer)
99
100                         if err != nil && f.Name() != "history" {
101                                 badFiles = append(badFiles, fname)
102                         }
103                 }
104
105                 if len(badFiles) > 0 {
106                         fmt.Printf("Detected %d files with an invalid format in %s\n", len(badFiles), filepath.Join(config.ConfigDir, "buffers"))
107                         fmt.Println("These files store cursor and undo history.")
108                         fmt.Printf("Removing badly formatted files in %s\n", filepath.Join(config.ConfigDir, "buffers"))
109
110                         if shouldContinue() {
111                                 for _, f := range badFiles {
112                                         err := os.Remove(f)
113                                         if err != nil {
114                                                 fmt.Println(err)
115                                                 continue
116                                         }
117                                 }
118
119                                 fmt.Println("Removed badly formatted files")
120                                 fmt.Print("\n\n")
121                         }
122                 }
123         }
124
125         // detect plugins/ directory
126         plugins := filepath.Join(config.ConfigDir, "plugins")
127         if stat, err := os.Stat(plugins); err == nil && stat.IsDir() {
128                 fmt.Printf("Found directory %s\n", plugins)
129                 fmt.Printf("Plugins should now be stored in %s\n", filepath.Join(config.ConfigDir, "plug"))
130                 fmt.Printf("Removing %s\n", plugins)
131
132                 if shouldContinue() {
133                         os.RemoveAll(plugins)
134                 }
135
136                 fmt.Print("\n\n")
137         }
138
139         fmt.Println("Done cleaning")
140 }