diff --git a/cmd/conf.go b/cmd/cfg.go similarity index 93% rename from cmd/conf.go rename to cmd/cfg.go index 855a261..a61dc30 100644 --- a/cmd/conf.go +++ b/cmd/cfg.go @@ -7,7 +7,7 @@ import ( var ( confCmd = &cobra.Command{ - Use: "conf", + Use: "cfg", Short: "change config", Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { diff --git a/conf/conf.go b/conf/conf.go index 3e48ae1..6fe0cf7 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -11,10 +11,9 @@ import ( ) const ( - Version = "0.1.1" - SepGroupInFile = "_" - SepInCmd = "," - + Version = "0.1.1" + SepGroupInFile = "_" + SepInCmd = "," BaseHostFileName = "base" HostFileExt = ".txt" OpEditor = "editor" @@ -93,7 +92,11 @@ func Sync() { } // todo writer interface - if err := cfg.SaveTo(ConfigFile); err != nil { + cfgFile, err := hfs.H.Create(ConfigFile) + if err != nil { + display.ErrExit(err) + } + if _, err = cfg.WriteTo(cfgFile); err != nil { display.ErrExit(err) } } diff --git a/conf/unix.go b/conf/unix.go index 2c6510f..1c202fa 100644 --- a/conf/unix.go +++ b/conf/unix.go @@ -3,15 +3,8 @@ package conf -import ( - "golang.org/x/text/encoding/unicode" -) - const ( - SysHost = "/etc/hosts" - NewLine = "\n" -) - -var ( - SysHostCharset = unicode.UTF8 + SysHost = "/etc/hosts" + NewLine = "\n" + DefaultEditor = "vim" ) diff --git a/conf/windows.go b/conf/windows.go index 51d299b..a680232 100644 --- a/conf/windows.go +++ b/conf/windows.go @@ -3,13 +3,8 @@ package conf -import "golang.org/x/text/encoding/charmap" - const ( - SysHost = "C:\\Windows\\System32\\drivers\\etc\\hosts" - NewLine = "\r\n" -) - -var ( - SysHostCharset = charmap.ISO8859_1 + SysHost = "C:\\Windows\\System32\\drivers\\etc\\hosts" + NewLine = "\r\n" + DefaultEditor = "notepad" ) diff --git a/hfs/hfs.go b/hfs/hfs.go index 4c32c82..ece794c 100644 --- a/hfs/hfs.go +++ b/hfs/hfs.go @@ -25,7 +25,6 @@ type Hfs interface { fs.FS fs.ReadDirFS fs.ReadFileFS - OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) WriteFile(path string, data []byte, perm os.FileMode) error MkdirAll(path string, perm os.FileMode) error Stat(path string) (fs.FileInfo, error) diff --git a/hfs/memfs.go b/hfs/memfs.go index 1794a43..2d2a2a0 100644 --- a/hfs/memfs.go +++ b/hfs/memfs.go @@ -28,11 +28,6 @@ type MemFs struct { rootDir *MemDir } -func (m *MemFs) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { - //TODO implement me - panic("mem fs not support OpenFile method") -} - func (m *MemFs) NewFs() Hfs { return &MemFs{ rootDir: &MemDir{ diff --git a/hfs/memfs_test.go b/hfs/memfs_test.go index 31d6723..617b490 100644 --- a/hfs/memfs_test.go +++ b/hfs/memfs_test.go @@ -1,3 +1,6 @@ +//go:build memfs +// +build memfs + /* @Author: ingbyr */ diff --git a/hfs/osfs.go b/hfs/osfs.go index 34c7c75..3854186 100644 --- a/hfs/osfs.go +++ b/hfs/osfs.go @@ -18,10 +18,6 @@ var H Hfs = NewOsFs() type OsFs struct { } -func (o *OsFs) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { - return os.OpenFile(name, flag, perm) -} - func (o *OsFs) NewFs() Hfs { panic("os fs do not support NewFs() method") } diff --git a/host/manager.go b/host/manager.go index 5805e6d..23ebed3 100644 --- a/host/manager.go +++ b/host/manager.go @@ -8,7 +8,6 @@ import ( "bufio" "bytes" "fmt" - "os" "sort" "strings" @@ -296,11 +295,11 @@ func (m *manager) ApplyGroup(group string, simulate bool) { } // open system host file - sysHost, err := fs.OpenFile(conf.SysHost, os.O_RDONLY|os.O_WRONLY|os.O_TRUNC, hfs.Perm644) - defer sysHost.Close() + sysHost, err := fs.Create(conf.SysHost) if err != nil { display.ErrExit(err) } + defer sysHost.Close() // write hosts to system host file if _, err = sysHost.Write(combinedHostContent); err != nil {