Skip to content

Commit 34e53ea

Browse files
committed
refactor: list,version: move core logic to usecase.*
1 parent d966a63 commit 34e53ea

File tree

12 files changed

+217
-172
lines changed

12 files changed

+217
-172
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
NAME := volt
33
SRC := $(shell find . -type d -name 'vendor' -prune -o -type f -name '*.go' -print)
4-
VERSION := $(shell sed -n -E 's/var voltVersion = "([^"]+)"/\1/p' gateway/version.go)
4+
VERSION := $(shell sed -n -E 's/var VoltVersion = "([^"]+)"/\1/p' usecase/version.go)
55
RELEASE_LDFLAGS := -s -w -extldflags '-static'
66
RELEASE_OS := linux windows darwin
77
RELEASE_ARCH := amd64 386

gateway/disable.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ func (cmd *disableCmd) Run(cmdctx *CmdContext) *Error {
5151
}
5252

5353
profCmd := profileCmd{}
54-
err = profCmd.doRm(append(
54+
cmdctx.Args = append(
5555
[]string{"-current"},
5656
reposPathList.Strings()...,
57-
))
57+
)
58+
err = profCmd.doRm(cmdctx)
5859
if err != nil {
5960
return &Error{Code: 11, Msg: err.Error()}
6061
}

gateway/enable.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ func (cmd *enableCmd) Run(cmdctx *CmdContext) *Error {
5151
}
5252

5353
profCmd := profileCmd{}
54-
err = profCmd.doAdd(append(
54+
cmdctx.Args = append(
5555
[]string{"-current"},
5656
reposPathList.Strings()...,
57-
))
57+
)
58+
err = profCmd.doAdd(cmdctx)
5859
if err != nil {
5960
return &Error{Code: 11, Msg: err.Error()}
6061
}

gateway/list.go

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package gateway
22

33
import (
4-
"encoding/json"
54
"flag"
65
"fmt"
7-
"github.com/pkg/errors"
6+
"io"
87
"os"
9-
"text/template"
108

9+
"github.com/vim-volt/volt/config"
1110
"github.com/vim-volt/volt/lockjson"
11+
"github.com/vim-volt/volt/usecase"
1212
)
1313

1414
func init() {
15-
cmdMap["list"] = &listCmd{}
15+
cmdMap["list"] = &listCmd{
16+
List: usecase.List,
17+
}
1618
}
1719

1820
type listCmd struct {
1921
helped bool
2022
format string
23+
24+
List func(w io.Writer, format string, lockJSON *lockjson.LockJSON, cfg *config.Config) error
2125
}
2226

2327
func (cmd *listCmd) ProhibitRootExecution(args []string) bool { return false }
@@ -131,64 +135,8 @@ func (cmd *listCmd) Run(cmdctx *CmdContext) *Error {
131135
if cmd.helped {
132136
return nil
133137
}
134-
if err := cmd.list(cmd.format); err != nil {
138+
if err := cmd.List(os.Stdout, cmd.format, cmdctx.LockJSON, cmdctx.Config); err != nil {
135139
return &Error{Code: 10, Msg: "Failed to render template: " + err.Error()}
136140
}
137141
return nil
138142
}
139-
140-
func (cmd *listCmd) list(format string) error {
141-
// Read lock.json
142-
lockJSON, err := lockjson.Read()
143-
if err != nil {
144-
return errors.Wrap(err, "failed to read lock.json")
145-
}
146-
// Parse template string
147-
t, err := template.New("volt").Funcs(cmd.funcMap(lockJSON)).Parse(format)
148-
if err != nil {
149-
return err
150-
}
151-
// Output templated information
152-
return t.Execute(os.Stdout, lockJSON)
153-
}
154-
155-
func (*listCmd) funcMap(lockJSON *lockjson.LockJSON) template.FuncMap {
156-
profileOf := func(name string) *lockjson.Profile {
157-
profile, err := lockJSON.Profiles.FindByName(name)
158-
if err != nil {
159-
return &lockjson.Profile{}
160-
}
161-
return profile
162-
}
163-
164-
return template.FuncMap{
165-
"json": func(value interface{}, args ...string) string {
166-
var b []byte
167-
switch len(args) {
168-
case 0:
169-
b, _ = json.MarshalIndent(value, "", "")
170-
case 1:
171-
b, _ = json.MarshalIndent(value, args[0], "")
172-
default:
173-
b, _ = json.MarshalIndent(value, args[0], args[1])
174-
}
175-
return string(b)
176-
},
177-
"currentProfile": func() *lockjson.Profile {
178-
return profileOf(lockJSON.CurrentProfileName)
179-
},
180-
"profile": profileOf,
181-
"version": func() string {
182-
return voltVersion
183-
},
184-
"versionMajor": func() int {
185-
return voltVersionInfo()[0]
186-
},
187-
"versionMinor": func() int {
188-
return voltVersionInfo()[1]
189-
},
190-
"versionPatch": func() int {
191-
return voltVersionInfo()[2]
192-
},
193-
}
194-
}

gateway/profile.go

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/vim-volt/volt/logger"
1414
"github.com/vim-volt/volt/pathutil"
1515
"github.com/vim-volt/volt/transaction"
16+
"github.com/vim-volt/volt/usecase"
1617
)
1718

