File tree Expand file tree Collapse file tree 4 files changed +44
-1
lines changed
Expand file tree Collapse file tree 4 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ require (
66 github.com/baneeishaque/adoptium_jdk_go v0.0.0-20221214163615-0b2cfe72098d
77 github.com/codegangsta/cli v1.20.0
88 github.com/tucnak/store v0.0.0-20170905113834-b02ecdcc6dfb
9+ golang.org/x/sys v0.40.0
910 gopkg.in/cheggaaa/pb.v1 v1.0.28
1011)
1112
@@ -18,7 +19,6 @@ require (
1819 github.com/itchyny/timefmt-go v0.1.7 // indirect
1920 github.com/mattn/go-colorable v0.1.12 // indirect
2021 github.com/mattn/go-runewidth v0.0.19 // indirect
21- golang.org/x/sys v0.40.0 // indirect
2222 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2323 gopkg.in/yaml.v2 v2.4.0 // indirect
2424)
Original file line number Diff line number Diff line change 11package cmdCli
22
33import (
4+ "errors"
45 "fmt"
56 "os"
67 "os/exec"
78 "path/filepath"
89
910 "github.com/codegangsta/cli"
1011 "github.com/ystyle/jvms/internal/entity"
12+ "github.com/ystyle/jvms/utils/admin"
1113 "github.com/ystyle/jvms/utils/file"
1214)
1315
@@ -29,6 +31,9 @@ func init_(config *entity.Config) *cli.Command {
2931 },
3032 },
3133 Action : func (c * cli.Context ) error {
34+ if ! admin .IsAdmin () {
35+ return errors .New ("jvms init requires administrator privileges. Please run as administrator" )
36+ }
3237 if c .IsSet ("java_home" ) || config .JavaHome == "" {
3338 config .JavaHome = c .String ("java_home" )
3439 }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import (
1111
1212 "github.com/codegangsta/cli"
1313 "github.com/ystyle/jvms/internal/entity"
14+ "github.com/ystyle/jvms/utils/admin"
1415 "github.com/ystyle/jvms/utils/file"
1516 "github.com/ystyle/jvms/utils/jdk"
1617)
@@ -43,6 +44,9 @@ func switch_(config *entity.Config) *cli.Command {
4344// SwitchFunc is used by both switch and use commands
4445func switchFunc (config * entity.Config ) func (* cli.Context ) error {
4546 return func (c * cli.Context ) error {
47+ if ! admin .IsAdmin () {
48+ return errors .New ("jvms switch requires administrator privileges. Please run as administrator" )
49+ }
4650 v := strings .TrimSpace (c .Args ().Get (0 ))
4751 if v == "" {
4852 return errors .New ("you should input a version or index number, Type \" jvms list\" to see what is installed" )
Original file line number Diff line number Diff line change 1+ package admin
2+
3+ import (
4+ "golang.org/x/sys/windows"
5+ )
6+
7+ // IsAdmin checks if the current process has administrative privileges
8+ func IsAdmin () bool {
9+ var sid * windows.SID
10+
11+ // Although this looks huge, it's the equivalent of
12+ // set "sid=S-1-5-32-544"
13+ err := windows .AllocateAndInitializeSid (
14+ & windows .SECURITY_NT_AUTHORITY ,
15+ 2 ,
16+ windows .SECURITY_BUILTIN_DOMAIN_RID ,
17+ windows .DOMAIN_ALIAS_RID_ADMINS ,
18+ 0 , 0 , 0 , 0 , 0 , 0 ,
19+ & sid )
20+ if err != nil {
21+ return false
22+ }
23+ defer windows .FreeSid (sid )
24+
25+ // This appears to cast a null pointer, but that is actually fine.
26+ // CheckTokenMembership allows the token handle to be 0 (current process token).
27+ // It's checked against the sid created above.
28+ token := windows .Token (0 )
29+ member , err := token .IsMember (sid )
30+ if err != nil {
31+ return false
32+ }
33+ return member
34+ }
You can’t perform that action at this time.
0 commit comments