Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nodejs to system install #675

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/system/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func MakeInstall() *cobra.Command {
command.AddCommand(MakeInstallCNI())
command.AddCommand(MakeInstallContainerd())
command.AddCommand(MakeInstallActionsRunner())
command.AddCommand(MakeInstallNode())

return command
}
124 changes: 124 additions & 0 deletions cmd/system/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package system

import (
"fmt"
"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"
"io"
"net/http"
"os"
"regexp"
"strings"
)

func getLatestNodeVersion(version, channel string) (*string, error) {
res, err := http.Get(fmt.Sprintf("https://nodejs.org/download/%s/%s", channel, version))
if err != nil {
return nil, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
regex := regexp.MustCompile(`(?m)node-(.*)-linux-.*"`)
result := regex.FindStringSubmatch(string(body))
if len(result) < 2 {
return nil, fmt.Errorf("could not find latest version for %s", version)
}
return &result[1], nil
}

func MakeInstallNode() *cobra.Command {
command := &cobra.Command{
Use: "node",
Short: "Install Node.js",
Long: `Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.`,
Example: `arkade system install node
arkade system install node --version v17.9.0`,
dirien marked this conversation as resolved.
Show resolved Hide resolved
SilenceUsage: true,
}

command.Flags().StringP("version", "v", "latest", "The version for Node.js to install, either a specific version, 'latest' or 'latest-CODENAME' (eg: latest-gallium)")
command.Flags().StringP("path", "p", "/usr/local/", "Installation path")
command.Flags().StringP("channel", "c", "release", "The channel to install from, can be 'releases' or 'nightly',")
command.Flags().Bool("progress", true, "Show download progress")
command.Flags().String("arch", "", "CPU architecture for Prometheus, eg: amd64")

command.RunE = func(cmd *cobra.Command, args []string) error {
installPath, _ := cmd.Flags().GetString("path")
version, _ := cmd.Flags().GetString("version")
progress, _ := cmd.Flags().GetBool("progress")
channel, _ := cmd.Flags().GetString("channel")

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

arch, osVer := env.GetClientArch()

if strings.ToLower(osVer) != "linux" {
return fmt.Errorf("this app only supports Linux")
}
if cmd.Flags().Changed("arch") {
arch, _ = cmd.Flags().GetString("arch")
}

dlArch := arch
if arch == "x86_64" {
dlArch = "x64"
} else if arch == "aarch64" {
dlArch = "arm64"
}

if (version == "latest" || strings.Contains(version, "latest-")) && channel == "release" {
v, err := getLatestNodeVersion(version, channel)
if err != nil {
return err
}
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")
}

if !strings.HasPrefix(version, "v") {
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)

fmt.Printf("Downloading from: %s\n", dlURL)
outPath, err := get.DownloadFileP(dlURL, 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()

tempUnpackPath, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("%s*", "node"))
if err != nil {
return err
}
defer os.RemoveAll(tempUnpackPath)
fmt.Printf("Unpacking binaries to: %s\n", tempUnpackPath)
if err = archive.UntarNested(f, tempUnpackPath); err != nil {
return err
}

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 {
return err
}
return nil
}
return command
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/morikuni/aec v1.0.0
github.com/olekukonko/tablewriter v0.0.5
github.com/otiai10/copy v1.7.0
github.com/pkg/errors v0.9.1
github.com/sethvargo/go-password v0.2.0
github.com/spf13/cobra v1.4.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down