From 5b578463d2e5f07e26f9726f656637e87ad840b1 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 29 May 2018 18:18:15 +0700 Subject: Add option "--noconfirm" to by pass confirmation --- README.md | 27 +++++++++++-------- cmd/beku/command.go | 9 ++++++- cmd/beku/command_test.go | 10 ++++++++ cmd/beku/main.go | 10 ++++++++ env.go | 67 ++++++++++++++++++++++++++++++++---------------- 5 files changed, 90 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index ada0dcf..f095098 100644 --- a/README.md +++ b/README.md @@ -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 [...] +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 @@ -29,6 +29,9 @@ func TestParseFlags(t *testing.T) { }, { args: []string{"--into"}, expErr: errInvalidOptions.Error(), + }, { + args: []string{"--noconfirm"}, + expErr: errInvalidOptions.Error(), }, { args: []string{"--update"}, expErr: errInvalidOptions.Error(), @@ -169,6 +172,13 @@ func TestParseFlags(t *testing.T) { op: opRemove | opRecursive, 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() diff --git a/env.go b/env.go index 0495faa..c30c58d 100644 --- a/env.go +++ b/env.go @@ -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 { -- cgit v1.3