Skip to content

Commit 26ea6a6

Browse files
authored
Merge pull request #20 from legendu-net/dev
merge dev into main
2 parents 1e374fa + 0028b89 commit 26ea6a6

27 files changed

+3963
-124
lines changed

cmd/ai/pytorch.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ai
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"legendu.net/icon/utils"
6+
"strings"
7+
)
8+
9+
// Install and configure PyTorch.
10+
func pytorch(cmd *cobra.Command, args []string) {
11+
if utils.GetBoolFlag(cmd, "install") {
12+
cudaVersion := utils.GetStringFlag(cmd, "cuda-version")
13+
version := "cpu"
14+
if cudaVersion != "" {
15+
version = "cu" + strings.ReplaceAll(cudaVersion, ".", "")
16+
}
17+
command := utils.Format("{pip_install} torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/{version}", map[string]string{
18+
"pip_install": utils.BuildPipInstall(cmd),
19+
"version": version,
20+
})
21+
utils.RunCmd(command)
22+
}
23+
if utils.GetBoolFlag(cmd, "config") {
24+
}
25+
if utils.GetBoolFlag(cmd, "uninstall") {
26+
}
27+
}
28+
29+
var PyTorchCmd = &cobra.Command{
30+
Use: "pytorch",
31+
Aliases: []string{"torch"},
32+
Short: "Install and configure PyTorch.",
33+
//Args: cobra.ExactArgs(1),
34+
Run: pytorch,
35+
}
36+
37+
func init() {
38+
PyTorchCmd.Flags().BoolP("install", "i", false, "Install IPython.")
39+
PyTorchCmd.Flags().Bool("uninstall", false, "Uninstall IPython.")
40+
PyTorchCmd.Flags().BoolP("config", "c", false, "Configure IPython.")
41+
PyTorchCmd.Flags().String("cuda-version", "", "The version of CUDA. If not specified, the CPU version is used.")
42+
utils.AddPythonFlags(PyTorchCmd)
43+
// rootCmd.AddCommand(ipythonCmd)
44+
}

cmd/spark.go renamed to cmd/bigdata/spark.go

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package bigdata
22

