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

Commit

Permalink
change host ext; add notepad for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Dec 21, 2021
1 parent 4136403 commit 1913cf9
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 68 deletions.
9 changes: 6 additions & 3 deletions conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package conf

import (
"os/user"
"path/filepath"
"path"
)

const (
SepGroupInFile = "_"
SepInCmd = ","
TmpCombinedHost = ".tmp_combined"
BaseHostFileName = "base"
HostFileExt = ".txt"
)

var (
BaseDir string
BaseHostFile string
ConfigFile string
)

func init() {
Expand All @@ -23,6 +25,7 @@ func init() {
panic(err)
}

BaseDir = filepath.Join(curr.HomeDir, ".gohost")
BaseHostFile = filepath.Join(BaseDir, "."+BaseHostFileName)
BaseDir = path.Join(curr.HomeDir, ".gohost")
BaseHostFile = path.Join(BaseDir, "."+BaseHostFileName)
ConfigFile = path.Join(BaseDir, "config.ini")
}
16 changes: 11 additions & 5 deletions editor/vim.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import (
"os/exec"
)

func OpenByVim(filePath string) error {
const vim = "vim"
type vim struct{}

func NewVim() vim {
return vim{}
}

func (vim) Open(filePath string) error {
const vimCmd = "vim"
const noSwap = "-n"
if _, err := exec.LookPath(vim); err != nil {
return fmt.Errorf("please install vim before editing file\n %v", err)
if _, err := exec.LookPath(vimCmd); err != nil {
return fmt.Errorf("can not find vim")
}
cmd := exec.Command(vim, noSwap, filePath)
cmd := exec.Command(vimCmd, noSwap, filePath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
return cmd.Run()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.2.1
golang.org/x/text v0.3.7
gopkg.in/ini.v1 v1.66.2 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
5 changes: 2 additions & 3 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Host struct {

func (h *Host) GenAutoFields() {
h.FileName = strings.Join(append(h.Groups, h.Name), conf.SepGroupInFile)
h.FilePath = path.Join(conf.BaseDir, h.FileName)
h.FilePath = path.Join(conf.BaseDir, h.FileName+conf.HostFileExt)
}

func (h *Host) RemoveGroup(group string) bool {
Expand All @@ -45,15 +45,14 @@ func NewHostByFileName(fileName string) *Host {
name := groups[len(groups)-1]
groups = groups[:len(groups)-1]
return &Host{
Name: name,
Name: name[:len(name)-len(conf.HostFileExt)],
FileName: fileName,
FilePath: path.Join(conf.BaseDir, fileName),
Groups: groups,
}
}

func NewHostByNameGroups(hostName string, groups []string) *Host {

// use host name as group if no specified groups
if len(groups) == 0 && hostName != conf.TmpCombinedHost {
groups = append(groups, hostName)
Expand Down
38 changes: 21 additions & 17 deletions host/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type manager struct {
_groups []string
noGroupHost []*Host
fs myfs.HostFs
editor editor.Editor
}

var M *manager
Expand All @@ -45,6 +46,10 @@ func init() {

// setup default fs
M.SetFs(myfs.NewOsFs())

// setup editor
// M.editor = editor.NewVim()
M.editor = editor.NewNotepad()
}

func (m *manager) SetFs(newFs myfs.HostFs) {
Expand All @@ -61,9 +66,9 @@ func (m *manager) SetFs(newFs myfs.HostFs) {
if _, err := m.fs.Stat(m.baseHost.FilePath); m.fs.IsNotExist(err) {
var content bytes.Buffer
content.WriteString("127.0.0.1 localhost")
content.WriteString(NewLine)
content.WriteString(conf.NewLine)
content.WriteString("::1 localhost")
content.WriteString(NewLine)
content.WriteString(conf.NewLine)
if err := m.fs.WriteFile(m.baseHost.FilePath, content.Bytes(), 0644); err != nil {
display.Panic("can not create base host file", err)
}
Expand All @@ -88,7 +93,7 @@ func (m *manager) LoadHosts() {
// load host files
for _, file := range files {
// skip dir and files started with '.'
if file.IsDir() || strings.HasPrefix(file.Name(), ".") {
if file.IsDir() || !strings.HasSuffix(file.Name(), conf.HostFileExt) {
continue
}
// create host
Expand Down Expand Up @@ -219,20 +224,19 @@ func (m *manager) AddGroup(hostName string, groups []string) {
}

func (m *manager) CreateNewHost(name string, groups []string, edit bool) {
if name == m.baseHost.Name {
display.ErrExit(fmt.Errorf("host file '%s' already exists", name))
}
if _, exist := m.hosts[name]; exist {
display.ErrExit(fmt.Errorf("host file '%s' already exists", name))
}
host := NewHostByNameGroups(name, groups)
// create the file before editing
m.fs.WriteFile(host.FilePath, nil, 0644)
if edit {
if err := editor.OpenByVim(host.FilePath); err != nil {
display.ErrExit(fmt.Errorf("failed to create file '%s'\n%v", host.FilePath, err))
if err := m.editor.Open(host.FilePath); err != nil {
display.ErrExit(fmt.Errorf("failed to edit file '%s'\n%v", host.FilePath, err))
}
} else {
if err := m.fs.WriteFile(host.FilePath, []byte(""), myfs.Perm644); err != nil {
display.ErrExit(fmt.Errorf("can not create %s file\n%v", host.FilePath, err))
display.ErrExit(fmt.Errorf("can not edit %s file\n%v", host.FilePath, err))
}
}
}
Expand Down Expand Up @@ -286,7 +290,7 @@ func (m *manager) ChangeGroupName(groupName string, newGroupName string) {

func (m *manager) EditHostFile(hostName string) {
host := m.mustHost(hostName)
if err := editor.OpenByVim(host.FilePath); err != nil {
if err := m.editor.Open(host.FilePath); err != nil {
display.ErrExit(err)
}
}
Expand All @@ -312,7 +316,7 @@ func (m *manager) ApplyGroup(group string, simulate bool) {
if err != nil {
display.ErrExit(err)
}
combinedHostFileWriter := transform.NewWriter(combinedHostFile, SysHostCharset.NewEncoder())
combinedHostFileWriter := transform.NewWriter(combinedHostFile, conf.SysHostCharset.NewEncoder())
_, err = combinedHostFileWriter.Write(combinedHostContent)
if err != nil {
display.ErrExit(err)
Expand All @@ -321,7 +325,7 @@ func (m *manager) ApplyGroup(group string, simulate bool) {
combinedHostFileWriter.Close()

// replace system host with temporary combined host file
if err := m.fs.Rename(combinedHost.FilePath, SysHost); err != nil {
if err := m.fs.Rename(combinedHost.FilePath, conf.SysHost); err != nil {
display.ErrExit(err)
}
fmt.Printf("applied group '%s' to system host:\n", group)
Expand All @@ -331,7 +335,7 @@ func (m *manager) ApplyGroup(group string, simulate bool) {
}

func (m *manager) PrintSysHost(max int) {
host, err := m.fs.Open(SysHost)
host, err := m.fs.Open(conf.SysHost)
if err != nil {
display.Panic("can not read system host file", err)
}
Expand Down Expand Up @@ -392,19 +396,19 @@ func (m *manager) printHosts() {
func (m *manager) combineHosts(hosts []*Host, head string) []byte {
var b bytes.Buffer
b.WriteString(head)
b.WriteString(NewLine + NewLine)
b.WriteString(conf.NewLine + conf.NewLine)
for _, host := range hosts {
file, err := m.fs.Open(host.FilePath)
if err != nil {
display.Panic("can not combine host", err)
}
scanner := bufio.NewScanner(file)
b.WriteString("# " + host.Name + NewLine)
b.WriteString("# " + host.Name + conf.NewLine)
for scanner.Scan() {
b.Write(scanner.Bytes())
b.WriteString(NewLine)
b.WriteString(conf.NewLine)
}
b.WriteString(NewLine)
b.WriteString(conf.NewLine)
_ = file.Close()
}
return b.Bytes()
Expand Down
21 changes: 0 additions & 21 deletions host/unix.go

This file was deleted.

19 changes: 0 additions & 19 deletions host/windows.go

This file was deleted.

0 comments on commit 1913cf9

Please sign in to comment.