diff --git a/go.mod b/go.mod index 3f7cf7d011..02baeb188c 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/google/uuid v1.1.1 github.com/gorilla/handlers v1.4.2 github.com/gorilla/securecookie v1.1.1 - github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/profile v1.3.0 github.com/prometheus/client_golang v1.2.1 github.com/prometheus/common v0.7.0 diff --git a/pkg/visor/config.go b/pkg/visor/config.go index c4c1bbbb37..554c6e3229 100644 --- a/pkg/visor/config.go +++ b/pkg/visor/config.go @@ -134,13 +134,13 @@ func (c *Config) RoutingTable() (routing.Table, error) { } // AppsConfig decodes AppsConfig from a local json config file. -func (c *Config) AppsConfig() ([]AppConfig, error) { - apps := make([]AppConfig, 0) +func (c *Config) AppsConfig() (map[string]AppConfig, error) { + apps := make(map[string]AppConfig) for _, app := range c.Apps { if app.Version == "" { app.Version = c.Version } - apps = append(apps, app) + apps[app.App] = app } return apps, nil diff --git a/pkg/visor/config_test.go b/pkg/visor/config_test.go index 3852a16e90..e0363a8e84 100644 --- a/pkg/visor/config_test.go +++ b/pkg/visor/config_test.go @@ -94,13 +94,13 @@ func TestAppsConfig(t *testing.T) { appsConf, err := conf.AppsConfig() require.NoError(t, err) - app1 := appsConf[0] + app1 := appsConf["foo"] assert.Equal(t, "foo", app1.App) assert.Equal(t, "1.1", app1.Version) assert.Equal(t, routing.Port(1), app1.Port) assert.False(t, app1.AutoStart) - app2 := appsConf[1] + app2 := appsConf["bar"] assert.Equal(t, "bar", app2.App) assert.Equal(t, "1.0", app2.Version) assert.Equal(t, routing.Port(2), app2.Port) diff --git a/pkg/visor/rpc_test.go b/pkg/visor/rpc_test.go index f30b6c64e0..e3f7ef4266 100644 --- a/pkg/visor/rpc_test.go +++ b/pkg/visor/rpc_test.go @@ -68,7 +68,8 @@ func TestUptime(t *testing.T) { // TODO (Darkren): fix tests func TestListApps(t *testing.T) { - apps := []AppConfig{ + apps := make(map[string]AppConfig) + appCfg := []AppConfig{ { App: "foo", AutoStart: false, @@ -81,9 +82,13 @@ func TestListApps(t *testing.T) { }, } + for _, app := range appCfg { + apps[app.App] = app + } + pm := &appserver.MockProcManager{} - pm.On("Exists", apps[0].App).Return(false) - pm.On("Exists", apps[1].App).Return(true) + pm.On("Exists", apps["foo"].App).Return(false) + pm.On("Exists", apps["bar"].App).Return(true) n := Node{ appsConf: apps, @@ -119,7 +124,7 @@ func TestStartStopApp(t *testing.T) { require.NoError(t, os.RemoveAll("skychat")) }() - apps := []AppConfig{ + appCfg := []AppConfig{ { App: "foo", Version: "1.0", @@ -127,9 +132,12 @@ func TestStartStopApp(t *testing.T) { Port: 10, }, } + apps := map[string]AppConfig{ + "foo": appCfg[0], + } unknownApp := "bar" - app := apps[0].App + app := apps["foo"].App nodeCfg := Config{} nodeCfg.Node.StaticPubKey = pk @@ -148,12 +156,12 @@ func TestStartStopApp(t *testing.T) { pm := &appserver.MockProcManager{} appCfg1 := appcommon.Config{ Name: app, - Version: apps[0].Version, + Version: apps["foo"].Version, SockFilePath: nodeCfg.AppServerSockFile, VisorPK: nodeCfg.Node.StaticPubKey.Hex(), - WorkDir: filepath.Join("", app, fmt.Sprintf("v%s", apps[0].Version)), + WorkDir: filepath.Join("", app, fmt.Sprintf("v%s", apps["foo"].Version)), } - appArgs1 := append([]string{filepath.Join(node.dir(), app)}, apps[0].Args...) + appArgs1 := append([]string{filepath.Join(node.dir(), app)}, apps["foo"].Args...) appPID1 := appcommon.ProcID(10) pm.On("Run", mock.Anything, appCfg1, appArgs1, mock.Anything, mock.Anything). Return(appPID1, testhelpers.NoErr) diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index f342358862..114c26c922 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -81,7 +81,7 @@ type Node struct { appsPath string localPath string - appsConf []AppConfig + appsConf map[string]AppConfig startedAt time.Time restartCtx *restart.Context @@ -487,11 +487,12 @@ func (node *Node) StopApp(appName string) error { // SetAutoStart sets an app to auto start or not. func (node *Node) SetAutoStart(appName string, autoStart bool) error { - for i, ac := range node.appsConf { - if ac.App == appName { - node.appsConf[i].AutoStart = autoStart - return nil - } + appConf, ok := node.appsConf[appName] + if !ok { + return ErrUnknownApp } - return ErrUnknownApp + + appConf.AutoStart = autoStart + node.appsConf[appName] = appConf + return nil } diff --git a/pkg/visor/visor_test.go b/pkg/visor/visor_test.go index 6313693a63..8b658d539e 100644 --- a/pkg/visor/visor_test.go +++ b/pkg/visor/visor_test.go @@ -88,7 +88,8 @@ func TestNodeStartClose(t *testing.T) { r.On("Serve", mock.Anything /* context */).Return(testhelpers.NoErr) r.On("Close").Return(testhelpers.NoErr) - apps := []AppConfig{ + apps := make(map[string]AppConfig) + appCfg := []AppConfig{ { App: "skychat", Version: "1.0", @@ -102,6 +103,10 @@ func TestNodeStartClose(t *testing.T) { }, } + for _, app := range appCfg { + apps[app.App] = app + } + defer func() { require.NoError(t, os.RemoveAll("skychat")) }() @@ -117,17 +122,17 @@ func TestNodeStartClose(t *testing.T) { pm := &appserver.MockProcManager{} appCfg1 := appcommon.Config{ - Name: apps[0].App, - Version: apps[0].Version, + Name: apps["skychat"].App, + Version: apps["skychat"].Version, SockFilePath: nodeCfg.AppServerSockFile, VisorPK: nodeCfg.Node.StaticPubKey.Hex(), - WorkDir: filepath.Join("", apps[0].App, fmt.Sprintf("v%s", apps[0].Version)), + WorkDir: filepath.Join("", apps["skychat"].App, fmt.Sprintf("v%s", apps["skychat"].Version)), } - appArgs1 := append([]string{filepath.Join(node.dir(), apps[0].App)}, apps[0].Args...) + appArgs1 := append([]string{filepath.Join(node.dir(), apps["skychat"].App)}, apps["skychat"].Args...) appPID1 := appcommon.ProcID(10) pm.On("Run", mock.Anything, appCfg1, appArgs1, mock.Anything, mock.Anything). Return(appPID1, testhelpers.NoErr) - pm.On("Wait", apps[0].App).Return(testhelpers.NoErr) + pm.On("Wait", apps["skychat"].App).Return(testhelpers.NoErr) pm.On("StopAll").Return() @@ -180,12 +185,15 @@ func TestNodeSpawnApp(t *testing.T) { Args: []string{"foo"}, } + apps := make(map[string]AppConfig) + apps["skychat"] = app + nodeCfg := Config{} nodeCfg.Node.StaticPubKey = pk node := &Node{ router: r, - appsConf: []AppConfig{app}, + appsConf: apps, logger: logging.MustGetLogger("test"), conf: &nodeCfg, } diff --git a/vendor/github.com/mitchellh/go-homedir/LICENSE b/vendor/github.com/mitchellh/go-homedir/LICENSE deleted file mode 100644 index f9c841a51e..0000000000 --- a/vendor/github.com/mitchellh/go-homedir/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2013 Mitchell Hashimoto - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/vendor/github.com/mitchellh/go-homedir/README.md b/vendor/github.com/mitchellh/go-homedir/README.md deleted file mode 100644 index d70706d5b3..0000000000 --- a/vendor/github.com/mitchellh/go-homedir/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# go-homedir - -This is a Go library for detecting the user's home directory without -the use of cgo, so the library can be used in cross-compilation environments. - -Usage is incredibly simple, just call `homedir.Dir()` to get the home directory -for a user, and `homedir.Expand()` to expand the `~` in a path to the home -directory. - -**Why not just use `os/user`?** The built-in `os/user` package requires -cgo on Darwin systems. This means that any Go code that uses that package -cannot cross compile. But 99% of the time the use for `os/user` is just to -retrieve the home directory, which we can do for the current user without -cgo. This library does that, enabling cross-compilation. diff --git a/vendor/github.com/mitchellh/go-homedir/go.mod b/vendor/github.com/mitchellh/go-homedir/go.mod deleted file mode 100644 index 7efa09a043..0000000000 --- a/vendor/github.com/mitchellh/go-homedir/go.mod +++ /dev/null @@ -1 +0,0 @@ -module github.com/mitchellh/go-homedir diff --git a/vendor/github.com/mitchellh/go-homedir/homedir.go b/vendor/github.com/mitchellh/go-homedir/homedir.go deleted file mode 100644 index 25378537ea..0000000000 --- a/vendor/github.com/mitchellh/go-homedir/homedir.go +++ /dev/null @@ -1,167 +0,0 @@ -package homedir - -import ( - "bytes" - "errors" - "os" - "os/exec" - "path/filepath" - "runtime" - "strconv" - "strings" - "sync" -) - -// DisableCache will disable caching of the home directory. Caching is enabled -// by default. -var DisableCache bool - -var homedirCache string -var cacheLock sync.RWMutex - -// Dir returns the home directory for the executing user. -// -// This uses an OS-specific method for discovering the home directory. -// An error is returned if a home directory cannot be detected. -func Dir() (string, error) { - if !DisableCache { - cacheLock.RLock() - cached := homedirCache - cacheLock.RUnlock() - if cached != "" { - return cached, nil - } - } - - cacheLock.Lock() - defer cacheLock.Unlock() - - var result string - var err error - if runtime.GOOS == "windows" { - result, err = dirWindows() - } else { - // Unix-like system, so just assume Unix - result, err = dirUnix() - } - - if err != nil { - return "", err - } - homedirCache = result - return result, nil -} - -// Expand expands the path to include the home directory if the path -// is prefixed with `~`. If it isn't prefixed with `~`, the path is -// returned as-is. -func Expand(path string) (string, error) { - if len(path) == 0 { - return path, nil - } - - if path[0] != '~' { - return path, nil - } - - if len(path) > 1 && path[1] != '/' && path[1] != '\\' { - return "", errors.New("cannot expand user-specific home dir") - } - - dir, err := Dir() - if err != nil { - return "", err - } - - return filepath.Join(dir, path[1:]), nil -} - -// Reset clears the cache, forcing the next call to Dir to re-detect -// the home directory. This generally never has to be called, but can be -// useful in tests if you're modifying the home directory via the HOME -// env var or something. -func Reset() { - cacheLock.Lock() - defer cacheLock.Unlock() - homedirCache = "" -} - -func dirUnix() (string, error) { - homeEnv := "HOME" - if runtime.GOOS == "plan9" { - // On plan9, env vars are lowercase. - homeEnv = "home" - } - - // First prefer the HOME environmental variable - if home := os.Getenv(homeEnv); home != "" { - return home, nil - } - - var stdout bytes.Buffer - - // If that fails, try OS specific commands - if runtime.GOOS == "darwin" { - cmd := exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`) - cmd.Stdout = &stdout - if err := cmd.Run(); err == nil { - result := strings.TrimSpace(stdout.String()) - if result != "" { - return result, nil - } - } - } else { - cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid())) - cmd.Stdout = &stdout - if err := cmd.Run(); err != nil { - // If the error is ErrNotFound, we ignore it. Otherwise, return it. - if err != exec.ErrNotFound { - return "", err - } - } else { - if passwd := strings.TrimSpace(stdout.String()); passwd != "" { - // username:password:uid:gid:gecos:home:shell - passwdParts := strings.SplitN(passwd, ":", 7) - if len(passwdParts) > 5 { - return passwdParts[5], nil - } - } - } - } - - // If all else fails, try the shell - stdout.Reset() - cmd := exec.Command("sh", "-c", "cd && pwd") - cmd.Stdout = &stdout - if err := cmd.Run(); err != nil { - return "", err - } - - result := strings.TrimSpace(stdout.String()) - if result == "" { - return "", errors.New("blank output when reading home directory") - } - - return result, nil -} - -func dirWindows() (string, error) { - // First prefer the HOME environmental variable - if home := os.Getenv("HOME"); home != "" { - return home, nil - } - - // Prefer standard environment variable USERPROFILE - if home := os.Getenv("USERPROFILE"); home != "" { - return home, nil - } - - drive := os.Getenv("HOMEDRIVE") - path := os.Getenv("HOMEPATH") - home := drive + path - if drive == "" || path == "" { - return "", errors.New("HOMEDRIVE, HOMEPATH, or USERPROFILE are blank") - } - - return home, nil -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 596630099d..88fab39975 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -50,8 +50,6 @@ github.com/mattn/go-isatty github.com/matttproud/golang_protobuf_extensions/pbutil # github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b github.com/mgutz/ansi -# github.com/mitchellh/go-homedir v1.1.0 -github.com/mitchellh/go-homedir # github.com/pkg/profile v1.3.0 github.com/pkg/profile # github.com/pmezard/go-difflib v1.0.0