|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + cmdQuery = &cobra.Command{ |
| 14 | + Use: "query", |
| 15 | + Short: "start the query that pulls data for the prompt", |
| 16 | + } |
| 17 | + flgQCmdStatus = cmdQuery.PersistentFlags().Int( |
| 18 | + "cmd-status", 0, |
| 19 | + "cmd status of previous command", |
| 20 | + ) |
| 21 | + flgQPreexecTS = cmdQuery.PersistentFlags().Int( |
| 22 | + "preexec-ts", 0, |
| 23 | + "pre-execution timestamp to gauge how log execution took", |
| 24 | + ) |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + cmdQuery.RunE = cmdQueryRun |
| 29 | +} |
| 30 | + |
| 31 | +func cmdQueryRun(_ *cobra.Command, _ []string) error { |
| 32 | + if *flgQCmdStatus != 0 { |
| 33 | + printPart("st", fmt.Sprintf("%#v", *flgQCmdStatus)) |
| 34 | + } |
| 35 | + |
| 36 | + wg := new(WaitGroupDispatcher) |
| 37 | + defer wg.Wait() |
| 38 | + |
| 39 | + wg.Dispatch(func() { |
| 40 | + homeDir := os.Getenv("HOME") |
| 41 | + |
| 42 | + if wd, err := os.Getwd(); err == nil { |
| 43 | + wdh := strings.Replace(wd, homeDir, "~", 1) |
| 44 | + |
| 45 | + printPart("wd_full", wdh) |
| 46 | + printPart("wd", trimPath(wdh)) |
| 47 | + } |
| 48 | + |
| 49 | + nowTS := time.Now() |
| 50 | + printPart("ts", nowTS.Format("15:04:05 01/02/06")) |
| 51 | + |
| 52 | + if *flgQPreexecTS != 0 { |
| 53 | + cmdTS := time.Unix(int64(*flgQPreexecTS), 0) |
| 54 | + |
| 55 | + diff := nowTS.Sub(cmdTS).Round(time.Second) |
| 56 | + if diff > 1 { |
| 57 | + printPart("ds", diff) |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + //wg.Dispatch(func() { |
| 63 | + // out, err := stringExec("git", "config", "--list") |
| 64 | + // printPart("debug_o", js(out)) |
| 65 | + // if err != nil { |
| 66 | + // printPart("debug_e", js(err.Error())) |
| 67 | + // } |
| 68 | + //}) |
| 69 | + |
| 70 | + wg.Dispatch(func() { |
| 71 | + cwg := new(WaitGroupDispatcher) |
| 72 | + defer cwg.Wait() |
| 73 | + |
| 74 | + if _, err := stringExec("git", "rev-parse", "--show-toplevel"); err == nil { |
| 75 | + printPart("vcs", "git") |
| 76 | + } else { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + cwg.Dispatch(func() { |
| 81 | + if branch, err := stringExec("git", "branch", "--show-current"); err == nil { |
| 82 | + branch = trim(branch) |
| 83 | + if len(branch) > 0 { |
| 84 | + printPart("vcs_br", trim(branch)) |
| 85 | + return |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if branch, err := stringExec("git", "name-rev", "--name-only", "HEAD"); err == nil { |
| 90 | + branch = trim(branch) |
| 91 | + if len(branch) > 0 { |
| 92 | + printPart("vcs_br", trim(branch)) |
| 93 | + return |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + cwg.Dispatch(func() { |
| 99 | + if status, err := stringExec("git", "status", "--porcelain"); err == nil { |
| 100 | + if len(status) > 0 { |
| 101 | + printPart("vcs_dirty", 1) |
| 102 | + //printPart("vcs_dirty_st", js(status)) |
| 103 | + } else { |
| 104 | + printPart("vsc_dirty", 0) |
| 105 | + } |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + cwg.Dispatch(func() { |
| 110 | + if status, err := stringExec("git", "rev-list", "--left-right", "--count", "HEAD...@{u}"); err == nil { |
| 111 | + parts := strings.SplitN(status, "\t", 2) |
| 112 | + if len(parts) < 2 { |
| 113 | + parts = []string{"0", "0"} |
| 114 | + } |
| 115 | + |
| 116 | + printPart("vcs_log_ahead", parts[0]) |
| 117 | + printPart("vcs_log_behind", parts[1]) |
| 118 | + } |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + wg.Dispatch(func() { |
| 123 | + var err error |
| 124 | + |
| 125 | + cwg := new(WaitGroupDispatcher) |
| 126 | + defer cwg.Wait() |
| 127 | + |
| 128 | + var stgSeriesLen string |
| 129 | + if stgSeriesLen, err = stringExec("stg", "series", "-c"); err == nil { |
| 130 | + printPart("stg", "1") |
| 131 | + printPart("stg_qlen", stgSeriesLen) |
| 132 | + } |
| 133 | + |
| 134 | + cwg.Dispatch(func() { |
| 135 | + if stgSeriesPos, err := stringExec("stg", "series", "-cA"); err == nil { |
| 136 | + printPart("stg_qpos", stgSeriesPos) |
| 137 | + } |
| 138 | + }) |
| 139 | + |
| 140 | + var stgPatchTop string |
| 141 | + if stgPatchTop, err = stringExec("stg", "top"); err == nil { |
| 142 | + printPart("stg_top", stgPatchTop) |
| 143 | + } else { |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + cwg.Dispatch(func() { |
| 148 | + gitSHA, _ := stringExec("stg", "id") |
| 149 | + stgSHA, _ := stringExec("stg", "id", stgPatchTop) |
| 150 | + |
| 151 | + if gitSHA != stgSHA { |
| 152 | + printPart("stg_dirty", 1) |
| 153 | + } else { |
| 154 | + printPart("stg_dirty", 0) |
| 155 | + } |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
0 commit comments