]> git.lizzy.rs Git - micro.git/blob - cmd/micro/clean.go
remove carriage return from -clean prompt and fix broken logic (#2186)
[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     text = strings.TrimRight(text, "\r\n")
27
28         return len(text) == 0 || strings.ToLower(text)[0] == 'y'
29 }
30
31 // CleanConfig performs cleanup in the user's configuration directory
32 func CleanConfig() {
33         fmt.Println("Cleaning your configuration directory at", config.ConfigDir)
34         fmt.Printf("Please consider backing up %s before continuing\n", config.ConfigDir)
35
36         if !shouldContinue() {
37                 fmt.Println("Stopping early")
38                 return
39         }
40
41         fmt.Println("Cleaning default settings")
42         config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
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, e := os.Open(fname)
95
96                         if e == nil {
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                                 file.Close()
104                         }
105                 }
106
107                 if len(badFiles) > 0 {
108                         fmt.Printf("Detected %d files with an invalid format in %s\n", len(badFiles), filepath.Join(config.ConfigDir, "buffers"))
109                         fmt.Println("These files store cursor and undo history.")
110                         fmt.Printf("Removing badly formatted files in %s\n", filepath.Join(config.ConfigDir, "buffers"))
111
112                         if shouldContinue() {
113                                 removed := 0
114                                 for _, f := range badFiles {
115                                         err := os.Remove(f)
116                                         if err != nil {
117                                                 fmt.Println(err)
118                                                 continue
119                                         }
120                                         removed++
121                                 }
122
123                                 if removed == 0 {
124                                         fmt.Println("Failed to remove files")
125                                 } else {
126                                         fmt.Printf("Removed %d badly formatted files\n", removed)
127                                 }
128                                 fmt.Print("\n\n")
129                         }
130                 }
131         }
132
133         // detect plugins/ directory
134         plugins := filepath.Join(config.ConfigDir, "plugins")
135         if stat, err := os.Stat(plugins); err == nil && stat.IsDir() {
136                 fmt.Printf("Found directory %s\n", plugins)
137                 fmt.Printf("Plugins should now be stored in %s\n", filepath.Join(config.ConfigDir, "plug"))
138                 fmt.Printf("Removing %s\n", plugins)
139
140                 if shouldContinue() {
141                         os.RemoveAll(plugins)
142                 }
143
144                 fmt.Print("\n\n")
145         }
146
147         fmt.Println("Done cleaning")
148 }