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

Commit

Permalink
use universal editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Dec 27, 2021
1 parent d05badd commit 73cb218
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 201 deletions.
3 changes: 1 addition & 2 deletions cmd/cfg.go
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/ingbyr/gohost/conf"
"github.com/spf13/cobra"
)

Expand All @@ -11,7 +10,7 @@ var (
Short: "change config",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
conf.Change(args[0], args[1])
app.ChangeConfig(args[0], args[1])
},
}
)
3 changes: 1 addition & 2 deletions cmd/edit.go
Expand Up @@ -5,7 +5,6 @@
package cmd

import (
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
)

Expand All @@ -15,7 +14,7 @@ var (
Short: "edit one host file",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
host.M.EditHostFile(args[0])
app.EditHostFile(args[0])
},
}
)
9 changes: 4 additions & 5 deletions cmd/group.go
Expand Up @@ -5,8 +5,7 @@
package cmd

import (
"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/host"
"github.com/ingbyr/gohost/config"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -23,13 +22,13 @@ var (
Run: func(cmd *cobra.Command, args []string) {
hostName := args[0]
if groupAdd != "" {
host.M.AddGroup(hostName, strings.Split(groupAdd, conf.SepInCmd))
app.AddGroup(hostName, strings.Split(groupAdd, config.SepInCmd))
}
if groupDel != "" {
host.M.DeleteHostGroups(hostName, strings.Split(groupDel, conf.SepInCmd))
app.DeleteHostGroups(hostName, strings.Split(groupDel, config.SepInCmd))
}
if groupList {
host.M.PrintGroup(hostName)
app.PrintGroup(hostName)
}
},
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/list.go
Expand Up @@ -5,7 +5,6 @@
package cmd

import (
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
)

Expand All @@ -16,9 +15,9 @@ var (
Short: "list all group",
Run: func(cmd *cobra.Command, args []string) {
if listAll {
host.M.DisplayHosts()
app.DisplayHosts()
} else {
host.M.DisplayGroups()
app.DisplayGroups()
}
},
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/new.go
Expand Up @@ -5,8 +5,7 @@
package cmd

import (
"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/host"
"github.com/ingbyr/gohost/config"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -18,9 +17,9 @@ var (
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
host.M.CreateNewHost(args[0], []string{}, true)
app.CreateNewHost(args[0], []string{}, true)
} else {
host.M.CreateNewHost(args[0], strings.Split(args[1], conf.SepInCmd), true)
app.CreateNewHost(args[0], strings.Split(args[1], config.SepInCmd), true)
}
},
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/remove.go
Expand Up @@ -5,8 +5,7 @@
package cmd

import (
"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/host"
"github.com/ingbyr/gohost/config"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -19,9 +18,9 @@ var (
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if removeGroup {
host.M.DeleteGroups(strings.Split(args[0], conf.SepInCmd))
app.DeleteGroups(strings.Split(args[0], config.SepInCmd))
} else {
host.M.DeleteHosts(strings.Split(args[0], conf.SepInCmd))
app.DeleteHosts(strings.Split(args[0], config.SepInCmd))
}
},
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/rename.go
Expand Up @@ -5,21 +5,20 @@
package cmd

import (
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
)

var (
renameGroupFlag bool
renameCmd = &cobra.Command{
renameCmd = &cobra.Command{
Use: "mv",
Short: "rename host file name",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
if renameGroupFlag {
host.M.ChangeGroupName(args[0], args[1])
app.ChangeGroupName(args[0], args[1])
} else {
host.M.ChangeHostName(args[0], args[1])
app.ChangeHostName(args[0], args[1])
}
},
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/root.go
Expand Up @@ -6,13 +6,17 @@ package cmd

import (
"github.com/ingbyr/gohost/display"
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "gohost",
Short: "Host switcher made by ingbyr",
}
var (
rootCmd = &cobra.Command{
Use: "gohost",
Short: "Host switcher made by ingbyr",
}
app = host.M
)

func Execute() {
rootCmd.AddCommand(versionCmd)
Expand Down
3 changes: 1 addition & 2 deletions cmd/sys.go
Expand Up @@ -5,7 +5,6 @@
package cmd

import (
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
)

Expand All @@ -15,7 +14,7 @@ var (
Use: "sys",
Short: "display current system host",
Run: func(cmd *cobra.Command, args []string) {
host.M.PrintSysHost(maxLine)
app.PrintSysHost(maxLine)
},
}
)
Expand Down
3 changes: 1 addition & 2 deletions cmd/use.go
Expand Up @@ -5,7 +5,6 @@
package cmd

import (
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
)

Expand All @@ -18,7 +17,7 @@ var (
Short: "use group host as system host",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
host.M.ApplyGroup(args[0], useSimulateFlag)
app.ApplyGroup(args[0], useSimulateFlag)
},
}
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/version.go
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/config"
"github.com/spf13/cobra"
)

Expand All @@ -11,7 +11,7 @@ var (
Use: "version",
Short: "display version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Version %s\n", conf.Version)
fmt.Printf("Version %s\n", config.Version)
},
}
)
10 changes: 0 additions & 10 deletions conf/unix.go

This file was deleted.

10 changes: 0 additions & 10 deletions conf/windows.go

This file was deleted.

54 changes: 24 additions & 30 deletions conf/conf.go → config/config.go
@@ -1,13 +1,12 @@
package conf
package config

import (
"fmt"
"github.com/ingbyr/gohost/display"
"github.com/ingbyr/gohost/editor"
"github.com/ingbyr/gohost/hfs"
"gopkg.in/ini.v1"
"os/user"
"path"
"path/filepath"
)

const (
Expand All @@ -19,37 +18,34 @@ const (
OpEditor = "editor"
)

type CustomConfig struct {
Editor string `init:"editor"`
}

var (
currUser, _ = user.Current()
BaseDir = path.Join(currUser.HomeDir, ".gohost")
BaseHostFile = path.Join(BaseDir, "."+BaseHostFileName)
ConfigFile = path.Join(BaseDir, "config.ini")
Custom = &CustomConfig{
Editor: editor.Default,
}

fs = hfs.H
BaseDir = filepath.Join(currUser.HomeDir, ".gohost")
BaseHostFile = filepath.Join(BaseDir, "."+BaseHostFileName)
File = filepath.Join(BaseDir, "config.ini")
fs = hfs.H
)

func init() {
type Config struct {
Editor string `ini:"editor"`
EditorArgs string `ini:"editor_args"`
}

func (c *Config) Init() {
// create base dir
if _, err := fs.Stat(BaseDir); fs.IsNotExist(err) {
if err := fs.MkdirAll(BaseDir, hfs.Perm644); err != nil {
display.Panic("can not create dir "+BaseDir, err)
}
}
// init config file
if _, err := fs.Stat(ConfigFile); fs.IsNotExist(err) {
newConfigFile, err := fs.Create(ConfigFile)
// init config file or load config from file
if _, err := fs.Stat(File); fs.IsNotExist(err) {
newConfigFile, err := fs.Create(File)
if err != nil {
display.ErrExit(err)
}
cfg := ini.Empty()
if err := ini.ReflectFrom(cfg, Custom); err != nil {
if err := ini.ReflectFrom(cfg, c); err != nil {
display.ErrExit(err)
}
if _, err := cfg.WriteTo(newConfigFile); err != nil {
Expand All @@ -59,40 +55,38 @@ func init() {
display.ErrExit(err)
}
}
file, err := hfs.H.Open(ConfigFile)
file, err := hfs.H.Open(File)
if err != nil {
display.ErrExit(err)
}
cfg, err := ini.Load(file)
if err != nil {
display.ErrExit(err)
}
if err := cfg.MapTo(Custom); err != nil {
if err := cfg.MapTo(c); err != nil {
display.ErrExit(err)
}
}

func Change(op string, value string) {
func (c *Config) Change(op string, value string) {
if op == "" || value == "" {
display.Err(fmt.Errorf("not valid config (op=%s, value=%s)", op, value))
}
switch op {
case OpEditor:
Custom.Editor = value
c.Editor = value
default:
display.ErrExit(fmt.Errorf("not valid config (op=%s, value=%s)", op, value))
}
Sync()
c.Sync()
}

func Sync() {
func (c *Config) Sync() {
cfg := ini.Empty()
if err := ini.ReflectFrom(cfg, Custom); err != nil {
if err := ini.ReflectFrom(cfg, c); err != nil {
display.ErrExit(err)
}

// todo writer interface
cfgFile, err := hfs.H.Create(ConfigFile)
cfgFile, err := hfs.H.Create(File)
if err != nil {
display.ErrExit(err)
}
Expand Down
2 changes: 1 addition & 1 deletion conf/conf_test.go → config/config_test.go
@@ -1,4 +1,4 @@
package conf
package config

import "testing"

Expand Down
9 changes: 9 additions & 0 deletions config/unix.go
@@ -0,0 +1,9 @@
//go:build linux || darwin
// +build linux darwin

package config

const (
SysHost = "/etc/hosts"
NewLine = "\n"
)
9 changes: 9 additions & 0 deletions config/windows.go
@@ -0,0 +1,9 @@
//go:build windows
// +build windows

package config

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

0 comments on commit 73cb218

Please sign in to comment.