69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
|
|
package shared
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"os/exec"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func GhAuthenticated() bool {
|
||
|
|
cmd := exec.Command("gh", "auth", "status")
|
||
|
|
output, _ := cmd.CombinedOutput()
|
||
|
|
return strings.Contains(string(output), "Logged in")
|
||
|
|
}
|
||
|
|
|
||
|
|
func Truncate(s string, max int) string {
|
||
|
|
if len(s) <= max {
|
||
|
|
return s
|
||
|
|
}
|
||
|
|
return s[:max-3] + "..."
|
||
|
|
}
|
||
|
|
|
||
|
|
func Confirm(prompt string) bool {
|
||
|
|
fmt.Printf("%s [y/N] ", prompt)
|
||
|
|
var response string
|
||
|
|
fmt.Scanln(&response)
|
||
|
|
response = strings.ToLower(strings.TrimSpace(response))
|
||
|
|
return response == "y" || response == "yes"
|
||
|
|
}
|
||
|
|
|
||
|
|
func FormatAge(t time.Time) string {
|
||
|
|
d := time.Since(t)
|
||
|
|
if d < time.Hour {
|
||
|
|
return fmt.Sprintf("%dm ago", int(d.Minutes()))
|
||
|
|
}
|
||
|
|
if d < 24*time.Hour {
|
||
|
|
return fmt.Sprintf("%dh ago", int(d.Hours()))
|
||
|
|
}
|
||
|
|
if d < 7*24*time.Hour {
|
||
|
|
return fmt.Sprintf("%dd ago", int(d.Hours()/24))
|
||
|
|
}
|
||
|
|
if d < 30*24*time.Hour {
|
||
|
|
return fmt.Sprintf("%dw ago", int(d.Hours()/(24*7)))
|
||
|
|
}
|
||
|
|
return fmt.Sprintf("%dmo ago", int(d.Hours()/(24*30)))
|
||
|
|
}
|
||
|
|
|
||
|
|
func GitClone(ctx context.Context, org, repo, path string) error {
|
||
|
|
if GhAuthenticated() {
|
||
|
|
httpsURL := fmt.Sprintf("https://github.com/%s/%s.git", org, repo)
|
||
|
|
cmd := exec.CommandContext(ctx, "gh", "repo", "clone", httpsURL, path)
|
||
|
|
output, err := cmd.CombinedOutput()
|
||
|
|
if err == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
errStr := strings.TrimSpace(string(output))
|
||
|
|
if strings.Contains(errStr, "already exists") {
|
||
|
|
return fmt.Errorf("%s", errStr)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
cmd := exec.CommandContext(ctx, "git", "clone", fmt.Sprintf("git@github.com:%s/%s.git", org, repo), path)
|
||
|
|
output, err := cmd.CombinedOutput()
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("%s", strings.TrimSpace(string(output)))
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|