Skip to content
This repository was archived by the owner on Jun 11, 2021. It is now read-only.

Commit f42e6bd

Browse files
committed
Speed everything up using GoRoutine and WaitGroup
1 parent b688260 commit f42e6bd

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

cmd/install.go

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"path"
2929
"regexp"
3030
"strings"
31+
"sync"
3132

3233
"github.com/otiai10/copy"
3334
"github.com/spf13/cobra"
@@ -49,45 +50,55 @@ var installCmd = &cobra.Command{
4950
os.RemoveAll(VendorDir)
5051
os.MkdirAll(VendorDir, os.ModePerm)
5152

53+
var wg sync.WaitGroup
54+
wg.Add(len(Config))
55+
5256
for moduleName, moduleMeta := range Config {
53-
moduleSource := moduleMeta.Source
54-
moduleVersion := "master"
55-
if len(moduleMeta.Version) > 0 {
56-
moduleVersion = moduleMeta.Version
57-
}
58-
modulePath := moduleMeta.Path
59-
60-
directory := path.Join(VendorDir, moduleName)
61-
62-
switch {
63-
case strings.HasPrefix(moduleSource, "./") || strings.HasPrefix(
64-
moduleSource, "../") || strings.HasPrefix(moduleSource, "/"):
65-
copyFile(moduleName, moduleSource, directory)
66-
case validRegistry(moduleSource):
67-
source, version := getRegistrySource(moduleName, moduleSource, moduleVersion)
68-
gitCheckout(moduleName, source, version, directory)
69-
case IContains(moduleSource, "git"):
70-
gitCheckout(moduleName, moduleSource, moduleVersion, directory)
71-
}
72-
73-
// If we have a path specified, let's extract it (move and copy stuff).
74-
if len(modulePath) > 0 {
75-
tmpDirectory := directory + ".tmp"
76-
pathWanted := path.Join(tmpDirectory, modulePath)
77-
78-
err := os.Rename(directory, tmpDirectory)
79-
CheckIfError(err)
80-
81-
err = copy.Copy(pathWanted, directory)
82-
CheckIfError(err)
83-
os.RemoveAll(tmpDirectory)
84-
}
85-
// Cleanup .git directoriy
86-
os.RemoveAll(path.Join(directory, ".git"))
57+
go getModule(moduleName, moduleMeta, &wg)
8758
}
59+
wg.Wait()
8860
},
8961
}
9062

63+
func getModule(moduleName string, moduleMeta module, wg *sync.WaitGroup) {
64+
defer wg.Done()
65+
66+
moduleSource := moduleMeta.Source
67+
moduleVersion := "master"
68+
if len(moduleMeta.Version) > 0 {
69+
moduleVersion = moduleMeta.Version
70+
}
71+
modulePath := moduleMeta.Path
72+
73+
directory := path.Join(VendorDir, moduleName)
74+
75+
switch {
76+
case strings.HasPrefix(moduleSource, "./") || strings.HasPrefix(
77+
moduleSource, "../") || strings.HasPrefix(moduleSource, "/"):
78+
copyFile(moduleName, moduleSource, directory)
79+
case validRegistry(moduleSource):
80+
source, version := getRegistrySource(moduleName, moduleSource, moduleVersion)
81+
gitCheckout(moduleName, source, version, directory)
82+
case IContains(moduleSource, "git"):
83+
gitCheckout(moduleName, moduleSource, moduleVersion, directory)
84+
}
85+
86+
// If we have a path specified, let's extract it (move and copy stuff).
87+
if len(modulePath) > 0 {
88+
tmpDirectory := directory + ".tmp"
89+
pathWanted := path.Join(tmpDirectory, modulePath)
90+
91+
err := os.Rename(directory, tmpDirectory)
92+
CheckIfError(err)
93+
94+
err = copy.Copy(pathWanted, directory)
95+
CheckIfError(err)
96+
os.RemoveAll(tmpDirectory)
97+
}
98+
// Cleanup .git directoriy
99+
os.RemoveAll(path.Join(directory, ".git"))
100+
}
101+
91102
func init() {
92103
jww.SetStdoutThreshold(jww.LevelInfo)
93104
rootCmd.AddCommand(installCmd)

0 commit comments

Comments
 (0)