Skip to content

Commit 089f65b

Browse files
committed
refactor: pass lock.json & config value to subcommands
1 parent 053e6dc commit 089f65b

File tree

13 files changed

+78
-38
lines changed

13 files changed

+78
-38
lines changed

_docs/layer.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
## Layered architecture
3+
4+
The volt commands like `volt get` which may modify lock.json, config.toml([#221](https://github.com/vim-volt/volt/issues/221)),
5+
filesystem, are executed in several steps:
6+
7+
1. (Subcmd layer): pass subcommand arguments, lock.json & config.toml structure
8+
to Gateway layer
9+
2. (Gateway layer): Create an AST according to given information
10+
* This layer cannot touch filesystem, because it makes unit testing difficult
11+
3. (Usecase layer): Execute the AST. This note mainly describes this layer's design
12+
13+
Below is the dependency graph:
14+
15+
```
16+
Subcmd --> Gateway --> Usecase
17+
```
18+
19+
* Subcmd only depends Gateway
20+
* Gateway doesn't know Subcmd
21+
* Gateway only depends Usecase
22+
* Usecase doesn't know Gateway

subcmd/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ Description
5353
return fs
5454
}
5555

56-
func (cmd *buildCmd) Run(args []string) *Error {
56+
func (cmd *buildCmd) Run(cmdctx *CmdContext) *Error {
5757
// Parse args
5858
fs := cmd.FlagSet()
59-
fs.Parse(args)
59+
fs.Parse(cmdctx.Args)
6060
if cmd.helped {
6161
return nil
6262
}

subcmd/cmd.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package subcmd
22

33
import (
44
"flag"
5-
"github.com/pkg/errors"
65
"os"
76
"os/user"
87
"runtime"
98

9+
"github.com/pkg/errors"
10+
1011
"github.com/vim-volt/volt/config"
12+
"github.com/vim-volt/volt/lockjson"
1113
"github.com/vim-volt/volt/logger"
1214
)
1315

@@ -17,13 +19,20 @@ var cmdMap = make(map[string]Cmd)
1719
// All subcommands must implement this.
1820
type Cmd interface {
1921
ProhibitRootExecution(args []string) bool
20-
Run(args []string) *Error
22+
Run(cmdctx *CmdContext) *Error
2123
FlagSet() *flag.FlagSet
2224
}
2325

26+
// CmdContext is a data transfer object between Subcmd and Gateway layer.
27+
type CmdContext struct {
28+
Args []string
29+
LockJSON *lockjson.LockJSON
30+
Config *config.Config
31+
}
32+
2433
// RunnerFunc invokes c with args.
2534
// On unit testing, a mock function was given.
26-
type RunnerFunc func(c Cmd, args []string) *Error
35+
type RunnerFunc func(c Cmd, cmdctx *CmdContext) *Error
2736

2837
// Error is a command error.
2938
// It also has a exit code.
@@ -37,8 +46,8 @@ func (e *Error) Error() string {
3746
}
3847

3948
// DefaultRunner simply runs command with args
40-
func DefaultRunner(c Cmd, args []string) *Error {
41-
return c.Run(args)
49+
func DefaultRunner(c Cmd, cmdctx *CmdContext) *Error {
50+
return c.Run(cmdctx)
4251
}
4352

4453
// Run is invoked by main(), each argument means 'volt {subcmd} {args}'.
@@ -61,7 +70,7 @@ func Run(args []string, cont RunnerFunc) *Error {
6170

6271
c, exists := cmdMap[subCmd]
6372
if !exists {
64-
return &Error{Code: 3, Msg: "unknown command '" + subCmd + "'"}
73+
return &Error{Code: 3, Msg: "Unknown command '" + subCmd + "'"}
6574
}
6675

6776
// Disallow executing the commands which may modify files in root priviledge
@@ -72,7 +81,21 @@ func Run(args []string, cont RunnerFunc) *Error {
7281
}
7382
}
7483

75-
return cont(c, args)
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+
})
7699
}
77100

78101
func expandAlias(subCmd string, args []string) (string, []string, error) {

subcmd/disable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Description
4141
return fs
4242
}
4343

44-
func (cmd *disableCmd) Run(args []string) *Error {
45-
reposPathList, err := cmd.parseArgs(args)
44+
func (cmd *disableCmd) Run(cmdctx *CmdContext) *Error {
45+
reposPathList, err := cmd.parseArgs(cmdctx.Args)
4646
if err == ErrShowedHelp {
4747
return nil
4848
}

subcmd/enable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Description
4141
return fs
4242
}
4343

44-
func (cmd *enableCmd) Run(args []string) *Error {
45-
reposPathList, err := cmd.parseArgs(args)
44+
func (cmd *enableCmd) Run(cmdctx *CmdContext) *Error {
45+
reposPathList, err := cmd.parseArgs(cmdctx.Args)
4646
if err == ErrShowedHelp {
4747
return nil
4848
}

subcmd/get.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,25 @@ Options`)
116116
return fs
117117
}
118118

119-
func (cmd *getCmd) Run(args []string) *Error {
119+
func (cmd *getCmd) Run(cmdctx *CmdContext) *Error {
120120
// Parse args
121-
args, err := cmd.parseArgs(args)
121+
args, err := cmd.parseArgs(cmdctx.Args)
122122
if err == ErrShowedHelp {
123123
return nil
124124
}
125125
if err != nil {
126126
return &Error{Code: 10, Msg: "Failed to parse args: " + err.Error()}
127127
}
128128

129-
// Read lock.json
130-
lockJSON, err := lockjson.Read()
131-
if err != nil {
132-
return &Error{Code: 11, Msg: "Could not read lock.json: " + err.Error()}
133-
}
134-
135-
reposPathList, err := cmd.getReposPathList(args, lockJSON)
129+
reposPathList, err := cmd.getReposPathList(args, cmdctx.LockJSON)
136130
if err != nil {
137131
return &Error{Code: 12, Msg: "Could not get repos list: " + err.Error()}
138132
}
139133
if len(reposPathList) == 0 {
140134
return &Error{Code: 13, Msg: "No repositories are specified"}
141135
}
142136

143-
err = cmd.doGet(reposPathList, lockJSON)
137+
err = cmd.doGet(reposPathList, cmdctx.LockJSON)
144138
if err != nil {
145139
return &Error{Code: 20, Msg: err.Error()}
146140
}

subcmd/help.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ Command
9999
return fs
100100
}
101101

102-
func (cmd *helpCmd) Run(args []string) *Error {
102+
func (cmd *helpCmd) Run(cmdctx *CmdContext) *Error {
103+
args := cmdctx.Args
103104
if len(args) == 0 {
104105
cmd.FlagSet().Usage()
105106
return nil
@@ -112,7 +113,7 @@ func (cmd *helpCmd) Run(args []string) *Error {
112113
if !exists {
113114
return &Error{Code: 1, Msg: fmt.Sprintf("Unknown command '%s'", args[0])}
114115
}
115-
args = append([]string{"-help"}, args[1:]...)
116-
fs.Run(args)
116+
cmdctx.Args = append([]string{"-help"}, args[1:]...)
117+
fs.Run(cmdctx)
117118
return nil
118119
}

subcmd/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ repos path:
125125
`
126126
}
127127

128-
func (cmd *listCmd) Run(args []string) *Error {
128+
func (cmd *listCmd) Run(cmdctx *CmdContext) *Error {
129129
fs := cmd.FlagSet()
130-
fs.Parse(args)
130+
fs.Parse(cmdctx.Args)
131131
if cmd.helped {
132132
return nil
133133
}

subcmd/migrate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ Available operations`)
5555
return fs
5656
}
5757

58-
func (cmd *migrateCmd) Run(args []string) *Error {
59-
op, err := cmd.parseArgs(args)
58+
func (cmd *migrateCmd) Run(cmdctx *CmdContext) *Error {
59+
op, err := cmd.parseArgs(cmdctx.Args)
6060
if err == ErrShowedHelp {
6161
return nil
6262
}

subcmd/profile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ Quick example
100100
return fs
101101
}
102102

103-
func (cmd *profileCmd) Run(args []string) *Error {
103+
func (cmd *profileCmd) Run(cmdctx *CmdContext) *Error {
104104
// Parse args
105-
args, err := cmd.parseArgs(args)
105+
args, err := cmd.parseArgs(cmdctx.Args)
106106
if err == ErrShowedHelp {
107107
return nil
108108
}

0 commit comments

Comments
 (0)