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

Commit

Permalink
add charset
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Dec 18, 2021
1 parent a6d0297 commit a2027e8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea/
.vscode/
*.exe
gohost*
2 changes: 1 addition & 1 deletion editor/vim.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func OpenByVim(filePath string) error {
const vim = "vim"
const noSwap = "-n"
if _, err := exec.LookPath(vim); err != nil {
return fmt.Errorf("please install vim before editing file\n")
return fmt.Errorf("please install vim before editing file\n %v", err)
}
cmd := exec.Command(vim, noSwap, filePath)
cmd.Stdin = os.Stdin
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.2.1
golang.org/x/text v0.3.7
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
10 changes: 6 additions & 4 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package host

import (
"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/util"
"path"
"strings"

"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/util"
)

type Host struct {
Expand Down Expand Up @@ -52,8 +53,9 @@ 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 {
if len(groups) == 0 && hostName != conf.TmpCombinedHost {
groups = append(groups, hostName)
} else {
// sort and unique the groups
Expand All @@ -77,4 +79,4 @@ func HostsToStr(hosts []*Host) string {
hsb.WriteString(", ")
}
return hsb.String()[:hsb.Len()-2]
}
}
36 changes: 23 additions & 13 deletions host/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"bufio"
"bytes"
"fmt"
"os"
"sort"
"strings"

"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/display"
"github.com/ingbyr/gohost/editor"
"github.com/ingbyr/gohost/myfs"
"github.com/ingbyr/gohost/util"
"io/ioutil"
"sort"
"strings"
"golang.org/x/text/transform"
)

type manager struct {
Expand Down Expand Up @@ -218,19 +220,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\n", 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\n", name))
display.ErrExit(fmt.Errorf("host file '%s' already exists", name))
}
host := NewHostByNameGroups(name, groups)
if edit {
if err := editor.OpenByVim(host.FilePath); err != nil {
display.ErrExit(fmt.Errorf("failed to create file '%s'\n", host.FilePath))
display.ErrExit(fmt.Errorf("failed to create 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", host.FilePath))
display.ErrExit(fmt.Errorf("can not create %s file\n%v", host.FilePath, err))
}
}
}
Expand All @@ -255,7 +257,7 @@ func (m *manager) ChangeHostName(hostName string, newHostName string) {
display.ErrExit(fmt.Errorf("can not change base host file name"))
}
if _, exist := m.host(newHostName); exist {
display.ErrExit(fmt.Errorf("host '%s' has been existed\n", newHostName))
display.ErrExit(fmt.Errorf("host '%s' has been existed", newHostName))
}
h := m.mustHost(hostName)
newHost := NewHostByNameGroups(newHostName, h.Groups)
Expand Down Expand Up @@ -304,13 +306,21 @@ func (m *manager) ApplyGroup(group string, simulate bool) {
return
}

// write to tmp file
// write to temporary combined host file
combinedHost := NewHostByNameGroups(conf.TmpCombinedHost, nil)
if err := ioutil.WriteFile(combinedHost.FilePath, combinedHostContent, 0664); err != nil {
combinedHostFile, err := os.Create(combinedHost.FilePath)
if err != nil {
display.ErrExit(err)
}
combinedHostFileWriter := transform.NewWriter(combinedHostFile, SysHostCharset.NewEncoder())
_, err = combinedHostFileWriter.Write(combinedHostContent)
if err != nil {
display.ErrExit(err)
}
combinedHostFile.Close()
combinedHostFileWriter.Close()

// replace system host
// replace system host with temporary combined host file
if err := m.fs.Rename(combinedHost.FilePath, SysHost); err != nil {
display.ErrExit(err)
}
Expand Down Expand Up @@ -351,7 +361,7 @@ func (m *manager) host(hostName string) (*Host, bool) {
func (m *manager) mustHost(hostName string) *Host {
host, exist := m.host(hostName)
if !exist {
display.ErrExit(fmt.Errorf("host file '%s' is not existed\n", hostName))
display.ErrExit(fmt.Errorf("host file '%s' is not existed", hostName))
}
return host
}
Expand All @@ -364,7 +374,7 @@ func (m *manager) group(groupName string) ([]*Host, bool) {
func (m *manager) mustGroup(groupName string) []*Host {
group, exist := m.group(groupName)
if !exist {
display.ErrExit(fmt.Errorf("group '%s' is not existed\n", groupName))
display.ErrExit(fmt.Errorf("group '%s' is not existed", groupName))
}
return group
}
Expand Down
11 changes: 10 additions & 1 deletion host/unix.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
//+build linux darwin
//go:build linux || darwin
// +build linux darwin

/*
@Author: ingbyr
*/

package host

import (
"golang.org/x/text/encoding/unicode"
)

const (
SysHost = "/etc/hosts"
NewLine = "\n"
)

var (
SysHostCharset = unicode.UTF8
)
12 changes: 10 additions & 2 deletions host/windows.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
//+build windows
//go:build windows
// +build windows

/*
@Author: ingbyr
*/
package host

import "golang.org/x/text/encoding/charmap"

const (
sysHost = "C:\\Windows\\System32\\drivers\\etc\\hosts"
SysHost = "C:\\Windows\\System32\\drivers\\etc\\hosts"
NewLine = "\r\n"
)

var (
// see https://stackoverflow.com/questions/701882/what-is-ansi-format/702023#702023
SysHostCharset = charmap.ISO8859_1
)

0 comments on commit a2027e8

Please sign in to comment.