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 <nitishkumarsingh71@gmail.com>

move powershell and go to tools

Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>

empty commit

Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>

implement custom version resolver

Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>

switch node to tools

Signed-off-by: Nitishkumar Singh <nitishkumarsingh71@gmail.com>
  • Loading branch information
nitishkumar71 committed Sep 15, 2024
1 parent bfba912 commit bdfb12a
Show file tree
Hide file tree
Showing 10 changed files with 744 additions and 319 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
81 changes: 12 additions & 69 deletions cmd/system/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ package system

import (
"fmt"
"io"
"net/http"
"os"
"path"
"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,51 +49,31 @@ 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"
}

if len(version) == 0 {
v, err := getGoVersion()
if err != nil {
return err
tools := get.MakeTools()
var tool *get.Tool
for _, t := range tools {
if t.Name == "go" {
tool = &t
break
}

version = v
} else if !strings.HasPrefix(version, "go") {
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)
if tool == nil {
return fmt.Errorf("unable to find go definition")
}

progress, _ := cmd.Flags().GetBool("progress")
outPath, err := get.DownloadFileP(dlURL, progress)
tempPath, 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)
err = get.MoveTo(tempPath, installPath)
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 All @@ -107,36 +83,3 @@ func MakeInstallGo() *cobra.Command {

return command
}

func getGoVersion() (string, error) {
req, err := http.NewRequest(http.MethodGet, "https://go.dev/VERSION?m=text", nil)
if err != nil {
return "", err
}

req.Header.Set("User-Agent", pkg.UserAgent())

res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}

if res.Body == nil {
return "", fmt.Errorf("unexpected empty body")
}

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code: %d", res.StatusCode)
}

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

return version, nil
}
117 changes: 57 additions & 60 deletions cmd/system/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,51 @@ package system

import (
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"

"github.com/alexellis/arkade/pkg"
"github.com/alexellis/arkade/pkg/archive"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
cp "github.com/otiai10/copy"
"github.com/spf13/cobra"
)

func getLatestNodeVersion(version, channel string) (*string, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://nodejs.org/download/%s/%s", channel, version), nil)
if err != nil {
return nil, err
}
// func getLatestNodeVersion(version, channel string) (*string, error) {
// req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://nodejs.org/download/%s/%s", channel, version), nil)
// if err != nil {
// return nil, err
// }

req.Header.Set("User-Agent", pkg.UserAgent())
// req.Header.Set("User-Agent", pkg.UserAgent())

res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
// res, err := http.DefaultClient.Do(req)
// if err != nil {
// return nil, err
// }

if res.Body != nil {
defer res.Body.Close()
}
// if res.Body != nil {
// defer res.Body.Close()
// }

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
// body, err := io.ReadAll(res.Body)
// if err != nil {
// return nil, err
// }

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not find latest version for %s, (%d), body: %s", version, res.StatusCode, string(body))
}
// if res.StatusCode != http.StatusOK {
// return nil, fmt.Errorf("could not find latest version for %s, (%d), body: %s", version, res.StatusCode, string(body))
// }

regex := regexp.MustCompile(`(?m)node-v(\d+.\d+.\d+)-linux-.*`)
result := regex.FindStringSubmatch(string(body))
// regex := regexp.MustCompile(`(?m)node-v(\d+.\d+.\d+)-linux-.*`)
// result := regex.FindStringSubmatch(string(body))

if len(result) < 2 {
if v, ok := os.LookupEnv("ARK_DEBUG"); ok && v == "1" {
fmt.Printf("Body: %s\n", string(body))
}
return nil, fmt.Errorf("could not find latest version for %s, (%d), %s", version, res.StatusCode, result)
}
return &result[1], nil
}
// if len(result) < 2 {
// if v, ok := os.LookupEnv("ARK_DEBUG"); ok && v == "1" {
// fmt.Printf("Body: %s\n", string(body))
// }
// return nil, fmt.Errorf("could not find latest version for %s, (%d), %s", version, res.StatusCode, result)
// }
// return &result[1], nil
// }

func MakeInstallNode() *cobra.Command {
command := &cobra.Command{
Expand Down Expand Up @@ -94,12 +88,18 @@ func MakeInstallNode() *cobra.Command {
dlArch = "arm64"
}

resolver := &get.NodeVersionResolver{
Channel: channel,
Version: version,
}

if (version == "latest" || strings.Contains(version, "latest-")) && channel == "release" {
v, err := getLatestNodeVersion(version, channel)
v, err := resolver.GetVersion()
if err != nil {
return err
}
version = *v
version = v
resolver.Version = v
} else if (version == "latest" || strings.Contains(version, "latest-")) && channel == "nightly" {
return fmt.Errorf("please set a specific version for downloading a nightly builds")
}
Expand All @@ -108,39 +108,36 @@ func MakeInstallNode() *cobra.Command {
version = "v" + version
}

fmt.Printf("Installing version: %s for: %s\n", version, dlArch)
filename := fmt.Sprintf("%s/%s.tar.gz", version, fmt.Sprintf("node-%s-linux-%s", version, dlArch))
dlURL := fmt.Sprintf("https://nodejs.org/download/%s/%s", channel, filename)
tools := get.MakeTools()
var tool *get.Tool
for _, t := range tools {
if t.Name == "node" {
tool = &t
break
}
}

fmt.Printf("Downloading from: %s\n", dlURL)
outPath, err := get.DownloadFileP(dlURL, progress)
if err != nil {
return err
if tool == nil {
return fmt.Errorf("unable to find node definition")
}
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
tool.VersionResolver = &get.NodeVersionResolver{
Channel: channel,
Version: version,
}
defer f.Close()

tempUnpackPath, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("%s*", "node"))
tempPath, err := get.DownloadNested(tool, arch, osVer, version, installPath, progress, !progress)
defer os.RemoveAll(tempPath)
if err != nil {
return err
}
defer os.RemoveAll(tempUnpackPath)
fmt.Printf("Unpacking binaries to: %s\n", tempUnpackPath)
if err = archive.UntarNested(f, tempUnpackPath, true, false); err != nil {
return err
}
fmt.Printf("Temp Path: %s \n", tempPath)

fmt.Printf("Copying binaries to: %s\n", installPath)
nodeDir := fmt.Sprintf("%s/%s", tempUnpackPath, fmt.Sprintf("node-%s-linux-%s", version, dlArch))
if err := cp.Copy(nodeDir, installPath); err != nil {
err = get.MoveFromInternalDir(fmt.Sprintf("node-%s-linux-%s", version, dlArch), tempPath, installPath)
if err != nil {
return err
}

return nil
}
return command
Expand Down
50 changes: 14 additions & 36 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,25 @@ 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)
tempPath, 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)
err = get.MoveTo(tempPath, installPath)
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
Loading

0 comments on commit bdfb12a

Please sign in to comment.