Skip to content

Commit fe64019

Browse files
authored
Merge pull request #14 from legendu-net/dev
merge dev for release
2 parents 2866829 + 7910728 commit fe64019

File tree

12 files changed

+1033
-146
lines changed

12 files changed

+1033
-146
lines changed

cmd/bash_it.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"legendu.net/icon/utils"
6+
"log"
7+
"path/filepath"
8+
"runtime"
9+
)
10+
11+
// Install bash-it, a community Bash framework.
12+
// For more details, please refer to https://github.com/Bash-it/bash-it#installation.
13+
func bashIt(cmd *cobra.Command, args []string) {
14+
home := utils.UserHomeDir()
15+
if utils.GetBoolFlag(cmd, "install") {
16+
dir := filepath.Join(home, ".bash_it")
17+
utils.RemoveAll(dir)
18+
command := utils.Format(`git clone --depth=1 https://github.com/Bash-it/bash-it.git {dir} \
19+
&& {dir}/install.sh --silent -f`,
20+
map[string]string{
21+
"dir": dir,
22+
})
23+
utils.RunCmd(command)
24+
}
25+
if utils.GetBoolFlag(cmd, "config") {
26+
profile := ".bash_profile"
27+
if runtime.GOOS == "linux" {
28+
profile = ".bashrc"
29+
}
30+
profile = filepath.Join(home, profile)
31+
utils.AddPathShell([]string{"$HOME/*/bin"}, profile)
32+
log.Printf("%s is configured to insert $HOME/*/bin into $PATH.", profile)
33+
if runtime.GOOS == "linux" {
34+
text := `
35+
# source in ~/.bashrc
36+
if [[ -f $HOME/.bashrc ]]; then
37+
. $HOME/.bashrc
38+
fi
39+
`
40+
utils.AppendToTextFile(filepath.Join(home, ".bash_profile"), text)
41+
}
42+
}
43+
if utils.GetBoolFlag(cmd, "uninstall") {
44+
utils.RunCmd("~/.bash_it/uninstall.sh && rm -rf ~/.bash_it/")
45+
}
46+
}
47+
48+
var bashItCmd = &cobra.Command{
49+
Use: "bash_it",
50+
Aliases: []string{"bashit", "bit"},
51+
Short: "Install and configure bash-it.",
52+
//Args: cobra.ExactArgs(1),
53+
Run: bashIt,
54+
}
55+
56+
func init() {
57+
bashItCmd.Flags().BoolP("install", "i", false, "If specified, install bash-it.")
58+
bashItCmd.Flags().Bool("uninstall", false, "If specified, uninstall bash-it.")
59+
bashItCmd.Flags().BoolP("config", "c", false, "If specified, configure bash-it.")
60+
rootCmd.AddCommand(bashItCmd)
61+
}

cmd/golang.go

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,38 @@
11
package cmd
22

33
import (
4-
"io/ioutil"
4+
"github.com/spf13/cobra"
5+
"golang.org/x/sys/unix"
6+
"legendu.net/icon/utils"
57
"log"
68
"net/http"
7-
"os"
9+
"path/filepath"
810
"regexp"
911
"runtime"
1012
"strings"
11-
12-
"github.com/spf13/cobra"
13-
"legendu.net/icon/utils"
1413
)
1514

1615
func getGolangVersion() string {
1716
resp, err := http.Get("https://github.com/golang/go/tags")
1817
if err != nil {
1918
log.Fatal(err)
2019
}
21-
body, err := ioutil.ReadAll(resp.Body)
22-
resp.Body.Close()
23-
if err != nil {
24-
log.Fatal(err)
25-
}
20+
html := utils.ReadAllText(resp.Body)
2621
if resp.StatusCode > 399 {
2722
log.Fatal("...")
2823
}
29-
html := string(body)
3024
re := regexp.MustCompile(`tag/go(\d+\.\d+\.\d+)`)
3125
return re.FindStringSubmatch(html)[1]
3226
}
3327

