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]>

empty commit

Signed-off-by: Nitishkumar Singh <[email protected]>
  • Loading branch information
nitishkumar71 committed Jul 17, 2024
1 parent 1219496 commit 82a9a6f
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-url-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:

jobs:
build:
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
with:
Expand Down
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
84 changes: 11 additions & 73 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,26 @@ 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)

progress, _ := cmd.Flags().GetBool("progress")
outPath, err := get.DownloadFileP(dlURL, progress)
if err != nil {
return err
if tool == nil {
return fmt.Errorf("unable to find go definition")
}
defer os.Remove(outPath)

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

f, err := os.OpenFile(outPath, os.O_RDONLY, 0644)
progress, _ := cmd.Flags().GetBool("progress")
err := get.DownloadNested(tool, arch, osVer, version, installPath, progress, !progress)
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 +78,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
}
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
51 changes: 50 additions & 1 deletion pkg/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

const GitHubVersionStrategy = "github"
const k8sVersionStrategy = "k8s"
const goVersionStrategy = "go"

var supportedOS = [...]string{"linux", "darwin", "ming"}
var supportedArchitectures = [...]string{"x86_64", "arm", "amd64", "armv6l", "armv7l", "arm64", "aarch64"}
Expand Down Expand Up @@ -64,6 +65,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 +77,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 @@ -167,6 +172,14 @@ func (tool Tool) GetURL(os, arch, version string, quiet bool) (string, error) {
version = v
}

if tool.VersionStrategy == goVersionStrategy {
v, err := FindGoRelease()
if err != nil {
return "", err
}
version = v
}

if !quiet {
log.Printf("Found: %s", version)
}
Expand Down Expand Up @@ -210,7 +223,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 Expand Up @@ -280,6 +292,43 @@ func FindK8sRelease() (string, error) {
return version, nil
}

func FindGoRelease() (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))
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
}

func getBinaryURL(owner, repo, version, downloadName string) string {
if in := strings.Index(downloadName, "/"); in > -1 {
return fmt.Sprintf(
Expand Down
Loading

0 comments on commit 82a9a6f

Please sign in to comment.