diff options
| author | Shulhan <ms@kilabit.info> | 2018-05-29 18:18:15 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-05-30 00:53:50 +0700 |
| commit | 5b578463d2e5f07e26f9726f656637e87ad840b1 (patch) | |
| tree | 8955950c0c79f707e606e64916bde038c90ed02c | |
| parent | 47ff3edb37ac53d5bdbffcb02807d8bb255c5b99 (diff) | |
| download | beku-5b578463d2e5f07e26f9726f656637e87ad840b1.tar.xz | |
Add option "--noconfirm" to by pass confirmation
| -rw-r--r-- | README.md | 27 | ||||
| -rw-r--r-- | cmd/beku/command.go | 9 | ||||
| -rw-r--r-- | cmd/beku/command_test.go | 10 | ||||
| -rw-r--r-- | cmd/beku/main.go | 10 | ||||
| -rw-r--r-- | env.go | 67 |
5 files changed, 90 insertions, 33 deletions
@@ -22,6 +22,23 @@ directory. If no file found, it will try to open package database, beku will scan entire "$GOPATH/src" and write the package database into "$GOPATH/var/beku/beku.db". +## Global Options + + --noconfirm + +No confirmation will be asked on any operation. Useful when running beku +inside a script. + + +## Freeze Operation + + -B, --freeze + +Operate on the package database and GOPATH. This operation will ensure that +all packages listed on database file is installed with their specific +version on GOPATH. Also, all packages that are not registered will be +removed from GOPATH "src" and "pkg" directories. + ## Database Operation @@ -45,16 +62,6 @@ Exclude package "github.com/shuLhan/beku" from future scanning, installation, or removal. -## Freeze Operation - - -B, --freeze - -Operate on the package database and GOPATH. This operation will ensure that -all packages listed on database file is installed with their specific -version on GOPATH. Also, all packages that are not registered will be -removed from GOPATH "src" and "pkg" directories. - - ## Query Operation -Q, --query [pkg ...] diff --git a/cmd/beku/command.go b/cmd/beku/command.go index 700117c..f8cde00 100644 --- a/cmd/beku/command.go +++ b/cmd/beku/command.go @@ -24,6 +24,7 @@ const ( flagOperationRemove = "Remove package." flagOperationSync = "Synchronize package. If no package is given, it will do rescan." + flagOptionNoConfirm = "No confirmation will be asked on any operation." flagOptionRecursive = "Remove package including their dependencies." flagOptionSyncInto = "Download package into `directory`." flagOptionUpdate = "Update all packages to latest version." @@ -35,10 +36,14 @@ type command struct { pkgs []string syncInto string firstTime bool + noConfirm bool } func (cmd *command) usage() { help := `usage: beku <operation> [...] +common options: + --noconfirm + ` + flagOptionNoConfirm + ` operations: beku {-h|--help} ` + flagOperationHelp + ` @@ -189,6 +194,8 @@ func (cmd *command) parseLongFlags(arg string) (op operation, err error) { op = opFreeze case "into": op = opSyncInto + case "noconfirm": + cmd.noConfirm = true case "query": op = opQuery case "recursive": @@ -262,7 +269,7 @@ func (cmd *command) parseFlags(args []string) (err error) { return } } - if cmd.op == opExclude || cmd.op == opRecursive || + if cmd.op == opNone || cmd.op == opExclude || cmd.op == opRecursive || cmd.op == opSyncInto || cmd.op == opUpdate { return errInvalidOptions } diff --git a/cmd/beku/command_test.go b/cmd/beku/command_test.go index 8478c30..573f1f2 100644 --- a/cmd/beku/command_test.go +++ b/cmd/beku/command_test.go @@ -30,6 +30,9 @@ func TestParseFlags(t *testing.T) { args: []string{"--into"}, expErr: errInvalidOptions.Error(), }, { + args: []string{"--noconfirm"}, + expErr: errInvalidOptions.Error(), + }, { args: []string{"--update"}, expErr: errInvalidOptions.Error(), }, { @@ -170,6 +173,13 @@ func TestParseFlags(t *testing.T) { pkgs: []string{"A"}, }, }, { + args: []string{"-Rs", "A", "--noconfirm"}, + expCmd: &command{ + op: opRemove | opRecursive, + noConfirm: true, + pkgs: []string{"A"}, + }, + }, { args: []string{"-Rx", "A"}, expErr: errInvalidOptions.Error(), }, { diff --git a/cmd/beku/main.go b/cmd/beku/main.go index bbf6a28..6226e1b 100644 --- a/cmd/beku/main.go +++ b/cmd/beku/main.go @@ -14,6 +14,14 @@ // package database, beku will scan entire "$GOPATH/src" and write the // package database into "$GOPATH/var/beku/beku.db". // +// ## Global Options +// +// --noconfirm +// +// No confirmation will be asked on any operation. Useful when running beku +// inside a script. +// +// // ## Freeze Operation // // -B, --freeze @@ -170,6 +178,8 @@ func main() { os.Exit(1) } + cmd.env.NoConfirm = cmd.noConfirm + switch cmd.op { case opHelp: cmd.usage() @@ -42,6 +42,7 @@ type Env struct { countUpdate int fmtMaxPath int dirty bool + NoConfirm bool } // @@ -183,9 +184,13 @@ func (env *Env) Freeze() (err error) { fmt.Println() - ok = confirm(os.Stdin, msgContinue, false) - if ok { + if env.NoConfirm { env.cleanUnused() + } else { + ok = confirm(os.Stdin, msgContinue, false) + if ok { + env.cleanUnused() + } } out: @@ -636,9 +641,13 @@ func (env *Env) Rescan() (ok bool, err error) { fmt.Println() - ok = confirm(os.Stdin, msgContinue, false) - if !ok { - return + if env.NoConfirm { + ok = true + } else { + ok = confirm(os.Stdin, msgContinue, false) + if !ok { + return + } } if env.countUpdate > 0 { @@ -712,9 +721,11 @@ This package is required by, fmt.Println(" *", importPath) } - ok := confirm(os.Stdin, msgContinue, false) - if !ok { - return + if !env.NoConfirm { + ok := confirm(os.Stdin, msgContinue, false) + if !ok { + return + } } for _, importPath := range listRemoved { @@ -944,9 +955,11 @@ func (env *Env) String() string { func (env *Env) install(pkg *Package) (ok bool, err error) { if !IsDirEmpty(pkg.FullPath) { fmt.Printf(">>> Directory %s is not empty.\n", pkg.FullPath) - ok = confirm(os.Stdin, msgContinue, false) - if !ok { - return + if !env.NoConfirm { + ok = confirm(os.Stdin, msgContinue, false) + if !ok { + return + } } } @@ -985,17 +998,25 @@ func (env *Env) update(curPkg, newPkg *Package) (ok bool, err error) { fmt.Printf("Updating package from,\n%s\nto,\n%s\n", curPkg.String(), newPkg.String()) - ok = confirm(os.Stdin, msgUpdateView, false) - if ok { - err = curPkg.CompareVersion(newPkg) - if err != nil { - return + if env.NoConfirm { + ok = true + } else { + ok = confirm(os.Stdin, msgUpdateView, false) + if ok { + err = curPkg.CompareVersion(newPkg) + if err != nil { + return + } } } - ok = confirm(os.Stdin, msgUpdateProceed, true) - if !ok { - return + if env.NoConfirm { + ok = true + } else { + ok = confirm(os.Stdin, msgUpdateProceed, true) + if !ok { + return + } } err = curPkg.Update(newPkg) @@ -1168,9 +1189,11 @@ func (env *Env) SyncAll() (err error) { fmt.Println(buf.String()) - ok := confirm(os.Stdin, msgContinue, false) - if !ok { - return + if !env.NoConfirm { + ok := confirm(os.Stdin, msgContinue, false) + if !ok { + return + } } for _, pkg := range env.pkgs { |