3428
// Install and configure Golang.
3529
func golang(cmd *cobra.Command, args []string) {
36-
install, err := cmd.Flags().GetBool("install")
37-
if err != nil {
38-
log.Fatal(err)
39-
}
40-
sudo, err := cmd.Flags().GetBool("sudo")
41-
if err != nil {
42-
log.Fatal(err)
43-
}
44-
prefix := ""
45-
if sudo {
46-
prefix = "sudo"
47-
}
48-
if install {
30+
prefix := utils.GetCommandPrefix(false, map[string]uint32{
31+
"/usr/local/go": unix.W_OK | unix.R_OK,
32+
"/usr/local": unix.W_OK | unix.R_OK,
33+
"/usr/local/bin": unix.W_OK | unix.R_OK,
34+
}, "ls")
35+
if utils.GetBoolFlag(cmd, "install") {
4936
switch runtime.GOOS {
5037
case "windows":
5138
case "darwin":
@@ -66,27 +53,22 @@ func golang(cmd *cobra.Command, args []string) {
6653
log.Fatal("The OS ", runtime.GOOS, " is not supported!")
6754
}
6855
}
69-
config, err := cmd.Flags().GetBool("config")
70-
if err != nil {
71-
log.Fatal(err)
72-
}
73-
if config {
56+
if utils.GetBoolFlag(cmd, "config") {
7457
switch runtime.GOOS {
7558
case "windows":
7659
case "darwin":
7760
case "linux":
78-
usr_local_bin := "/usr/local/bin/"
79-
files, err := os.ReadDir("/usr/local/go/bin/")
80-
if err != nil {
81-
log.Fatal(err)
82-
}
83-
for _, file := range files {
61+
usr_local_bin := "/usr/local/bin"
62+
go_bin := "/usr/local/go/bin"
63+
entries := utils.ReadDir(go_bin)
64+
for _, entry := range entries {
65+
file := filepath.Join(go_bin, entry.Name())
8466
log.Printf(
8567
"Creating a symbolic link of %s into %s/ ...", file, usr_local_bin,
8668
)
8769
cmd := utils.Format("{prefix} ln -svf {file} {usr_local_bin}/", map[string]string{
8870
"prefix": prefix,
89-
"file": file.Name(),
71+
"file": file,
9072
"usr_local_bin": usr_local_bin,
9173
})
9274
utils.RunCmd(cmd)

cmd/ipython.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"legendu.net/icon/utils"
6+
//"log"
7+
"path/filepath"
8+
//"runtime"
9+
)
10+
11+
// Install IPython.
12+
func ipython(cmd *cobra.Command, args []string) {
13+
if utils.GetBoolFlag(cmd, "install") {
14+
command := utils.Format("{prefix} {pip_install} ipython", map[string]string{
15+
"prefix": utils.GetCommandPrefix(
16+
utils.GetBoolFlag(cmd, "sudo"),
17+
map[string]uint32{},
18+
"ls",
19+
),
20+
"pip_install": utils.BuildPipInstall(cmd),
21+
})
22+
utils.RunCmd(command)
23+
}
24+
if utils.GetBoolFlag(cmd, "config") {
25+
profile_dir := utils.GetStringFlag(cmd, "profile-dir")
26+
profile_default := filepath.Join(profile_dir, "profile_default")
27+
utils.CopyEmbedFile("data/ipython/startup.ipy", filepath.Join(profile_default, "startup/startup.ipy"))
28+
utils.CopyEmbedFile("data/ipython/ipython_config.py", filepath.Join(profile_default, "ipython_config.py"))
29+
}
30+
if utils.GetBoolFlag(cmd, "config") {
31+
}
32+
}
33+
34+
var ipythonCmd = &cobra.Command{
35+
Use: "ipython",
36+
Aliases: []string{"ipy"},
37+
Short: "Install and configure IPython.",
38+
//Args: cobra.ExactArgs(1),
39+
Run: ipython,
40+
}
41+
42+
func init() {
43+
ipythonCmd.Flags().BoolP("install", "i", false, "If specified, install IPython.")
44+
ipythonCmd.Flags().Bool("uninstall", false, "If specified, uninstall IPython.")
45+
ipythonCmd.Flags().BoolP("config", "c", false, "If specified, configure IPython.")
46+
ipythonCmd.Flags().Bool("sudo", false, "If specified, force using sudo.")
47+
ipythonCmd.Flags().String("profile-dir", filepath.Join(utils.UserHomeDir(), ".ipython"), "The directory for storing IPython configuration files.")
48+
ipythonCmd.Flags().String("python", "python3", "Path to the python3 command.")
49+
ipythonCmd.Flags().Bool("user", false, "Install Python packages to user's local directory.")
50+
ipythonCmd.Flags().StringSlice("extra-pip-options", []string{}, "Extra options (separated by comma) to pass to pip.")
51+
rootCmd.AddCommand(ipythonCmd)
52+
}

0 commit comments

Comments
 (0)