summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <g.giovanini@gridx.de>2023-04-27 11:29:53 +0200
committerGabriel A. Giovanini <g.giovanini@gridx.de>2023-11-07 11:33:04 +0100
commit4246ea81d56c93c1df2954e34cc33fb6e6524f49 (patch)
treef6ce5b4a65648a10897029777f79916f1803ddfd /main.go
downloadworkctl-4246ea81d56c93c1df2954e34cc33fb6e6524f49.tar.gz
workctl-4246ea81d56c93c1df2954e34cc33fb6e6524f49.tar.bz2
workctl-4246ea81d56c93c1df2954e34cc33fb6e6524f49.zip
feat: Initial commitHEADmaster
Diffstat (limited to 'main.go')
-rw-r--r--main.go169
1 files changed, 169 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..cd143db
--- /dev/null
+++ b/main.go
@@ -0,0 +1,169 @@
+package main
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "os/exec"
+ "regexp"
+)
+
+const (
+ prLinkRegex = `https://github.com/.+/.+/(pull|issues)/[0-9]*`
+ version = "0.0.1"
+)
+
+var (
+ prRegex = regexp.MustCompile(prLinkRegex)
+)
+
+func extractPRLink(text string) string {
+ return prRegex.FindString(text)
+}
+
+func getLinkFromSTDIn() (string, error) {
+ in, err := io.ReadAll(os.Stdin)
+ if err != nil {
+ return "", err
+ }
+
+ if err != nil {
+ return "", err
+ }
+
+ prLink := extractPRLink(string(in))
+ if prLink == "" {
+ return "", fmt.Errorf("Unable to find link")
+ }
+
+ return prLink, nil
+}
+
+func pipeTo(app string, args ...string) error {
+ prLink, err := getLinkFromSTDIn()
+ if err != nil {
+ return err
+ }
+
+ args = append(args, prLink)
+
+ _, err = execCommand(app, args...)
+ return err
+}
+
+func execCommand(app string, args ...string) (string, error) {
+ cmd := exec.Command(app, args...)
+ stdout, err := cmd.Output()
+
+ if err != nil {
+ return "", err
+ }
+
+ return string(stdout), nil
+}
+
+func shellIn(app string, args ...string) error {
+ cmd := exec.Command(app, args...)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+
+ return cmd.Run()
+}
+
+func merge() error {
+ return pipeTo("gh", "pr", "merge", "--merge")
+}
+
+func approve() error {
+ return pipeTo("gh", "pr", "review", "--approve")
+}
+
+func dependabot() error {
+ return pipeTo("gh", "pr", "comment", "--body", "@dependabot rebase\n@dependabot merge")
+}
+
+func open() error {
+ return pipeTo("qutebrowser", "-l", "critical")
+}
+
+func edit() error {
+ file, err := os.CreateTemp("", "workctl")
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+ defer os.Remove(file.Name())
+
+ prLink, err := getLinkFromSTDIn()
+ if err != nil {
+ return err
+ }
+
+ out, err := execCommand("gh", "pr", "diff", "--patch", prLink)
+ if err != nil {
+ return err
+ }
+
+ _, err = file.WriteString(out)
+ if err != nil {
+ return err
+ }
+
+ return shellIn("nvim", "-c", "setlocal filetype=gitcommit", file.Name())
+}
+
+func printVersion() {
+ fmt.Print("workctl ", version)
+}
+
+func help() {
+ fmt.Printf(`workctl %s
+Extract a general link from a given "text/plain" email from github and append
+it to another commands.
+
+USAGE:
+ workctl [COMMAND]
+
+COMMAND:
+ vimfy edit a file with vim and trim breaking lines so it fits textboxes with easy
+ open open a given link on qutebrowser
+ merge issue a merge request for a given link
+ approve approve PR from a given link
+ dependabot comment dependabot to rebase and merge for given link
+ edit open the patch file from a given link`, version)
+}
+
+func printIfError(err error) {
+ if err != nil {
+ fmt.Println("Error running the command:")
+ fmt.Println(err.Error())
+ os.Exit(1)
+ }
+}
+
+func main() {
+ if len(os.Args) > 2 {
+ fmt.Println("Invalid number of param")
+ help()
+ os.Exit(1)
+ }
+
+ action := os.Args[1]
+
+ switch action {
+ case "open":
+ printIfError(open())
+ case "merge":
+ printIfError(merge())
+ case "edit":
+ printIfError(edit())
+ case "approve":
+ printIfError(approve())
+ case "dependabot":
+ printIfError(dependabot())
+ case "version", "--version":
+ printVersion()
+ default:
+ help()
+ }
+}