diff options
| author | Shulhan <ms@kilabit.info> | 2018-05-24 22:24:24 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-05-25 06:42:45 +0700 |
| commit | 95c8077c3e84e93d4ab892e3e210715ebf6a3483 (patch) | |
| tree | 551d0fb455a9dd3672d4403b64df3857c8c904cc | |
| parent | bb551cb628cc3a7fd7fb34000c64ac51b7b1d153 (diff) | |
| download | beku-95c8077c3e84e93d4ab892e3e210715ebf6a3483.tar.xz | |
Implement sync that rescan package in GOPATH
| -rw-r--r-- | cmd/beku/command.go | 2 | ||||
| -rw-r--r-- | cmd/beku/command_test.go | 12 | ||||
| -rw-r--r-- | cmd/beku/main.go | 9 | ||||
| -rw-r--r-- | env.go | 87 | ||||
| -rw-r--r-- | env_test.go | 3 | ||||
| -rw-r--r-- | package.go | 12 | ||||
| -rw-r--r-- | package_test.go | 1 | ||||
| -rw-r--r-- | packagestate.go | 10 |
8 files changed, 127 insertions, 9 deletions
diff --git a/cmd/beku/command.go b/cmd/beku/command.go index 15ee035..80a79d0 100644 --- a/cmd/beku/command.go +++ b/cmd/beku/command.go @@ -207,7 +207,7 @@ func (cmd *command) parseFlags(args []string) (err error) { } // (2) - if op == opSync || op == opRemove { + if op == opRemove { if len(cmd.pkgs) == 0 { return errNoTarget } diff --git a/cmd/beku/command_test.go b/cmd/beku/command_test.go index 48c8631..b64d3f4 100644 --- a/cmd/beku/command_test.go +++ b/cmd/beku/command_test.go @@ -78,11 +78,15 @@ func TestParseFlags(t *testing.T) { pkgs: []string{"A", "B"}, }, }, { - args: []string{"-S"}, - expErr: errNoTarget.Error(), + args: []string{"-S"}, + expCmd: &command{ + op: opSync, + }, }, { - args: []string{"--sync"}, - expErr: errNoTarget.Error(), + args: []string{"--sync"}, + expCmd: &command{ + op: opSync, + }, }, { args: []string{"-S", "package", "another"}, expCmd: &command{ diff --git a/cmd/beku/main.go b/cmd/beku/main.go index 7460cb4..7a90341 100644 --- a/cmd/beku/main.go +++ b/cmd/beku/main.go @@ -64,6 +64,9 @@ // // Sync operation will not install missing dependencies. // +// If no parameter is given, beku will rescan GOPATH, checking for new +// packages. +// // ### Options // // [--into <destination>] @@ -135,7 +138,11 @@ func main() { case opRemove | opRecursive: err = cmd.env.Remove(cmd.pkgs[0], true) case opSync: - err = cmd.env.Sync(cmd.pkgs[0], "") + if len(cmd.pkgs) > 0 { + err = cmd.env.Sync(cmd.pkgs[0], "") + } else { + err = cmd.env.Rescan() + } case opSync | opSyncInto: err = cmd.env.Sync(cmd.pkgs[0], cmd.syncInto) default: @@ -36,6 +36,8 @@ type Env struct { db *ini.Ini dbDefFile string dbFile string + countNew int + countUpdate int dirty bool } @@ -228,7 +230,17 @@ func (env *Env) newPackage(fullPath string, vcsMode VCSMode) (err error) { return fmt.Errorf("%s: %s", pkgName, err) } - env.pkgs = append(env.pkgs, pkg) + curPkg := env.GetPackage(pkg.ImportPath, pkg.RemoteURL) + if curPkg == nil { + env.pkgs = append(env.pkgs, pkg) + env.countNew++ + } else { + if curPkg.Version != pkg.Version { + curPkg.VersionNext = pkg.Version + curPkg.state = packageStateChange + env.countUpdate++ + } + } return nil } @@ -284,6 +296,7 @@ func (env *Env) Load(file string) (err error) { pkg := &Package{ ImportPath: sec.Sub, FullPath: filepath.Join(env.dirSrc, sec.Sub), + state: packageStateLoad, } pkg.load(sec) @@ -314,6 +327,78 @@ func (env *Env) Query(pkgs []string) { } // +// Rescan GOPATH for new packages. +// +func (env *Env) Rescan() (err error) { + err = env.Scan() + if err != nil { + return + } + + if env.countUpdate > 0 { + fmt.Printf(">>> The following packages will be updated,\n\n") + fmt.Printf("ImportPath\tOld Version\tNew Version\n\n") + + for _, pkg := range env.pkgs { + if pkg.state&packageStateChange == 0 { + continue + } + + fmt.Printf("%s\t%s\t%s\n", pkg.ImportPath, + pkg.Version, pkg.VersionNext) + } + } + if env.countNew > 0 { + fmt.Printf("\n>>> New packages,\n\n") + fmt.Printf("ImportPath\tVersion\n\n") + + for _, pkg := range env.pkgs { + if pkg.state&packageStateNew == 0 { + continue + } + + fmt.Printf("%s\t%s\n", pkg.ImportPath, pkg.Version) + } + } + + if env.countUpdate == 0 && env.countNew == 0 { + fmt.Println(">>> Database and GOPATH is in sync.") + return + } + + fmt.Println() + + ok := confirm(os.Stdin, msgContinue, false) + if !ok { + return + } + + if env.countUpdate > 0 { + for x, pkg := range env.pkgs { + if pkg.state&packageStateChange == 0 { + continue + } + + env.pkgs[x].Version = env.pkgs[x].VersionNext + env.pkgs[x].VersionNext = "" + env.pkgs[x].state = packageStateDirty + } + } + if env.countNew > 0 { + for _, pkg := range env.pkgs { + if pkg.state&packageStateNew == 0 { + continue + } + pkg.state = packageStateDirty + env.updateMissing(pkg) + } + } + env.dirty = true + + return +} + +// // Remove package from GOPATH. If recursive is true, it will also remove their // dependencies, as long as they are not required by other package. // diff --git a/env_test.go b/env_test.go index 51ed69c..62ade9c 100644 --- a/env_test.go +++ b/env_test.go @@ -57,6 +57,7 @@ func testEnvGetPackage(t *testing.T) { RemoteURL: "https://github.com/alecthomas/gometalinter", Version: "0725fc6", vcs: VCSModeGit, + state: packageStateLoad, Deps: []string{ "github.com/stretchr/testify", "gotest.tools", @@ -75,6 +76,7 @@ func testEnvGetPackage(t *testing.T) { Version: "v2.0.0", isTag: true, vcs: VCSModeGit, + state: packageStateLoad, Deps: []string{ "github.com/pkg/errors", "golang.org/x/tools", @@ -317,6 +319,7 @@ func testEnvScan(t *testing.T) { RemoteURL: "git@github.com:shuLhan/beku_test.git", Version: "c9f69fb", vcs: VCSModeGit, + state: packageStateNew, DepsMissing: []string{ "github.com/shuLhan/share/lib/text", }, @@ -32,6 +32,7 @@ type Package struct { Deps []string RequiredBy []string vcs VCSMode + state packageState isTag bool } @@ -46,6 +47,7 @@ func NewPackage(pkgName, importPath string, vcsMode VCSMode) ( RemoteURL: "https://" + pkgName, FullPath: filepath.Join(build.Default.GOPATH, dirSrc, importPath), vcs: vcsMode, + state: packageStateNew, } switch vcsMode { @@ -220,7 +222,7 @@ func (pkg *Package) Scan() (err error) { // only external dependencies. // func (pkg *Package) ScanDeps(env *Env) (err error) { - fmt.Println(">>> Scanning dependencies ...") + fmt.Printf(">>> Scanning dependencies for %s ...\n", pkg.ImportPath) imports, err := pkg.GetRecursiveImports() if err != nil { @@ -247,7 +249,9 @@ func (pkg *Package) GetRecursiveImports() ( ) { //nolint:gas cmd := exec.Command("go", "list", "-e", "-f", `{{ join .Deps "\n"}}`, "./...") - fmt.Println(">>>", cmd.Args) + if Debug >= DebugL1 { + fmt.Println(">>>", cmd.Args) + } cmd.Dir = pkg.FullPath cmd.Stderr = defStderr @@ -277,6 +281,10 @@ func (pkg *Package) GetRecursiveImports() ( sort.Strings(imports) + if Debug >= DebugL1 { + fmt.Println(">>> imports:", imports) + } + return } diff --git a/package_test.go b/package_test.go index 81e2ecb..694f463 100644 --- a/package_test.go +++ b/package_test.go @@ -62,6 +62,7 @@ func testPackageInstall(t *testing.T) { RemoteURL: "https://" + testGitRepoShare, Version: "9337967", vcs: VCSModeGit, + state: packageStateNew, }, }, { desc: `Install again`, diff --git a/packagestate.go b/packagestate.go new file mode 100644 index 0000000..913b689 --- /dev/null +++ b/packagestate.go @@ -0,0 +1,10 @@ +package beku + +type packageState uint + +const ( + packageStateNew packageState = 1 << iota + packageStateLoad + packageStateChange + packageStateDirty +) |