33
import (
44
//"embed"
@@ -18,19 +18,7 @@ import (
1818
// Get the latest version of Spark.
1919
func getVersion() string {
2020
log.Printf("Parsing the latest version of Spark ...")
21-
resp, err := http.Get("https://spark.apache.org/downloads.html")
22-
if err != nil {
23-
log.Fatal(err)
24-
}
25-
body, err := ioutil.ReadAll(resp.Body)
26-
resp.Body.Close()
27-
if err != nil {
28-
log.Fatal(err)
29-
}
30-
if resp.StatusCode > 399 {
31-
log.Fatal("HTTP request got an error response with the status code ", resp.StatusCode)
32-
}
33-
html := string(body)
21+
html := utils.HttpGetAsString("https://spark.apache.org/downloads.html")
3422
re := regexp.MustCompile(`Latest Release \(Spark (\d.\d.\d)\)`)
3523
for _, line := range strings.Split(html, "\n") {
3624
match := re.FindString(line)
@@ -48,8 +36,8 @@ func getSparkDownloadUrl(sparkVersion string, hadoopVersion string) string {
4836
if err != nil {
4937
log.Fatal(err)
5038
}
39+
defer resp.Body.Close()
5140
body, err := ioutil.ReadAll(resp.Body)
52-
resp.Body.Close()
5341
if err != nil {
5442
log.Fatal(err)
5543
}
@@ -90,15 +78,11 @@ func spark(cmd *cobra.Command, args []string) {
9078
sparkHdp := fmt.Sprintf("spark-%s-bin-hadoop%s", sparkVersion, hadoopVersion)
9179
sparkHome := filepath.Join(dir, sparkHdp)
9280
// install Spark
93-
install, err := cmd.Flags().GetBool("install")
94-
if err != nil {
95-
log.Fatal(err)
96-
}
9781
prefix := utils.GetCommandPrefix(false, map[string]uint32{
9882
dir: unix.W_OK | unix.R_OK,
99-
}, "ls")
100-
if install {
101-
sparkTgz := utils.DownloadFile("https://archive.apache.org/dist/spark/spark-3.3.0/spark-3.3.0-bin-hadoop3.tgz", "spark_*.tgz").Name()
83+
})
84+
if utils.GetBoolFlag(cmd, "install") {
85+
sparkTgz := utils.DownloadFile("https://archive.apache.org/dist/spark/spark-3.3.0/spark-3.3.0-bin-hadoop3.tgz", "spark_*.tgz", true)
10286
log.Printf("Installing Spark into the directory %s ...\n", sparkHome)
10387
switch runtime.GOOS {
10488
case "windows":
@@ -111,11 +95,7 @@ func spark(cmd *cobra.Command, args []string) {
11195
utils.RunCmd(cmd)
11296
}
11397
}
114-
config, err := cmd.Flags().GetBool("config")
115-
if err != nil {
116-
log.Fatal(err)
117-
}
118-
if config {
98+
if utils.GetBoolFlag(cmd, "config") {
11999
metastoreDb := filepath.Join(sparkHome, "metastoreDb")
120100
warehouse := filepath.Join(sparkHome, "warehouse")
121101
switch runtime.GOOS {
@@ -158,14 +138,15 @@ func spark(cmd *cobra.Command, args []string) {
158138
runCmd(f"{args.prefix} chmod -R 777 {metastoreDb}")
159139
*/
160140
}
161-
/*
162-
if args.uninstall:
163-
cmd = f"{args.prefix} rm -rf {sparkHome}"
164-
runCmd(cmd)
165-
*/
141+
if utils.GetBoolFlag(cmd, "uninstall") {
142+
/*
143+
cmd = f"{args.prefix} rm -rf {sparkHome}"
144+
runCmd(cmd)
145+
*/
146+
}
166147
}
167148

168-
var sparkCmd = &cobra.Command{
149+
var SparkCmd = &cobra.Command{
169150
Use: "spark",
170151
Aliases: []string{},
171152
Short: "Install and configure Spark.",
@@ -188,10 +169,10 @@ func init() {
188169
"which containing SQL code for creating tables."
189170
)
190171
*/
191-
sparkCmd.Flags().String("spark-version", "3.3.0", "The version of Spark version to install.")
192-
sparkCmd.Flags().String("hadoop-version", "3.2", "The version of Spark version to install.")
193-
sparkCmd.Flags().StringP("directory", "d", "/opt", "The directory to install Spark.")
194-
sparkCmd.Flags().BoolP("install", "i", false, "If specified, install Spark.")
195-
sparkCmd.Flags().BoolP("config", "c", false, "If specified, configure Spark.")
196-
rootCmd.AddCommand(sparkCmd)
172+
SparkCmd.Flags().String("spark-version", "3.3.0", "The version of Spark version to install.")
173+
SparkCmd.Flags().String("hadoop-version", "3.2", "The version of Spark version to install.")
174+
SparkCmd.Flags().StringP("directory", "d", "/opt", "The directory to install Spark.")
175+
SparkCmd.Flags().BoolP("install", "i", false, "If specified, install Spark.")
176+
SparkCmd.Flags().BoolP("config", "c", false, "If specified, configure Spark.")
177+
// rootCmd.AddCommand(sparkCmd)
197178
}

cmd/dev/git.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package dev
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"legendu.net/icon/cmd/network"
6+
"legendu.net/icon/utils"
7+
"log"
8+
"path/filepath"
9+
"runtime"
10+
"strings"
11+
)
12+
13+
// Install and configure Git.
14+
func git(cmd *cobra.Command, args []string) {
15+
if utils.GetBoolFlag(cmd, "install") {
16+
switch runtime.GOOS {
17+
case "linux":
18+
if utils.IsDebianSeries() {
19+
command := utils.Format("{prefix} apt-get update && {prefix} apt-get install {yes_s} git git-lfs", map[string]string{
20+
"prefix": utils.GetCommandPrefix(
21+
true,
22+
map[string]uint32{},
23+
),
24+
"yes_s": utils.BuildYesFlag(cmd),
25+
})
26+
utils.RunCmd(command)
27+
// TODO: leverage from_github to download git-delta and install it to /usr/local/bin!!!
28+
} else if utils.IsFedoraSeries() {
29+
command := utils.Format("{prefix} yum install {yes_s} git", map[string]string{
30+
"prefix": utils.GetCommandPrefix(
31+
true,
32+
map[string]uint32{},
33+
),
34+
"yes_s": utils.BuildYesFlag(cmd),
35+
})
36+
utils.RunCmd(command)
37+
}
38+
case "darwin":
39+
utils.BrewInstallSafe([]string{"git", "git-lfs", "bash-completion@2"})
40+
default:
41+
}
42+
utils.RunCmd("git lfs install")
43+
}
44+
if utils.GetBoolFlag(cmd, "config") {
45+
network.SshClient(cmd, args)
46+
home := utils.UserHomeDir()
47+
gitConfig := filepath.Join(home, ".gitconfig")
48+
utils.CopyEmbedFile("data/git/gitconfig", gitConfig, 0o600)
49+
log.Printf("%s is copied to %s", "data/git/gitconfig", gitConfig)
50+
switch runtime.GOOS {
51+
case "darwin":
52+
file := "/usr/local/etc/bash_completion.d/git-completion.bash"
53+
script := utils.Format("\n# Git completion\n[ -f {file} ] && . {file}", map[string]string{
54+
"file": file,
55+
})
56+
utils.AppendToTextFile(filepath.Join(home, ".bash_profile"), script)
57+
log.Printf("Bash completion is enabled for Git.")
58+
default:
59+
}
60+
configureGitIgnore(cmd)
61+
proxy := utils.GetStringFlag(cmd, "proxy")
62+
if proxy != "" {
63+
command := utils.Format("git config --global http.proxy {proxy} && git config --global https.proxy {proxy}", map[string]string{
64+
"proxy": proxy,
65+
})
66+
utils.RunCmd(command)
67+
}
68+
}
69+
if utils.GetBoolFlag(cmd, "uninstall") {
70+
utils.RunCmd("git lfs uninstall")
71+
switch runtime.GOOS {
72+
case "darwin":
73+
utils.RunCmd("brew uninstall git git-lfs")
74+
case "linux":
75+
if utils.IsDebianSeries() {
76+
command := utils.Format("{prefix} apt-get purge {yes_s} git git-lfs", map[string]string{
77+
"prefix": utils.GetCommandPrefix(
78+
true,
79+
map[string]uint32{},
80+
),
81+
})
82+
utils.RunCmd(command)
83+
} else if utils.IsFedoraSeries() {
84+
command := utils.Format("{prefix} yum remove git", map[string]string{
85+
"prefix": utils.GetCommandPrefix(
86+
true,
87+
map[string]uint32{},
88+
),
89+
})
90+
utils.RunCmd(command)
91+
}
92+
default:
93+
}
94+
}
95+
}
96+
97+
// Insert patterns to ingore into .gitignore in the current directory.
98+
func configureGitIgnore(cmd *cobra.Command) {
99+
lang := strings.ToLower(utils.GetStringFlag(cmd, "lang"))
100+
if lang == "" {
101+
return
102+
}
103+
srcFile := "data/git/gitignore_" + lang
104+
dstDir := utils.GetStringFlag(cmd, "dst-dir")
105+
dstFile := filepath.Join(dstDir, ".gitignore")
106+
if utils.GetBoolFlag(cmd, "append") {
107+
utils.AppendToTextFile(dstFile, utils.ReadEmbedFileAsString(srcFile))
108+
log.Printf("%s is appended into %s.", srcFile, dstFile)
109+
} else {
110+
utils.CopyEmbedFile(srcFile, dstFile, 0o600)
111+
log.Printf("%s is copied to %s.", srcFile, dstFile)
112+
}
113+
}
114+
115+
var GitCmd = &cobra.Command{
116+
Use: "git",
117+
Aliases: []string{},
118+
Short: "Install and configure Git.",
119+
//Args: cobra.ExactArgs(1),
120+
Run: git,
121+
}
122+
123+
func init() {
124+
GitCmd.Flags().BoolP("install", "i", false, "Install Git.")
125+
GitCmd.Flags().Bool("uninstall", false, "Uninstall Git.")
126+
GitCmd.Flags().BoolP("config", "c", false, "Configure Git.")
127+
GitCmd.Flags().String("proxy", "", "Configure Git to use the specified proxy.")
128+
GitCmd.Flags().StringP("dest-dir", "d", ".", "The destination directory (current directory, by default) to copy gitignore files to.")
129+
GitCmd.Flags().BoolP("append", "a", false, "Append to the .gitignore instead of oveerwriting it.")
130+
GitCmd.Flags().StringP("lang", "l", "", "The language to configure .gitignore for.")
131+
// rootCmd.AddCommand(gitCmd)
132+
}

cmd/golang.go renamed to cmd/dev/golang.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package dev
22

33
import (
44
"github.com/spf13/cobra"
@@ -17,6 +17,7 @@ func getGolangVersion() string {
1717
if err != nil {
1818
log.Fatal(err)
1919
}
20+
defer resp.Body.Close()
2021
html := utils.ReadAllAsText(resp.Body)
2122
if resp.StatusCode > 399 {
2223
log.Fatal("...")
@@ -31,7 +32,7 @@ func golang(cmd *cobra.Command, args []string) {
3132
"/usr/local/go": unix.W_OK | unix.R_OK,
3233
"/usr/local": unix.W_OK | unix.R_OK,
3334
"/usr/local/bin": unix.W_OK | unix.R_OK,
34-
}, "ls")
35+
})
3536
if utils.GetBoolFlag(cmd, "install") {
3637
switch runtime.GOOS {
3738
case "windows":
@@ -40,7 +41,7 @@ func golang(cmd *cobra.Command, args []string) {
4041
case "linux":
4142
ver := getGolangVersion()
4243
url := strings.ReplaceAll("https://go.dev/dl/go{ver}.linux-amd64.tar.gz", "{ver}", ver)
43-
goTgz := utils.DownloadFile(url, "go_*.tar.gz").Name()
44+
goTgz := utils.DownloadFile(url, "go_*.tar.gz", true)
4445
cmd := utils.Format(`{prefix} rm -rf /usr/local/go \
4546
&& {prefix} tar -C /usr/local/ -xzf {goTgz}`,
4647
map[string]string{
@@ -77,9 +78,11 @@ func golang(cmd *cobra.Command, args []string) {
7778
log.Fatal("The OS ", runtime.GOOS, " is not supported!")
7879
}
7980
}
81+
if utils.GetBoolFlag(cmd, "uninstall") {
82+
}
8083
}
8184

82-
var golangCmd = &cobra.Command{
85+
var GolangCmd = &cobra.Command{
8386
Use: "golang",
8487
Aliases: []string{"go"},
8588
Short: "Install and configure Golang.",
@@ -88,7 +91,7 @@ var golangCmd = &cobra.Command{
8891
}
8992

9093
func init() {
91-
golangCmd.Flags().BoolP("install", "i", false, "If specified, install Golang.")
92-
golangCmd.Flags().BoolP("config", "c", false, "If specified, configure Golang.")
93-
rootCmd.AddCommand(golangCmd)
94+
GolangCmd.Flags().BoolP("install", "i", false, "Install Golang.")
95+
GolangCmd.Flags().BoolP("config", "c", false, "Configure Golang.")
96+
// rootCmd.AddCommand(golangCmd)
9497
}

0 commit comments

Comments
 (0)