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

Commit

Permalink
write to system host directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Dec 27, 2021
1 parent 18de72a commit 2e030ca
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
1 change: 0 additions & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const (
SepGroupInFile = "_"
SepInCmd = ","

TmpCombinedHost = ".tmp_combined"
BaseHostFileName = "base"
HostFileExt = ".txt"
OpEditor = "editor"
Expand Down
1 change: 1 addition & 0 deletions hfs/hfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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: 5 additions & 0 deletions hfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ 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
4 changes: 4 additions & 0 deletions hfs/osfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ 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
2 changes: 1 addition & 1 deletion host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewHostByFileName(fileName string) *Host {

func NewHostByNameGroups(hostName string, groups []string) *Host {
// use host name as group if no specified groups
if len(groups) == 0 && hostName != conf.TmpCombinedHost {
if len(groups) == 0 {
groups = append(groups, hostName)
} else {
// sort and unique the groups
Expand Down
53 changes: 23 additions & 30 deletions host/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ingbyr/gohost/editor"
"github.com/ingbyr/gohost/hfs"
"github.com/ingbyr/gohost/util"
"golang.org/x/text/transform"
)

type manager struct {
Expand All @@ -30,7 +29,10 @@ type manager struct {
editor editor.Editor
}

var M *manager
var (
M *manager
fs = hfs.H
)

func init() {
// create manager
Expand All @@ -47,19 +49,18 @@ func init() {
}

func (m *manager) Init() {

// create base host file
if _, err := hfs.H.Stat(m.baseHost.FilePath); hfs.H.IsNotExist(err) {
if _, err := fs.Stat(m.baseHost.FilePath); fs.IsNotExist(err) {
var content bytes.Buffer
content.WriteString("127.0.0.1 localhost")
content.WriteString(conf.NewLine)
content.WriteString("::1 localhost")
content.WriteString(conf.NewLine)
if err := hfs.H.WriteFile(m.baseHost.FilePath, content.Bytes(), 0644); err != nil {
if err := fs.WriteFile(m.baseHost.FilePath, content.Bytes(), 0644); err != nil {
display.Panic("can not create base host file", err)
}
}

// load hosts
m.LoadHosts()
}

Expand All @@ -71,12 +72,12 @@ func (m *manager) LoadHosts() {
m._groups = make([]string, 0)
m.noGroupHost = make([]*Host, 0)

files, err := hfs.H.ReadDir(conf.BaseDir)
files, err := fs.ReadDir(conf.BaseDir)
if err != nil {
display.ErrExit(fmt.Errorf("failed to Init gohost dir"))
}

// Init host files
// init host files
for _, file := range files {
// skip dir and files started with '.'
if file.IsDir() || !strings.HasSuffix(file.Name(), conf.HostFileExt) {
Expand Down Expand Up @@ -177,7 +178,7 @@ func (m *manager) DeleteGroup(group string) bool {
if host.RemoveGroup(group) {
oldFilePath := host.FilePath
host.GenAutoFields()
if err := hfs.H.Rename(oldFilePath, host.FilePath); err != nil {
if err := fs.Rename(oldFilePath, host.FilePath); err != nil {
display.ErrExit(err)
}
}
Expand All @@ -189,7 +190,7 @@ func (m *manager) DeleteHostGroups(hostName string, delGroups []string) {
host := m.mustHost(hostName)
newGroups, removedGroups := util.SliceSub(host.Groups, delGroups)
newHost := NewHostByNameGroups(hostName, newGroups)
err := hfs.H.Rename(host.FilePath, newHost.FilePath)
err := fs.Rename(host.FilePath, newHost.FilePath)
if err != nil {
display.ErrExit(fmt.Errorf("failed to delete groups"))
}
Expand All @@ -201,7 +202,7 @@ func (m *manager) AddGroup(hostName string, groups []string) {
host := m.mustHost(hostName)
newGroups, addGroups := util.SliceUnion(host.Groups, groups)
newHost := NewHostByNameGroups(hostName, newGroups)
err := hfs.H.Rename(host.FilePath, newHost.FilePath)
err := fs.Rename(host.FilePath, newHost.FilePath)
if err != nil {
display.ErrExit(fmt.Errorf("failed to delete groups"))
}
Expand All @@ -215,7 +216,7 @@ func (m *manager) CreateNewHost(name string, groups []string, edit bool) {
}
host := NewHostByNameGroups(name, groups)
// create the file before editing
if err := hfs.H.WriteFile(host.FilePath, []byte(""), hfs.Perm644); err != nil {
if err := fs.WriteFile(host.FilePath, []byte(""), hfs.Perm644); err != nil {
display.ErrExit(fmt.Errorf("failed to create file %s", host.FilePath), err)
}
if edit {
Expand All @@ -229,7 +230,7 @@ func (m *manager) DeleteHosts(hostNames []string) {
deleted := make([]string, 0)
for _, hostName := range hostNames {
if host, exist := m.hosts[hostName]; exist {
err := hfs.H.Remove(host.FilePath)
err := fs.Remove(host.FilePath)
if err != nil {
display.ErrExit(err)
continue
Expand All @@ -249,7 +250,7 @@ func (m *manager) ChangeHostName(hostName string, newHostName string) {
}
h := m.mustHost(hostName)
newHost := NewHostByNameGroups(newHostName, h.Groups)
if err := hfs.H.Rename(h.FilePath, newHost.FilePath); err != nil {
if err := fs.Rename(h.FilePath, newHost.FilePath); err != nil {
display.ErrExit(err)
}
fmt.Printf("renamed '%s' to '%s'\n", h.Name, newHostName)
Expand All @@ -265,7 +266,7 @@ func (m *manager) ChangeGroupName(groupName string, newGroupName string) {
newGroups = append(newGroups, newGroupName)
newGroups = util.SortUniqueStringSlice(newGroups)
newHost := NewHostByNameGroups(host.Name, newGroups)
if err := hfs.H.Rename(host.FilePath, newHost.FilePath); err != nil {
if err := fs.Rename(host.FilePath, newHost.FilePath); err != nil {
display.ErrExit(err)
}
}
Expand Down Expand Up @@ -294,32 +295,24 @@ func (m *manager) ApplyGroup(group string, simulate bool) {
return
}

// write to temporary combined host file
combinedHost := NewHostByNameGroups(conf.TmpCombinedHost, nil)
combinedHostFile, err := os.Create(combinedHost.FilePath)
if err != nil {
display.ErrExit(err)
}
combinedHostFileWriter := transform.NewWriter(combinedHostFile, conf.SysHostCharset.NewEncoder())
_, err = combinedHostFileWriter.Write(combinedHostContent)
// open system host file
sysHost, err := fs.OpenFile(conf.SysHost, os.O_RDONLY|os.O_WRONLY|os.O_TRUNC, hfs.Perm644)
defer sysHost.Close()
if err != nil {
display.ErrExit(err)
}
combinedHostFile.Close()
combinedHostFileWriter.Close()

// replace system host with temporary combined host file
if err := hfs.H.Rename(combinedHost.FilePath, conf.SysHost); err != nil {
// write hosts to system host file
if _, err = sysHost.Write(combinedHostContent); err != nil {
display.ErrExit(err)
}
fmt.Printf("applied group '%s' to system host:\n", group)

// display system host
m.PrintSysHost(10)
}

func (m *manager) PrintSysHost(max int) {
host, err := hfs.H.Open(conf.SysHost)
host, err := fs.Open(conf.SysHost)
if err != nil {
display.Panic("can not read system host file", err)
}
Expand Down Expand Up @@ -382,7 +375,7 @@ func (m *manager) combineHosts(hosts []*Host, head string) []byte {
b.WriteString(head)
b.WriteString(conf.NewLine + conf.NewLine)
for _, host := range hosts {
file, err := hfs.H.Open(host.FilePath)
file, err := fs.Open(host.FilePath)
if err != nil {
display.Panic("can not combine host", err)
}
Expand Down

0 comments on commit 2e030ca

Please sign in to comment.