diff options
author | Valery Piashchynski <[email protected]> | 2022-04-28 11:52:16 +0200 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-04-28 12:16:41 +0200 |
commit | 62ca41aca6d5058da36088ee453f751c1c1bbf3d (patch) | |
tree | 6b9f0e8cc51772315d1a1354bdeeaa043a96a671 | |
parent | 71de185b717ec51dac9b6034f456a9fac6412d14 (diff) |
fix chdir routine
Signed-off-by: Valery Piashchynski <[email protected]>
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 9 | ||||
-rw-r--r-- | internal/cli/root.go | 10 | ||||
-rw-r--r-- | internal/cli/root_test.go | 30 |
4 files changed, 48 insertions, 3 deletions
@@ -54,6 +54,8 @@ require ( github.com/vbauerster/mpb/v5 v5.4.0 ) +replace github.com/roadrunner-server/config/v2 => ../plugins/config + require ( github.com/VividCortex/ewma v1.2.0 // indirect github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect @@ -373,6 +373,7 @@ github.com/roadrunner-server/amqp/v2 v2.12.1 h1:IxAhcGun1NNooiwY/Sy3PI6wTv0EeQB+ github.com/roadrunner-server/amqp/v2 v2.12.1/go.mod h1:j6ANaANaUMleHFoI1o+SWDTJITxiq5zifc4cjzYEURs= github.com/roadrunner-server/api/v2 v2.14.0 h1:U7CrMXv5h8qlO9vvqdvPDK0g+zMLwcERUAPcoLy6tYs= github.com/roadrunner-server/api/v2 v2.14.0/go.mod h1:COjb3T7DYHydaKIfino/N8gVa8op1jGAXFF+qKHMnk0= +<<<<<<< HEAD github.com/roadrunner-server/beanstalk/v2 v2.12.1 h1:RnReSyyspD2Zg9o80oyPZYhy7J2n5Bo6UfdRJujFaao= github.com/roadrunner-server/beanstalk/v2 v2.12.1/go.mod h1:IuFIPRJalRwfv9RJBb2hAso8oGKzENS8/oV1EC59uS8= github.com/roadrunner-server/boltdb/v2 v2.12.1 h1:Mkaz0t4qpJ2obpododQFssQnnL4o5Tq6794SMS3Zkw4= @@ -383,6 +384,14 @@ github.com/roadrunner-server/cache/v2 v2.12.1 h1:h+x6V9xwWrQub0ux2lcEFHHtVK+b+Kz github.com/roadrunner-server/cache/v2 v2.12.1/go.mod h1:XG6WnobhCJmvX6MhqePYo4nPfzK8HvNSzIAoJ9spfGc= github.com/roadrunner-server/config/v2 v2.13.1 h1:p7dSAvjQmE7umHQ3QxSv/j0VUvNyXTOfNT2iYVSXC3M= github.com/roadrunner-server/config/v2 v2.13.1/go.mod h1:OxUdPzKTgkNVsBGpaJlPjC6l2Bxyf3jbGvYE/ybNqS8= +github.com/roadrunner-server/beanstalk/v2 v2.12.2 h1:/U4s5tJdtK/f7rucCsm/PLvaksc3Jj2fGTUKWiDlJcw= +github.com/roadrunner-server/beanstalk/v2 v2.12.2/go.mod h1:QitrdRD4wpEvXMptzorS37qmYQ2oQ5gBDyJqVkUR+mM= +github.com/roadrunner-server/boltdb/v2 v2.12.2 h1:Z9zhfvCiM3qriWLY0grEpViz6m2/RPxv+y2wRPHf890= +github.com/roadrunner-server/boltdb/v2 v2.12.2/go.mod h1:VwLSrIGe/aP3cP0x7vPydcidJ54KL/07+qOkfSvDtiM= +github.com/roadrunner-server/broadcast/v2 v2.11.2 h1:rYq0umlXrbIwleWS41W+c96SakPMz89UNt1LfyBea+o= +github.com/roadrunner-server/broadcast/v2 v2.11.2/go.mod h1:6XAE7LRQToEB/zv0iEbBI2APPbjmIHd4kN6AwJLdW9A= +github.com/roadrunner-server/cache/v2 v2.12.2 h1:OePy9morZOgwjymRvZZl/g5gO/EsIRohz95jIcbyIjk= +github.com/roadrunner-server/cache/v2 v2.12.2/go.mod h1:h0HH+9MiIVLwMvSMmp3S20e+8HHsbzynTXbVREKh/r4= github.com/roadrunner-server/endure v1.2.3 h1:Smm+pHDERzeqXsLO602oe5iy0q+w94jAH3UT1pqgZiY= github.com/roadrunner-server/endure v1.2.3/go.mod h1:Yahi4Uoabla2o+0N+pbjWwIJnAigYWppGwbLU3D6cLc= github.com/roadrunner-server/errors v1.1.2 h1:+LPw9Akgg+8LAGM4wASMWLAoBIbyP+mhNfR+FBS2Slw= diff --git a/internal/cli/root.go b/internal/cli/root.go index 7e22124a..0df74be0 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -46,18 +46,22 @@ func NewCommand(cmdName string) *cobra.Command { //nolint:funlen return errors.Str("no configuration file provided") } + // if user set the wd, change the current wd if workDir != "" { if err := os.Chdir(workDir); err != nil { return err } } + // try to get the absolute path to the configuration if absPath, err := filepath.Abs(*cfgFile); err == nil { *cfgFile = absPath // switch config path to the absolute - // force working absPath related to config file - if err = os.Chdir(filepath.Dir(absPath)); err != nil { - return err + // if workDir is empty - force working absPath related to config file + if workDir == "" { + if err = os.Chdir(filepath.Dir(absPath)); err != nil { + return err + } } } diff --git a/internal/cli/root_test.go b/internal/cli/root_test.go index 602b9d3b..f77acec3 100644 --- a/internal/cli/root_test.go +++ b/internal/cli/root_test.go @@ -135,3 +135,33 @@ func TestCommandNoEnvFileNoError(t *testing.T) { _ = os.RemoveAll(path.Join(tmp, ".rr.yaml")) }) } + +func TestCommandWorkingDir(t *testing.T) { + tmp := os.TempDir() + + cmd := cli.NewCommand("serve") + cmd.SetArgs([]string{"-w", tmp}) + + var executed bool + + var wd string + + f2, err := os.Create(path.Join(tmp, ".rr.yaml")) + require.NoError(t, err) + + if cmd.Run == nil { // override "Run" property for test (if it was not set) + cmd.Run = func(cmd *cobra.Command, args []string) { + executed = true + wd, _ = os.Getwd() + } + } + + assert.NoError(t, cmd.Execute()) + assert.True(t, executed) + assert.Equal(t, "/tmp", wd) + + t.Cleanup(func() { + _ = f2.Close() + _ = os.RemoveAll(path.Join(tmp, ".rr.yaml")) + }) +} |