]> git.lizzy.rs Git - micro.git/blob - cmd/micro/clean.go
6ec5455f5ae303ea740c5ce5968c60935a42e9aa
[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/v2/internal/buffer"
14         "github.com/zyedidia/micro/v2/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         fmt.Println("Cleaning default settings")
45         config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
46
47         // detect unused options
48         var unusedOptions []string
49         defaultSettings := config.DefaultAllSettings()
50         for k := range config.GlobalSettings {
51                 if _, ok := defaultSettings[k]; !ok {
52                         valid := false
53                         for _, p := range config.Plugins {
54                                 if strings.HasPrefix(k, p.Name+".") || k == p.Name {
55                                         valid = true
56                                 }
57                         }
58                         if !valid {
59                                 unusedOptions = append(unusedOptions, k)
60                         }
61                 }
62         }
63
64         if len(unusedOptions) > 0 {
65                 fmt.Println("The following options are unused:")
66
67                 sort.Strings(unusedOptions)
68
69                 for _, s := range unusedOptions {
70                         fmt.Printf("%s (value: %v)\n", s, config.GlobalSettings[s])
71                 }
72
73                 fmt.Printf("These options will be removed from %s\n", filepath.Join(config.ConfigDir, "settings.json"))
74
75                 if shouldContinue() {
76                         for _, s := range unusedOptions {
77                                 delete(config.GlobalSettings, s)
78                         }
79
80                         err := config.OverwriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
81                         if err != nil {
82                                 fmt.Println("Error writing settings.json file: " + err.Error())
83                         }
84
85                         fmt.Println("Removed unused options")
86                         fmt.Print("\n\n")
87                 }
88         }
89
90         // detect incorrectly formatted buffer/ files
91         files, err := ioutil.ReadDir(filepath.Join(config.ConfigDir, "buffers"))
92         if err == nil {
93                 var badFiles []string
94                 var buffer buffer.SerializedBuffer
95                 for _, f := range files {
96                         fname := filepath.Join(config.ConfigDir, "buffers", f.Name())
97                         file, e := os.Open(fname)
98
99                         if e == nil {
100                                 decoder := gob.NewDecoder(file)
101                                 err = decoder.Decode(&buffer)
102
103                                 if err != nil && f.Name() != "history" {
104                                         badFiles = append(badFiles, fname)
105                                 }
106                                 file.Close()
107                         }
108                 }
109
110                 if len(badFiles) > 0 {
111                         fmt.Printf("Detected %d files with an invalid format in %s\n", len(badFiles), filepath.Join(config.ConfigDir, "buffers"))
112                         fmt.Println("These files store cursor and undo history.")
113                         fmt.Printf("Removing badly formatted files in %s\n", filepath.Join(config.ConfigDir, "buffers"))
114
115                         if shouldContinue() {
116                                 removed := 0
117                                 for _, f := range badFiles {
118                                         err := os.Remove(f)
119                                         if err != nil {
120                                                 fmt.Println(err)
121                                                 continue
122                                         }
123                                         removed++
124                                 }
125
126                                 if removed == 0 {
127                                         fmt.Println("Failed to remove files")
128                                 } else {
129                                         fmt.Printf("Removed %d badly formatted files\n", removed)
130                                 }
131                                 fmt.Print("\n\n")
132                         }
133                 }
134         }
135
136         // detect plugins/ directory
137         plugins := filepath.Join(config.ConfigDir, "plugins")
138         if stat, err := os.Stat(plugins); err == nil && stat.IsDir() {
139                 fmt.Printf("Found directory %s\n", plugins)
140                 fmt.Printf("Plugins should now be stored in %s\n", filepath.Join(config.ConfigDir, "plug"))
141                 fmt.Printf("Removing %s\n", plugins)
142
143                 if shouldContinue() {
144                         os.RemoveAll(plugins)
145                 }
146
147                 fmt.Print("\n\n")
148         }
149
150         fmt.Println("Done cleaning")
151 }