Files
wehub-resource-sync 26f897c1ec
release / release-please (push) Failing after 1m49s
docs / build (push) Failing after 6m34s
release / build-and-upload (arm64, linux) (push) Has been cancelled
release / build-and-upload (arm64, windows) (push) Has been cancelled
release / build-darwin (amd64, darwin) (push) Has been cancelled
release / checksums (push) Has been cancelled
release / finalize (push) Has been cancelled
release / build-darwin (arm64, darwin) (push) Has been cancelled
release / build-and-upload (amd64, linux) (push) Has been cancelled
release / build-and-upload (amd64, windows) (push) Has been cancelled
docs / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:34:16 +08:00

97 lines
2.9 KiB
Go

package cli
import (
"fmt"
"time"
"github.com/kunchenguid/no-mistakes/internal/daemon"
"github.com/kunchenguid/no-mistakes/internal/db"
"github.com/kunchenguid/no-mistakes/internal/safeurl"
"github.com/spf13/cobra"
)
func newStatusCmd() *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Show status of the current repository",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return trackReadSurface("status", nil, func() (string, string, error) {
p, d, err := openResources()
if err != nil {
return "", "", err
}
defer d.Close()
w := cmd.OutOrStdout()
// Look up repo from current directory.
repo, err := findRepo(d)
if err != nil {
fmt.Fprintln(w, err)
return "uninitialized", "error", nil
}
fmt.Fprintf(w, " %s %s\n", sDim.Render(" repo:"), repo.WorkingPath)
remoteURL := repo.UpstreamURL
if repo.ForkURL != "" {
remoteURL = safeurl.Redact(remoteURL)
}
fmt.Fprintf(w, " %s %s\n", sDim.Render("remote:"), remoteURL)
if repo.ForkURL != "" {
fmt.Fprintf(w, " %s %s\n", sDim.Render(" fork:"), safeurl.Redact(repo.ForkURL))
}
fmt.Fprintf(w, " %s %s\n", sDim.Render(" gate:"), p.RepoDir(repo.ID))
// Check daemon status.
alive, _ := daemon.IsRunning(p)
daemonState := "stopped"
if alive {
daemonState = "running"
}
if alive {
fmt.Fprintf(w, " %s %s %s\n", sDim.Render("daemon:"), sGreen.Render("●"), "running")
} else {
fmt.Fprintf(w, " %s %s %s\n", sDim.Render("daemon:"), sDim.Render("○"), "stopped")
}
// Check for active run.
activeRun, err := d.GetActiveRun(repo.ID, "")
if err != nil {
return "", "", fmt.Errorf("check active run: %w", err)
}
fingerprint := statusFingerprint(repo.ID, daemonState, activeRun)
if activeRun != nil {
fmt.Fprintln(w)
fmt.Fprintf(w, " %s\n", sCyan.Render("Active run"))
sha := activeRun.HeadSHA[:minLen(len(activeRun.HeadSHA), 8)]
ts := time.Unix(activeRun.CreatedAt, 0).Format(time.DateTime)
fmt.Fprintf(w, " %s %s\n", sDim.Render(" id:"), activeRun.ID)
fmt.Fprintf(w, " %s %s\n", sDim.Render(" branch:"), activeRun.Branch)
fmt.Fprintf(w, " %s %s\n", sDim.Render(" status:"), runStatusStyle(activeRun.Status))
fmt.Fprintf(w, " %s %s\n", sDim.Render(" head:"), sDim.Render(sha))
fmt.Fprintf(w, " %s %s\n", sDim.Render("started:"), sDim.Render(ts))
} else {
fmt.Fprintf(w, "\n %s\n", sDim.Render("no active run"))
}
return fingerprint, "success", nil
})
},
}
}
func statusFingerprint(repoID, daemonState string, activeRun *db.Run) string {
if activeRun == nil {
return repoID + "|" + daemonState + "|idle"
}
return fmt.Sprintf("%s|%s|%s:%s:%s:%s", repoID, daemonState, activeRun.ID, activeRun.Branch, activeRun.Status, activeRun.HeadSHA)
}
func minLen(a, b int) int {
if a < b {
return a
}
return b
}