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

Commit

Permalink
add mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Dec 21, 2021
1 parent 1790239 commit a3b2cc6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cmd/root.go
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"github.com/ingbyr/gohost/conf"
"github.com/ingbyr/gohost/display"
"github.com/ingbyr/gohost/host"
"github.com/spf13/cobra"
Expand All @@ -16,7 +17,7 @@ var rootCmd = &cobra.Command{
}

func Execute() {
host.M.SetCmdMode()
host.M.Init(conf.C.Mode)
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(editCmd)
rootCmd.AddCommand(useCmd)
Expand Down
37 changes: 26 additions & 11 deletions conf/conf.go
@@ -1,6 +1,7 @@
package conf

import (
"fmt"
"github.com/ingbyr/gohost/display"
"github.com/ingbyr/gohost/editor"
"gopkg.in/ini.v1"
Expand All @@ -14,36 +15,50 @@ const (
TmpCombinedHost = ".tmp_combined"
BaseHostFileName = "base"
HostFileExt = ".txt"
ModeStorage = "storage"
ModeMemory = "memory"
)

type conf struct {
Editor string `ini:"editor"`
Mode string
Editor string
}

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

func init() {
// init config file
err := ini.MapTo(Conf, ConfigFile)
if err != nil {
Sync()
_ = ini.MapTo(C, ConfigFile)
checkMode()
}

func checkMode() {
switch C.Mode {
case ModeStorage, ModeMemory:
default:
display.ErrExit(fmt.Errorf("not valid mode %s", C.Mode))
}
}

func Sync() {
cfg := ini.Empty()
if err := ini.ReflectFrom(cfg, Conf); err != nil {
display.ErrExit(err)
}
if err := cfg.SaveTo(ConfigFile); err != nil {
display.ErrExit(err)
switch C.Mode {
case ModeStorage:
cfg := ini.Empty()
if err := ini.ReflectFrom(cfg, C); err != nil {
display.ErrExit(err)
}
if err := cfg.SaveTo(ConfigFile); err != nil {
display.ErrExit(err)
}
case ModeMemory:
}
}
3 changes: 2 additions & 1 deletion host/host_test.go
Expand Up @@ -7,11 +7,12 @@ package host
import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/ingbyr/gohost/conf"
"testing"
)

func TestNewHostByFileName(t *testing.T) {
M.SetMockMode()
M.Init(conf.ModeMemory)
var tests = []struct {
name string
want *Host
Expand Down
15 changes: 8 additions & 7 deletions host/manager.go
Expand Up @@ -42,16 +42,17 @@ func init() {
FilePath: conf.BaseHostFile,
Groups: nil,
},
editor: editor.New(conf.C.Editor),
}
}

func (m *manager) SetCmdMode() {
m.SetFs(myfs.NewOsFs())
m.editor = editor.New(conf.Conf.Editor)
}

func (m *manager) SetMockMode() {
m.SetFs(myfs.NewMemFs())
func (m *manager) Init(mode string) {
switch mode {
case conf.ModeStorage:
M.SetFs(myfs.NewOsFs())
case conf.ModeMemory:
M.SetFs(myfs.NewMemFs())
}
}

func (m *manager) SetFs(newFs myfs.HostFs) {
Expand Down
3 changes: 2 additions & 1 deletion host/manager_test.go
Expand Up @@ -5,6 +5,7 @@
package host

import (
"github.com/ingbyr/gohost/conf"
"testing"
)

Expand All @@ -23,7 +24,7 @@ type hostTestOp struct {
}

func TestManager_CreateRemoveNewHost(t *testing.T) {
M.SetMockMode()
M.Init(conf.ModeMemory)
var tests = []hostTestOp{
{
initHosts: []hosts{
Expand Down

0 comments on commit a3b2cc6

Please sign in to comment.