mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:46:45 +08:00
implement cli tools for ease of use
This commit is contained in:
1 parent
2e91a67c8f
commit
4207db52ba
6 files changed
+208
-5
No files matched your search
@@ -0,0 +1,62 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var installCmd = &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "Install's Guh in your system",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
||||
fmt.Println("Installing guh to your PATH...")
|
||||
|
||||
src, _ := os.Executable()
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "linux", "darwin":
|
||||
destDir := filepath.Join(os.Getenv("HOME"), ".local", "bin")
|
||||
os.MkdirAll(destDir, 0755)
|
||||
|
||||
dest := filepath.Join(destDir, "guh")
|
||||
data, _ := os.ReadFile(src)
|
||||
os.WriteFile(dest, data, 0755)
|
||||
|
||||
fmt.Printf("Installed guh to %s\n", dest)
|
||||
fmt.Println("Make sure ~/.local/bin is in your PATH.")
|
||||
|
||||
case "windows":
|
||||
destDir := filepath.Join(os.Getenv("APPDATA"), "dev.pages.codedave.guh")
|
||||
os.MkdirAll(destDir, 0755)
|
||||
|
||||
dest := filepath.Join(destDir, "guh.exe")
|
||||
data, _ := os.ReadFile(src)
|
||||
os.WriteFile(dest, data, 0755)
|
||||
|
||||
currentPath := os.Getenv("PATH")
|
||||
if !containsPath(currentPath, destDir) {
|
||||
newPath := destDir + ";" + currentPath
|
||||
exec.Command("setx", "PATH", newPath).Run()
|
||||
}
|
||||
|
||||
fmt.Printf("Installed guh to %s\n", dest)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func containsPath(path, dir string) bool {
|
||||
for _, p := range filepath.SplitList(path) {
|
||||
if p == dir {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"github.com/daveberrys/guh/src/cmd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
cmd.RootCmd.AddCommand(cliCmd)
|
||||
cliCmd.AddCommand(updateCmd)
|
||||
cliCmd.AddCommand(versionCmd)
|
||||
cliCmd.AddCommand(installCmd)
|
||||
}
|
||||
|
||||
var cliCmd = &cobra.Command{
|
||||
Use: "cli",
|
||||
Short: "Manage the CLI tool",
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "Updates Guh to the latest version",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
||||
resp, err := http.Get("https://api.github.com/repos/daveberrys/guh/commits")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
var commits []struct {
|
||||
Sha string `json:"sha"`
|
||||
}
|
||||
json.Unmarshal(body, &commits)
|
||||
guhGithubHash := commits[0].Sha[:7]
|
||||
|
||||
if guhGithubHash == Version {
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("Updating from %s -> %s\n", Version, guhGithubHash)
|
||||
fmt.Println("This may take a long time depending on your internet...")
|
||||
return doUpdateSequence()
|
||||
},
|
||||
}
|
||||
|
||||
func doUpdateSequence() error {
|
||||
var url string
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
url = "https://nightly.link/daveberrys/guh/workflows/compile.yaml/main/GUH-Windows.exe.zip"
|
||||
case "linux":
|
||||
url = "https://nightly.link/daveberrys/guh/workflows/compile.yaml/main/GUH-Linux.zip"
|
||||
case "darwin":
|
||||
url = "https://nightly.link/daveberrys/guh/workflows/compile.yaml/main/GUH-macOS.zip"
|
||||
default:
|
||||
return fmt.Errorf("unsupported OS: %s", runtime.GOOS)
|
||||
}
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil { return err }
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
zipReader, _ := zip.NewReader(bytes.NewReader(body), int64(len(body)))
|
||||
|
||||
var binName string
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
binName = "GUH-Windows.exe"
|
||||
case "linux":
|
||||
binName = "GUH-Linux"
|
||||
case "darwin":
|
||||
binName = "GUH-macOS"
|
||||
default:
|
||||
return fmt.Errorf("A fatal error occurred! We don't know what happened...")
|
||||
}
|
||||
|
||||
var binData []byte
|
||||
for _, f := range zipReader.File {
|
||||
if f.FileInfo().IsDir() {
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(f.Name, binName) || strings.HasSuffix(f.Name, "/"+binName) {
|
||||
rc, _ := f.Open()
|
||||
binData, _ = io.ReadAll(rc)
|
||||
rc.Close()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if binData == nil {
|
||||
return fmt.Errorf("could not find %s in archive", binName)
|
||||
}
|
||||
|
||||
exePath, _ := os.Executable()
|
||||
exePath, _ = filepath.EvalSymlinks(exePath)
|
||||
|
||||
tmpPath := exePath + ".tmp"
|
||||
os.WriteFile(tmpPath, binData, 0755)
|
||||
os.Rename(tmpPath, exePath)
|
||||
|
||||
fmt.Println("Update successful!")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version string
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Prints the version of Guh",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cobraCmd *cobra.Command, args []string) error {
|
||||
fmt.Printf("Current `guh` hash version; %s\n", Version)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user