]> git.lizzy.rs Git - micro.git/commitdiff
Use filepath.Join more
authorZachary Yedidia <zyedidia@gmail.com>
Tue, 11 Feb 2020 18:09:17 +0000 (13:09 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Tue, 11 Feb 2020 18:09:17 +0000 (13:09 -0500)
internal/action/bindings.go
internal/action/command.go
internal/buffer/backup.go
internal/buffer/buffer.go
internal/buffer/serialize.go
internal/config/config.go
internal/config/settings.go
internal/info/history.go

index b5667b77d0602d50dd9ddfe724af8b109b7ccdd9..73f17886b43a5e678cca7f5f97ab8a764baca80e 100644 (file)
@@ -5,6 +5,7 @@ import (
        "errors"
        "io/ioutil"
        "os"
+       "path/filepath"
        "strings"
        "unicode"
 
@@ -27,7 +28,7 @@ func InitBindings() {
        var parsed map[string]string
        defaults := DefaultBindings()
 
-       filename := config.ConfigDir + "/bindings.json"
+       filename := filepath.Join(config.ConfigDir, "bindings.json")
        createBindingsIfNotExist(filename)
 
        if _, e := os.Stat(filename); e == nil {
@@ -167,7 +168,7 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
        var e error
        var parsed map[string]string
 
-       filename := config.ConfigDir + "/bindings.json"
+       filename := filepath.Join(config.ConfigDir, "bindings.json")
        createBindingsIfNotExist(filename)
        if _, e = os.Stat(filename); e == nil {
                input, err := ioutil.ReadFile(filename)
@@ -217,7 +218,7 @@ func UnbindKey(k string) error {
        var e error
        var parsed map[string]string
 
-       filename := config.ConfigDir + "/bindings.json"
+       filename := filepath.Join(config.ConfigDir, "bindings.json")
        createBindingsIfNotExist(filename)
        if _, e = os.Stat(filename); e == nil {
                input, err := ioutil.ReadFile(filename)
index 3a445caa0a64e0aa76dadb118834147e2a838f18..c92b782c0331529fca20e5f091c59f919351a7cb 100644 (file)
@@ -475,7 +475,7 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
                b.SetOptionNative(option, nativeValue)
        }
 
-       return config.WriteSettings(config.ConfigDir + "/settings.json")
+       return config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
 }
 
 func SetGlobalOption(option, value string) error {
index 192ab63832384af11d4194a3a7c963ba5482f04f..56308843e87598d9d87ae1fecc527f7ecda5f65e 100644 (file)
@@ -4,6 +4,7 @@ import (
        "fmt"
        "io"
        "os"
+       "path/filepath"
        "time"
 
        "github.com/zyedidia/micro/internal/config"
@@ -42,12 +43,12 @@ func (b *Buffer) Backup(checkTime bool) error {
 
        b.lastbackup = time.Now()
 
-       backupdir := config.ConfigDir + "/backups/"
+       backupdir := filepath.Join(config.ConfigDir, "backups")
        if _, err := os.Stat(backupdir); os.IsNotExist(err) {
                os.Mkdir(backupdir, os.ModePerm)
        }
 
-       name := backupdir + util.EscapePath(b.AbsPath)
+       name := filepath.Join(backupdir, util.EscapePath(b.AbsPath))
 
        err := overwriteFile(name, encoding.Nop, func(file io.Writer) (e error) {
                if len(b.lines) == 0 {
@@ -81,7 +82,7 @@ func (b *Buffer) RemoveBackup() {
        if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault {
                return
        }
-       f := config.ConfigDir + "/backups/" + util.EscapePath(b.AbsPath)
+       f := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
        os.Remove(f)
 }
 
@@ -89,7 +90,7 @@ func (b *Buffer) RemoveBackup() {
 // Returns true if a backup was applied
 func (b *Buffer) ApplyBackup(fsize int64) bool {
        if b.Settings["backup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
-               backupfile := config.ConfigDir + "/backups/" + util.EscapePath(b.AbsPath)
+               backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
                if info, err := os.Stat(backupfile); err == nil {
                        backup, err := os.Open(backupfile)
                        if err == nil {
index 402aeffa782996e97aa4e517f94ae292bb88ba11..a57a228b2b2b095b83eee2feaa52bf88d4bbf678 100644 (file)
@@ -272,8 +272,8 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
        b.UpdateRules()
        config.InitLocalSettings(b.Settings, b.Path)
 
-       if _, err := os.Stat(config.ConfigDir + "/buffers/"); os.IsNotExist(err) {
-               os.Mkdir(config.ConfigDir+"/buffers/", os.ModePerm)
+       if _, err := os.Stat(filepath.Join(config.ConfigDir, "buffers")); os.IsNotExist(err) {
+               os.Mkdir(filepath.Join(config.ConfigDir, "buffers"), os.ModePerm)
        }
 
        if startcursor.X != -1 && startcursor.Y != -1 {
index b11b7322ce9aa1c0d4392b891ecb84f4fae8f4ef..79c21f6e64a765e9d65ffc823ad7651e42070c19 100644 (file)
@@ -31,7 +31,7 @@ func (b *Buffer) Serialize() error {
                return nil
        }
 
-       name := config.ConfigDir + "/buffers/" + util.EscapePath(b.AbsPath)
+       name := filepath.Join(config.ConfigDir, "buffers", util.EscapePath(b.AbsPath))
 
        return overwriteFile(name, encoding.Nop, func(file io.Writer) error {
                err := gob.NewEncoder(file).Encode(SerializedBuffer{
index 17d9311d07ff197eed0d57ddbfe32451b2bb6ad2..d2ceb058d677a2c5bcb79770dc10351e8a5983f4 100644 (file)
@@ -3,6 +3,7 @@ package config
 import (
        "errors"
        "os"
+       "path/filepath"
 
        homedir "github.com/mitchellh/go-homedir"
 )
@@ -24,10 +25,10 @@ func InitConfigDir(flagConfigDir string) error {
                        if err != nil {
                                return errors.New("Error finding your home directory\nCan't load config files: " + err.Error())
                        }
-                       xdgHome = home + "/.config"
+                       xdgHome = filepath.Join(home, ".config")
                }
 
-               microHome = xdgHome + "/micro"
+               microHome = filepath.Join(xdgHome, "micro")
        }
        ConfigDir = microHome
 
index 6b93d1172a4ca7662dbad8b68cb471975d9a87b1..a9e8b70c0497ec3e154ac076ca022c862a360e84 100644 (file)
@@ -147,7 +147,7 @@ func RegisterCommonOptionPlug(pl string, name string, defaultvalue interface{})
        if v, ok := GlobalSettings[name]; !ok {
                defaultCommonSettings[name] = defaultvalue
                GlobalSettings[name] = defaultvalue
-               err := WriteSettings(filepath.Join(ConfigDir, "/settings.json"))
+               err := WriteSettings(filepath.Join(ConfigDir, "settings.json"))
                if err != nil {
                        return errors.New("Error writing settings.json file: " + err.Error())
                }
index e6d03fee79a3d05d7dda9df3e7bf55a241d29860..8668de1087d224067286ae7186e023cf9c5ea1fa 100644 (file)
@@ -3,6 +3,7 @@ package info
 import (
        "encoding/gob"
        "os"
+       "path/filepath"
 
        "github.com/zyedidia/micro/internal/config"
 )
@@ -12,7 +13,7 @@ import (
 // The savehistory option must be on
 func (i *InfoBuf) LoadHistory() {
        if config.GetGlobalOption("savehistory").(bool) {
-               file, err := os.Open(config.ConfigDir + "/buffers/history")
+               file, err := os.Open(filepath.Join(config.ConfigDir, "buffers", "history"))
                defer file.Close()
                var decodedMap map[string][]string
                if err == nil {
@@ -46,7 +47,7 @@ func (i *InfoBuf) SaveHistory() {
                        }
                }
 
-               file, err := os.Create(config.ConfigDir + "/buffers/history")
+               file, err := os.Create(filepath.Join(config.ConfigDir, "buffers", "history"))
                defer file.Close()
                if err == nil {
                        encoder := gob.NewEncoder(file)