@@ -2,19 +2,18 @@ package subcmd
2
2
3
3
import (
4
4
"flag"
5
- "os"
6
- "os/user"
7
- "runtime"
8
-
9
- "github.com/pkg/errors"
10
5
11
6
"github.com/vim-volt/volt/config"
12
7
"github.com/vim-volt/volt/lockjson"
13
- "github.com/vim-volt/volt/logger"
14
8
)
15
9
16
10
var cmdMap = make (map [string ]Cmd )
17
11
12
+ // LookUpCmd looks up subcommand by name.
13
+ func LookUpCmd (cmd string ) Cmd {
14
+ return cmdMap [cmd ]
15
+ }
16
+
18
17
// Cmd represents volt's subcommand interface.
19
18
// All subcommands must implement this.
20
19
type Cmd interface {
@@ -30,10 +29,6 @@ type CmdContext struct {
30
29
Config * config.Config
31
30
}
32
31
33
- // RunnerFunc invokes c with args.
34
- // On unit testing, a mock function was given.
35
- type RunnerFunc func (c Cmd , cmdctx * CmdContext ) * Error
36
-
37
32
// Error is a command error.
38
33
// It also has a exit code.
39
34
type Error struct {
@@ -44,88 +39,3 @@ type Error struct {
44
39
func (e * Error ) Error () string {
45
40
return e .Msg
46
41
}
47
-
48
- // DefaultRunner simply runs command with args
49
- func DefaultRunner (c Cmd , cmdctx * CmdContext ) * Error {
50
- return c .Run (cmdctx )
51
- }
52
-
53
- // Run is invoked by main(), each argument means 'volt {subcmd} {args}'.
54
- func Run (args []string , cont RunnerFunc ) * Error {
55
- if os .Getenv ("VOLT_DEBUG" ) != "" {
56
- logger .SetLevel (logger .DebugLevel )
57
- }
58
-
59
- if len (args ) <= 1 {
60
- args = append (args , "help" )
61
- }
62
- subCmd := args [1 ]
63
- args = args [2 :]
64
-
65
- // Expand subcommand alias
66
- subCmd , args , err := expandAlias (subCmd , args )
67
- if err != nil {
68
- return & Error {Code : 1 , Msg : err .Error ()}
69
- }
70
-
71
- c , exists := cmdMap [subCmd ]
72
- if ! exists {
73
- return & Error {Code : 3 , Msg : "Unknown command '" + subCmd + "'" }
74
- }
75
-
76
- // Disallow executing the commands which may modify files in root priviledge
77
- if c .ProhibitRootExecution (args ) {
78
- err := detectPriviledgedUser ()
79
- if err != nil {
80
- return & Error {Code : 4 , Msg : err .Error ()}
81
- }
82
- }
83
-
84
- lockJSON , err := lockjson .Read ()
85
- if err != nil {
86
- return & Error {Code : 20 , Msg : errors .Wrap (err , "failed to read lock.json" ).Error ()}
87
- }
88
-
89
- cfg , err := config .Read ()
90
- if err != nil {
91
- return & Error {Code : 30 , Msg : errors .Wrap (err , "failed to read config.toml" ).Error ()}
92
- }
93
-
94
- return cont (c , & CmdContext {
95
- Args : args ,
96
- LockJSON : lockJSON ,
97
- Config : cfg ,
98
- })
99
- }
100
-
101
- func expandAlias (subCmd string , args []string ) (string , []string , error ) {
102
- cfg , err := config .Read ()
103
- if err != nil {
104
- return "" , nil , errors .Wrap (err , "could not read config.toml" )
105
- }
106
- if newArgs , exists := cfg .Alias [subCmd ]; exists && len (newArgs ) > 0 {
107
- subCmd = newArgs [0 ]
108
- args = append (newArgs [1 :], args ... )
109
- }
110
- return subCmd , args , nil
111
- }
112
-
113
- // On Windows, this function always returns nil.
114
- // Because if even administrator user creates a file, the file can be
115
- // overwritten by normal user.
116
- // On Linux, if current user's uid == 0, returns non-nil error.
117
- func detectPriviledgedUser () error {
118
- if runtime .GOOS == "windows" {
119
- return nil
120
- }
121
- u , err := user .Current ()
122
- if err != nil {
123
- return errors .Wrap (err , "cannot get current user" )
124
- }
125
- if u .Uid == "0" {
126
- return errors .New (
127
- "cannot run this sub command with root priviledge. " +
128
- "Please run as normal user" )
129
- }
130
- return nil
131
- }
0 commit comments