Skip to content

Commit

Permalink
add new command nested-sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed Dec 28, 2024
1 parent ae5117a commit 738d48e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/vmr/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
28 changes: 28 additions & 0 deletions cmd/vmr/cli/vcli/sessions.go
Original file line number Diff line number Diff line change
@@ -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."))
}
},
}
36 changes: 24 additions & 12 deletions internal/cnf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

/*
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions internal/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 738d48e

Please sign in to comment.