Skip to content

Commit

Permalink
include system url as template
Browse files Browse the repository at this point in the history
Signed-off-by: Nitishkumar Singh <[email protected]>

move powershell and go to tools

Signed-off-by: Nitishkumar Singh <[email protected]>
  • Loading branch information
nitishkumar71 committed Jul 1, 2024
1 parent dbe95ca commit 5c9e788
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 71 deletions.
2 changes: 1 addition & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// MakeGet creates the Get command to download software
func MakeGet() *cobra.Command {
tools := get.MakeTools()
tools := get.MakeToolsWithoutSystemApp()
sort.Sort(tools)
var validToolOptions []string = make([]string, len(tools))
for _, t := range tools {
Expand Down
47 changes: 18 additions & 29 deletions cmd/system/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"github.com/alexellis/arkade/pkg"
"github.com/alexellis/arkade/pkg/archive"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,13 +52,17 @@ func MakeInstallGo() *cobra.Command {
return fmt.Errorf("this app only supports Linux")
}

dlArch := arch
if arch == "x86_64" {
dlArch = "amd64"
} else if arch == "aarch64" {
dlArch = "arm64"
} else if arch == "armv7" || arch == "armv7l" {
dlArch = "armv6l"
tools := get.MakeTools()
var tool *get.Tool
for _, t := range tools {
if t.Name == "go" {
tool = &t
break
}
}

if tool == nil {
return fmt.Errorf("unable to find go definition")
}

if len(version) == 0 {
Expand All @@ -73,31 +76,13 @@ func MakeInstallGo() *cobra.Command {
version = "go" + version
}

fmt.Printf("Installing version: %s for: %s\n", version, dlArch)

dlURL := fmt.Sprintf("https://go.dev/dl/%s.%s-%s.tar.gz", version, strings.ToLower(osVer), dlArch)
fmt.Printf("Downloading from: %s\n", dlURL)

progress, _ := cmd.Flags().GetBool("progress")
outPath, err := get.DownloadFileP(dlURL, progress)
err := get.DownloadNested(tool, arch, osVer, version, installPath, progress, !progress)
if err != nil {
return err
}
defer os.Remove(outPath)

fmt.Printf("Downloaded to: %s\n", outPath)

f, err := os.OpenFile(outPath, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer f.Close()

fmt.Printf("Unpacking Go to: %s\n", path.Join(installPath, "go"))

if err := archive.UntarNested(f, installPath, true, false); err != nil {
return err
}
fmt.Printf("Downloaded to: %sgo\n", installPath)

fmt.Printf("\nexport PATH=$PATH:%s:$HOME/go/bin\n"+
"export GOPATH=$HOME/go/\n", path.Join(installPath, "go", "bin"))
Expand Down Expand Up @@ -133,10 +118,14 @@ func getGoVersion() (string, error) {
}

content := strings.TrimSpace(string(body))
version, _, ok := strings.Cut(content, "\n")
txtVersion, _, ok := strings.Cut(content, "\n")
if !ok {
return "", fmt.Errorf("format unexpected: %q", content)
}

version, ok := strings.CutPrefix(txtVersion, "go")
if !ok {
return "", fmt.Errorf("format unexpected: %q", txtVersion)
}
return version, nil
}
53 changes: 13 additions & 40 deletions cmd/system/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/Masterminds/semver"
"github.com/alexellis/arkade/pkg/archive"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,17 +47,21 @@ func MakeInstallPowershell() *cobra.Command {
arch, _ = cmd.Flags().GetString("arch")
}

dlArch := arch
if arch == "x86_64" {
dlArch = "x64"
} else if arch == "aarch64" {
dlArch = "arm64"
} else if arch == "armv7" || arch == "armv7l" {
dlArch = "arm32"
tools := get.MakeTools()
var tool *get.Tool
for _, t := range tools {
if t.Name == "pwsh" {
tool = &t
break
}
}

if tool == nil {
return fmt.Errorf("unable to find powershell definition")
}

if version == "" {
v, err := get.FindGitHubRelease("PowerShell", "PowerShell")
v, err := get.FindGitHubRelease(tool.Owner, tool.Repo)
if err != nil {
return err
}
Expand All @@ -67,50 +70,20 @@ func MakeInstallPowershell() *cobra.Command {
version = "v" + version
}

fmt.Printf("Installing version: %s for: %s\n", version, dlArch)

semVer := semver.MustParse(version)
majorVersion := semVer.Major()
// semVer := strings.TrimPrefix(version, "v")

// majorVerDemlimiter := strings.Index(semVer, ".")
// majorVersion := semVer[:majorVerDemlimiter]

installPath = fmt.Sprintf("%s/%d", installPath, majorVersion)

fmt.Printf("Installing Powershell to %s\n", installPath)

installPath = strings.ReplaceAll(installPath, "$HOME", os.Getenv("HOME"))

if err := os.MkdirAll(installPath, 0755); err != nil && !os.IsExist(err) {
fmt.Printf("Error creating directory %s, error: %s\n", installPath, err.Error())
}

filename := fmt.Sprintf("powershell-%s-linux-%s.tar.gz", semVer, dlArch)
dlURL := fmt.Sprintf(githubDownloadTemplate, "PowerShell", "PowerShell", version, filename)

fmt.Printf("Downloading from: %s\n", dlURL)

progress, _ := cmd.Flags().GetBool("progress")
outPath, err := get.DownloadFileP(dlURL, progress)
err := get.DownloadNested(tool, arch, osVer, version, installPath, progress, !progress)
if err != nil {
return err
}
defer os.Remove(outPath)

fmt.Printf("Downloaded to: %s\n", outPath)

f, err := os.OpenFile(outPath, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer f.Close()

fmt.Printf("Unpacking Powershell to: %s\n", installPath)

if err := archive.Untar(f, installPath, true, true); err != nil {
return err
}

lnPath := "/usr/bin/pwsh"
fmt.Printf("Creating Symbolic link to: %s\n", lnPath)
Expand Down
54 changes: 54 additions & 0 deletions pkg/get/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/alexellis/arkade/pkg/config"
"github.com/alexellis/arkade/pkg/env"
"github.com/cheggaaa/pb/v3"
cp "github.com/otiai10/copy"
)

const (
Expand All @@ -29,6 +30,59 @@ func (e *ErrNotFound) Error() string {
return "server returned status: 404"
}

func DownloadNested(tool *Tool, arch, operatingSystem, version string, movePath string, displayProgress, quiet bool) error {
downloadURL, err := GetDownloadURL(tool,
strings.ToLower(operatingSystem),
strings.ToLower(arch),
version, quiet)
if err != nil {
return err
}

if !quiet {
fmt.Printf("Downloading: %s\n", downloadURL)
}

outPath, err := DownloadFileP(downloadURL, displayProgress)
if err != nil {
return err
}
defer os.Remove(outPath)

fmt.Printf("Downloaded to: %s\n", outPath)

f, err := os.OpenFile(outPath, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer f.Close()

tempUnpackPath, err := os.MkdirTemp(os.TempDir(), "arkade-*")
if err != nil {
return err
}
defer os.RemoveAll(tempUnpackPath)
tempUnpackPath = fmt.Sprintf("%s/%s", tempUnpackPath, tool.Name)

fmt.Printf("Unpacking %s to: %s\n", tool.Name, tempUnpackPath)
if err = archive.UntarNested(f, tempUnpackPath, true, true); err != nil {
return err
}

if err := os.MkdirAll(movePath, 0755); err != nil && !os.IsExist(err) {
fmt.Printf("Error creating directory %s, error: %s\n", movePath, err.Error())
}

fmt.Printf("Copying binaries to: %s\n", movePath)
opts := cp.Options{
AddPermission: 0755,
}
if err := cp.Copy(tempUnpackPath, movePath, opts); err != nil {
return err
}
return nil
}

func Download(tool *Tool, arch, operatingSystem, version string, movePath string, displayProgress, quiet bool) (string, string, error) {

downloadURL, err := GetDownloadURL(tool,
Expand Down
5 changes: 4 additions & 1 deletion pkg/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ type Tool struct {
// NoExtension is required for tooling such as kubectx
// which at time of writing is a bash script.
NoExtension bool

// true if tool should be used as system install only
SystemOnly bool
}

type ToolLocal struct {
Expand All @@ -73,6 +76,7 @@ type ToolLocal struct {

var templateFuncs = map[string]interface{}{
"HasPrefix": func(s, prefix string) bool { return strings.HasPrefix(s, prefix) },
"ToLower": func(s string) string { return strings.ToLower(s) },
}

func (tool Tool) IsArchive(quiet bool) (bool, error) {
Expand Down Expand Up @@ -210,7 +214,6 @@ func getURLByGithubTemplate(tool Tool, os, arch, version string) (string, error)

func FindGitHubRelease(owner, repo string) (string, error) {
url := fmt.Sprintf("https://github.com/%s/%s/releases/latest", owner, repo)

client := makeHTTPClient(&githubTimeout, false)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
Expand Down
Loading

0 comments on commit 5c9e788

Please sign in to comment.