Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
use fs.Create
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Dec 27, 2021
1 parent 5dd773e commit d05badd
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/conf.go → cmd/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
)

const (
Version = "0.1.1"
SepGroupInFile = "_"
SepInCmd = ","

Version = "0.1.1"
SepGroupInFile = "_"
SepInCmd = ","
BaseHostFileName = "base"
HostFileExt = ".txt"
OpEditor = "editor"
Expand Down Expand Up @@ -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)
}
}
13 changes: 3 additions & 10 deletions conf/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
11 changes: 3 additions & 8 deletions conf/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
1 change: 0 additions & 1 deletion hfs/hfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions hfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
3 changes: 3 additions & 0 deletions hfs/memfs_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build memfs
// +build memfs

/*
@Author: ingbyr
*/
Expand Down
4 changes: 0 additions & 4 deletions hfs/osfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 2 additions & 3 deletions host/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bufio"
"bytes"
"fmt"
"os"
"sort"
"strings"

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d05badd

Please sign in to comment.