diff --git a/cmd/vmr/cli/cli.go b/cmd/vmr/cli/cli.go index 5029f14..fbddc4a 100644 --- a/cmd/vmr/cli/cli.go +++ b/cmd/vmr/cli/cli.go @@ -67,6 +67,7 @@ func (c *Cli) initiate() { c.rootCmd.AddCommand(vcli.ShowInstalledSDKInfo) c.rootCmd.AddCommand(vcli.UninstallVersionCmd) c.rootCmd.AddCommand(vcli.SetupAutoCompletions) + c.rootCmd.AddCommand(vcli.ToggleAllowNestedSessions) } func (c *Cli) Run() { diff --git a/cmd/vmr/cli/vcli/sessions.go b/cmd/vmr/cli/vcli/sessions.go new file mode 100644 index 0000000..f722bb5 --- /dev/null +++ b/cmd/vmr/cli/vcli/sessions.go @@ -0,0 +1,28 @@ +package vcli + +import ( + "fmt" + + "github.com/gvcgo/goutils/pkgs/gtea/gprint" + "github.com/gvcgo/version-manager/internal/cnf" + "github.com/spf13/cobra" +) + +/* +Allow/Disallow nested sessions. +*/ + +var ToggleAllowNestedSessions = &cobra.Command{ + Use: "nested-sessions", + Aliases: []string{"ns"}, + GroupID: GroupID, + Short: "Toggle nested sessions.", + Long: "Example: vmr ns.", + Run: func(cmd *cobra.Command, args []string) { + if ok := cnf.DefaultConfig.ToggleAllowNestedSessions(); ok { + fmt.Println(gprint.CyanStr("Nested sessions are now allowed.")) + } else { + fmt.Println(gprint.YellowStr("Nested sessions are now disallowed.")) + } + }, +} diff --git a/internal/cnf/conf.go b/internal/cnf/conf.go index 862fcf2..4340b99 100644 --- a/internal/cnf/conf.go +++ b/internal/cnf/conf.go @@ -28,12 +28,13 @@ const ( Envs */ const ( - VMRSdkInstallationDirEnv string = "VMR_SDK_INSTALLATION_DIR" - VMRHostUrlEnv string = "VMR_HOST" - VMRReverseProxyEnv string = "VMR_REVERSE_PROXY" - VMRLocalProxyEnv string = "VMR_LOCAL_PROXY" - VMRDonwloadThreadEnv string = "VMR_DOWNLOAD_THREADS" - VMRUseCustomedMirrorEnv string = "VMR_USE_CUSTOMED_MIRRORS" + VMRSdkInstallationDirEnv string = "VMR_SDK_INSTALLATION_DIR" + VMRHostUrlEnv string = "VMR_HOST" + VMRReverseProxyEnv string = "VMR_REVERSE_PROXY" + VMRLocalProxyEnv string = "VMR_LOCAL_PROXY" + VMRDonwloadThreadEnv string = "VMR_DOWNLOAD_THREADS" + VMRUseCustomedMirrorEnv string = "VMR_USE_CUSTOMED_MIRRORS" + VMRAllowNestedSessionsEnv string = "VMR_ALLOW_NESTED_SESSIONS" ) /* @@ -114,12 +115,13 @@ vmr config file. ============================== */ type VMRConf struct { - ProxyUri string `json,toml:"proxy_uri"` - ReverseProxy string `json,toml:"reverse_proxy"` - SDKIntallationDir string `json,toml:"sdk_installation_dir"` - VersionHostUrl string `json,toml:"version_host_url"` - ThreadNum int `json,toml:"download_thread_num"` - UseCustomedMirrors bool `json,toml:"use_customed_mirrors"` + ProxyUri string `json,toml:"proxy_uri"` + ReverseProxy string `json,toml:"reverse_proxy"` + SDKIntallationDir string `json,toml:"sdk_installation_dir"` + VersionHostUrl string `json,toml:"version_host_url"` + ThreadNum int `json,toml:"download_thread_num"` + UseCustomedMirrors bool `json,toml:"use_customed_mirrors"` + AllowNestedSessions bool `json,toml:"allow_nested_sessions"` } func NewVMRConf() (v *VMRConf) { @@ -145,6 +147,9 @@ func NewVMRConf() (v *VMRConf) { if v.ReverseProxy != "" { os.Setenv(VMRReverseProxyEnv, v.ReverseProxy) } + if v.AllowNestedSessions { + os.Setenv(VMRAllowNestedSessionsEnv, "true") + } return v } @@ -204,3 +209,10 @@ func (v *VMRConf) ToggleUseCustomedMirrors() { v.UseCustomedMirrors = !v.UseCustomedMirrors v.Save() } + +func (v *VMRConf) ToggleAllowNestedSessions() bool { + v.Load() + v.AllowNestedSessions = !v.AllowNestedSessions + v.Save() + return v.AllowNestedSessions +} diff --git a/internal/terminal/terminal.go b/internal/terminal/terminal.go index 29125ff..9d2ab54 100644 --- a/internal/terminal/terminal.go +++ b/internal/terminal/terminal.go @@ -10,6 +10,7 @@ import ( "github.com/gvcgo/goutils/pkgs/gtea/gprint" "github.com/gvcgo/goutils/pkgs/gutils" + "github.com/gvcgo/version-manager/internal/cnf" "github.com/gvcgo/version-manager/internal/installer/install" "github.com/gvcgo/version-manager/internal/shell/sh" "github.com/gvcgo/version-manager/internal/terminal/term" @@ -116,8 +117,8 @@ func GetTerminalSize() (height, width int, err error) { } func RunTerminal() { - // already run a terminal. - if os.Getenv(sh.VMDisableEnvName) != "" { + // already run a terminal. Allow/Disallow nested sessions. + if os.Getenv(sh.VMDisableEnvName) != "" && !cnf.DefaultConfig.AllowNestedSessions { return }