Skip to content

Commit

Permalink
refactor(cmd/install_bin): Refactor for simple external Go use
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Apr 29, 2023
1 parent 672591c commit 1fb438e
Showing 1 changed file with 75 additions and 32 deletions.
107 changes: 75 additions & 32 deletions cmd/install_bin/install_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ func getUrl(
version string,
getOsFunc func(string) string,
getArchFunc func(string) string,
defaultOSFunc func() string,
defaultArchFunc func() string,
) string {
os_ := getOsFunc(CmdFlagOS)
arch := getArchFunc(CmdFlagArch)
os_ := getOsFunc(defaultOSFunc())
arch := getArchFunc(defaultArchFunc())

funcMap := template.FuncMap{
"capitalize": strings.Title,
Expand Down Expand Up @@ -78,9 +80,11 @@ func getSourcePath(
version string,
getOsFunc func(string) string,
getArchFunc func(string) string,
defaultOSFunc func() string,
defaultArchFunc func() string,
) string {
os_ := getOsFunc(CmdFlagOS)
arch := getArchFunc(CmdFlagArch)
os_ := getOsFunc(defaultOSFunc())
arch := getArchFunc(defaultArchFunc())

funcMap := template.FuncMap{
"capitalize": strings.Title,
Expand Down Expand Up @@ -113,45 +117,33 @@ func buildCmd(
sourceTemlate string,
urlTemplate string,
defaultVersionFunc func() string,
getUrlFunc func(string, string, func(string) string, func(string) string) string,
getSourcePathFunc func(string, string, func(string) string, func(string) string) string,
getUrlFunc func(string, string, func(string) string, func(string) string, func() string, func() string) string,
getSourcePathFunc func(string, string, func(string) string, func(string) string, func() string, func() string) string,
getOsFunc func(string) string,
getArchFunc func(string) string,
defaultOSFunc func() string,
defaultArchFunc func() string,
defaultBinDirFunc func() string,
) *cobra.Command {
var cmd = &cobra.Command{
Use: name,
Short: "Install " + name + " binary",
Aliases: aliases,
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
if sourceTemlate == "" {
sourceTemlate = name
}
version := defaultVersionFunc()
if FlagVersion != "latest" {
version = FlagVersion
}
url := getUrlFunc(
urlTemplate,
version,
getOsFunc,
getArchFunc,
)
if FlagVerbose {
fmt.Println(url)
}
source := getSourcePathFunc(
run(
name,
aliases,
sourceTemlate,
version,
urlTemplate,
defaultVersionFunc,
getUrlFunc,
getSourcePathFunc,
getOsFunc,
getArchFunc,
)
install_bin_utils.InstallBin(
url,
source,
CmdFlagBinDir,
name,
CmdFlagOS == "windows",
defaultOSFunc,
defaultArchFunc,
defaultBinDirFunc,
)
},
}
Expand Down Expand Up @@ -209,7 +201,6 @@ func init() {
if tool.GetArchFunc != nil {
getArchFunc = tool.GetArchFunc
}

Cmd.AddCommand(buildCmd(
tool.Name,
tool.Aliases,
Expand All @@ -220,6 +211,9 @@ func init() {
getSourcePath,
getOsFunc,
getArchFunc,
func() string { return CmdFlagOS },
func() string { return CmdFlagArch },
func() string { return CmdFlagBinDir },
))
}
}
Expand Down Expand Up @@ -253,3 +247,52 @@ func k6_Os(osName string) string {
}
return osName
}

func run(
name string,
aliases []string,
sourceTemlate string,
urlTemplate string,
defaultVersionFunc func() string,
getUrlFunc func(string, string, func(string) string, func(string) string, func() string, func() string) string,
getSourcePathFunc func(string, string, func(string) string, func(string) string, func() string, func() string) string,
getOsFunc func(string) string,
getArchFunc func(string) string,
defaultOSFunc func() string,
defaultArchFunc func() string,
defaultBinDirFunc func() string,
) {
if sourceTemlate == "" {
sourceTemlate = name
}
version := defaultVersionFunc()
if FlagVersion != "latest" {
version = FlagVersion
}
url := getUrlFunc(
urlTemplate,
version,
getOsFunc,
getArchFunc,
defaultOSFunc,
defaultArchFunc,
)
if FlagVerbose {
fmt.Println(url)
}
source := getSourcePathFunc(
sourceTemlate,
version,
getOsFunc,
getArchFunc,
defaultOSFunc,
defaultArchFunc,
)
install_bin_utils.InstallBin(
url,
source,
defaultBinDirFunc(),
name,
defaultOSFunc() == "windows",
)
}

0 comments on commit 1fb438e

Please sign in to comment.