mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:07:01 +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
+4
-5
@@ -1,16 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "This will install the compiled local 'guh' binary to '~/.local/bin' directory."
|
||||
echo "Installing..."
|
||||
echo "This script will install the 'guh' binary in the '~/.local/bin' directory."
|
||||
echo
|
||||
|
||||
go mod tidy && echo "Tidied go.mod and go.sum files."
|
||||
go get ./ && echo "Installed all packages." || echo "Failed to install all packages."
|
||||
go build && echo "Built 'guh' binary." || echo "Failed to build 'guh' binary."
|
||||
go build -ldflags "-X 'github.com/daveberrys/guh/src/cmd/cli.Version=$(git rev-parse --short HEAD)'" -o guh . && echo "Built 'guh' binary." || echo "Failed to build 'guh' binary."
|
||||
echo
|
||||
|
||||
cp guh ~/.local/bin/guh && echo "Successfully placed 'guh' in '~/.local/bin' directory." || echo "Failed to place 'guh' in '~/.local/bin' directory."
|
||||
chmod +x ~/.local/bin/guh && echo "Successfully made 'guh' executable." || echo "Failed to make 'guh' executable."
|
||||
cp guh /home/$USER/.local/bin/guh && echo "Successfully placed 'guh' in '~/.local/bin' directory." || echo "Failed to place 'guh' in '~/.local/bin' directory."
|
||||
chmod +x /home/$USER/.local/bin/guh && echo "Successfully made 'guh' executable." || echo "Failed to make 'guh' executable."
|
||||
|
||||
guh >> /dev/null && echo "Successfully installed 'guh' binary." || echo "Failed to install 'guh' binary."
|
||||
echo "Installation complete."
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
_ "github.com/daveberrys/guh/src/cmd/push"
|
||||
_ "github.com/daveberrys/guh/src/cmd/stash"
|
||||
_ "github.com/daveberrys/guh/src/cmd/undo"
|
||||
_ "github.com/daveberrys/guh/src/cmd/cli"
|
||||
)
|
||||
|
||||
func main() { cmd.Execute() }
|
||||
@@ -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