1819
type profileCmd struct {
@@ -110,26 +111,28 @@ func (cmd *profileCmd) Run(cmdctx *CmdContext) *Error {
110111
return &Error{Code: 10, Msg: err.Error()}
111112
}
112113

113-
gateway := args[0]
114-
switch gateway {
114+
subCmd := args[0]
115+
cmdctx.Args = args[1:]
116+
117+
switch subCmd {
115118
case "set":
116-
err = cmd.doSet(args[1:])
119+
err = cmd.doSet(cmdctx)
117120
case "show":
118-
err = cmd.doShow(args[1:])
121+
err = cmd.doShow(cmdctx)
119122
case "list":
120-
err = cmd.doList(args[1:])
123+
err = cmd.doList(cmdctx)
121124
case "new":
122-
err = cmd.doNew(args[1:])
125+
err = cmd.doNew(cmdctx)
123126
case "destroy":
124-
err = cmd.doDestroy(args[1:])
127+
err = cmd.doDestroy(cmdctx)
125128
case "rename":
126-
err = cmd.doRename(args[1:])
129+
err = cmd.doRename(cmdctx)
127130
case "add":
128-
err = cmd.doAdd(args[1:])
131+
err = cmd.doAdd(cmdctx)
129132
case "rm":
130-
err = cmd.doRm(args[1:])
133+
err = cmd.doRm(cmdctx)
131134
default:
132-
return &Error{Code: 11, Msg: "Unknown subcommand: " + gateway}
135+
return &Error{Code: 11, Msg: "Unknown subcommand: " + subCmd}
133136
}
134137

135138
if err != nil {
@@ -161,7 +164,8 @@ func (*profileCmd) getCurrentProfile() (string, error) {
161164
return lockJSON.CurrentProfileName, nil
162165
}
163166

164-
func (cmd *profileCmd) doSet(args []string) error {
167+
func (cmd *profileCmd) doSet(cmdctx *CmdContext) error {
168+
args := cmdctx.Args
165169
// Parse args
166170
createProfile := false
167171
if len(args) > 0 && args[0] == "-n" {
@@ -191,7 +195,8 @@ func (cmd *profileCmd) doSet(args []string) error {
191195
if !createProfile {
192196
return err
193197
}
194-
if err = cmd.doNew([]string{profileName}); err != nil {
198+
cmdctx.Args = []string{profileName}
199+
if err = cmd.doNew(cmdctx); err != nil {
195200
return err
196201
}
197202
// Read lock.json again
@@ -231,7 +236,8 @@ func (cmd *profileCmd) doSet(args []string) error {
231236
return nil
232237
}
233238

234-
func (cmd *profileCmd) doShow(args []string) error {
239+
func (cmd *profileCmd) doShow(cmdctx *CmdContext) error {
240+
args := cmdctx.Args
235241
if len(args) == 0 {
236242
cmd.FlagSet().Usage()
237243
logger.Error("'volt profile show' receives profile name.")
@@ -254,25 +260,28 @@ func (cmd *profileCmd) doShow(args []string) error {
254260
}
255261
}
256262

257-
return (&listCmd{}).list(fmt.Sprintf(`name: %s
263+
format := fmt.Sprintf(`name: %s
258264
repos path:
259265
{{- with profile %q -}}
260266
{{- range .ReposPath }}
261267
{{ . }}
262268
{{- end -}}
263269
{{- end }}
264-
`, profileName, profileName))
270+
`, profileName, profileName)
271+
return usecase.List(os.Stdout, format, cmdctx.LockJSON, cmdctx.Config)
265272
}
266273

267-
func (cmd *profileCmd) doList(args []string) error {
268-
return (&listCmd{}).list(`
274+
func (cmd *profileCmd) doList(cmdctx *CmdContext) error {
275+
format := `
269276
{{- range .Profiles -}}
270277
{{- if eq .Name $.CurrentProfileName -}}*{{- else }} {{ end }} {{ .Name }}
271278
{{ end -}}
272-
`)
279+
`
280+
return usecase.List(os.Stdout, format, cmdctx.LockJSON, cmdctx.Config)
273281
}
274282

275-
func (cmd *profileCmd) doNew(args []string) error {
283+
func (cmd *profileCmd) doNew(cmdctx *CmdContext) error {
284+
args := cmdctx.Args
276285
if len(args) == 0 {
277286
cmd.FlagSet().Usage()
278287
logger.Error("'volt profile new' receives profile name.")
@@ -316,7 +325,8 @@ func (cmd *profileCmd) doNew(args []string) error {
316325
return nil
317326
}
318327

319-
func (cmd *profileCmd) doDestroy(args []string) error {
328+
func (cmd *profileCmd) doDestroy(cmdctx *CmdContext) error {
329+
args := cmdctx.Args
320330
if len(args) == 0 {
321331
cmd.FlagSet().Usage()
322332
logger.Error("'volt profile destroy' receives profile name.")
@@ -374,7 +384,8 @@ func (cmd *profileCmd) doDestroy(args []string) error {
374384
return merr.ErrorOrNil()
375385
}
376386

377-
func (cmd *profileCmd) doRename(args []string) error {
387+
func (cmd *profileCmd) doRename(cmdctx *CmdContext) error {
388+
args := cmdctx.Args
378389
if len(args) != 2 {
379390
cmd.FlagSet().Usage()
380391
logger.Error("'volt profile rename' receives profile name.")
@@ -433,7 +444,8 @@ func (cmd *profileCmd) doRename(args []string) error {
433444
return nil
434445
}
435446

436-
func (cmd *profileCmd) doAdd(args []string) error {
447+
func (cmd *profileCmd) doAdd(cmdctx *CmdContext) error {
448+
args := cmdctx.Args
437449
// Read lock.json
438450
lockJSON, err := lockjson.Read()
439451
if err != nil {
@@ -475,7 +487,8 @@ func (cmd *profileCmd) doAdd(args []string) error {
475487
return nil
476488
}
477489

478-
func (cmd *profileCmd) doRm(args []string) error {
490+
func (cmd *profileCmd) doRm(cmdctx *CmdContext) error {
491+
args := cmdctx.Args
479492
// Read lock.json
480493
lockJSON, err := lockjson.Read()
481494
if err != nil {
@@ -519,10 +532,10 @@ func (cmd *profileCmd) doRm(args []string) error {
519532
return nil
520533
}
521534

522-
func (cmd *profileCmd) parseAddArgs(lockJSON *lockjson.LockJSON, gateway string, args []string) (string, []pathutil.ReposPath, error) {
535+
func (cmd *profileCmd) parseAddArgs(lockJSON *lockjson.LockJSON, subCmd string, args []string) (string, []pathutil.ReposPath, error) {
523536
if len(args) == 0 {
524537
cmd.FlagSet().Usage()
525-
logger.Errorf("'volt profile %s' receives profile name and one or more repositories.", gateway)
538+
logger.Errorf("'volt profile %s' receives profile name and one or more repositories.", subCmd)
526539
return "", nil, nil
527540
}
528541

gateway/self_upgrade.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/vim-volt/volt/httputil"
2020
"github.com/vim-volt/volt/logger"
21+
"github.com/vim-volt/volt/usecase"
2122
)
2223

2324
func init() {
@@ -139,15 +140,15 @@ func (cmd *selfUpgradeCmd) doSelfUpgrade(latestURL string) error {
139140
return err
140141
}
141142
logger.Debugf("tag_name = %q", release.TagName)
142-
tagNameVer, err := parseVersion(release.TagName)
143+
tagNameVer, err := usecase.ParseVersion(release.TagName)
143144
if err != nil {
144145
return err
145146
}
146-
if compareVersion(tagNameVer, voltVersionInfo()) <= 0 {
147+
if usecase.CompareVersion(tagNameVer, usecase.Version()) <= 0 {
147148
logger.Info("No updates were found.")
148149
return nil
149150
}
150-
logger.Infof("Found update: %s -> %s", voltVersion, release.TagName)
151+
logger.Infof("Found update: %s -> %s", usecase.VersionString(), release.TagName)
151152

152153
// Show release note
153154
fmt.Println("---")

0 commit comments

Comments
 (0)