]> git.lizzy.rs Git - micro.git/blob - cmd/micro/clean.go
Merge branch 'diff-gutter' of https://github.com/p-e-w/micro
[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, e := os.Open(fname)
95
96                         if e == nil {
97                                 defer file.Close()
98
99                                 decoder := gob.NewDecoder(file)
100                                 err = decoder.Decode(&buffer)
101
102                                 if err != nil && f.Name() != "history" {
103                                         badFiles = append(badFiles, fname)
104                                 }
105                         }
106                 }
107
108                 if len(badFiles) > 0 {
109                         fmt.Printf("Detected %d files with an invalid format in %s\n", len(badFiles), filepath.Join(config.ConfigDir, "buffers"))
110                         fmt.Println("These files store cursor and undo history.")
111                         fmt.Printf("Removing badly formatted files in %s\n", filepath.Join(config.ConfigDir, "buffers"))
112
113                         if shouldContinue() {
114                                 for _, f := range badFiles {
115                                         err := os.Remove(f)
116                                         if err != nil {
117                                                 fmt.Println(err)
118                                                 continue
119                                         }
120                                 }
121
122                                 fmt.Println("Removed badly formatted files")
123                                 fmt.Print("\n\n")
124                         }
125                 }
126         }
127
128         // detect plugins/ directory
129         plugins := filepath.Join(config.ConfigDir, "plugins")
130         if stat, err := os.Stat(plugins); err == nil && stat.IsDir() {
131                 fmt.Printf("Found directory %s\n", plugins)
132                 fmt.Printf("Plugins should now be stored in %s\n", filepath.Join(config.ConfigDir, "plug"))
133                 fmt.Printf("Removing %s\n", plugins)
134
135                 if shouldContinue() {
136                         os.RemoveAll(plugins)
137                 }
138
139                 fmt.Print("\n\n")
140         }
141
142         fmt.Println("Done cleaning")
143 }