|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + boatstack "github.com/operatorstack/boatstack/boatstack" |
| 11 | +) |
| 12 | + |
| 13 | +func readInsightInput(path string) ([]byte, error) { |
| 14 | + path = strings.TrimSpace(path) |
| 15 | + if path == "" || path == "-" { |
| 16 | + return io.ReadAll(os.Stdin) |
| 17 | + } |
| 18 | + return os.ReadFile(path) |
| 19 | +} |
| 20 | + |
| 21 | +func printInsightView(view boatstack.InsightView, jsonOutput bool) int { |
| 22 | + if jsonOutput { |
| 23 | + return emitJSON(view) |
| 24 | + } |
| 25 | + fmt.Printf("Insight %s: %s\n", view.Capture.ID, view.Evaluation.State) |
| 26 | + fmt.Println(view.Evaluation.Reason) |
| 27 | + fmt.Printf("Repository diff: %s\n", view.RepositoryPath) |
| 28 | + return 0 |
| 29 | +} |
| 30 | + |
| 31 | +func insightCommand(arguments []string) int { |
| 32 | + if len(arguments) == 0 { |
| 33 | + fmt.Fprintln(os.Stderr, "usage: boatstack-helper insight <check|save|list|show|associate|bind|evaluate|frontier|disposition>") |
| 34 | + return 2 |
| 35 | + } |
| 36 | + switch arguments[0] { |
| 37 | + case "check": |
| 38 | + flags := flag.NewFlagSet("insight check", flag.ContinueOnError) |
| 39 | + repo := flags.String("repo", ".", "repository whose insight inbox should be checked") |
| 40 | + input := flags.String("input", "-", "capture JSON file, or - for stdin") |
| 41 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 42 | + return 2 |
| 43 | + } |
| 44 | + value, err := readInsightInput(*input) |
| 45 | + if err != nil { |
| 46 | + return fail(err) |
| 47 | + } |
| 48 | + result, err := boatstack.CheckInsightCapture(*repo, value) |
| 49 | + if err != nil { |
| 50 | + return fail(err) |
| 51 | + } |
| 52 | + return emitJSON(result) |
| 53 | + case "save": |
| 54 | + flags := flag.NewFlagSet("insight save", flag.ContinueOnError) |
| 55 | + repo := flags.String("repo", ".", "repository whose tracked insight inbox should receive the capture") |
| 56 | + input := flags.String("input", "-", "capture JSON file, or - for stdin") |
| 57 | + nonce := flags.String("preview-nonce", "", "nonce returned by insight check") |
| 58 | + fingerprint := flags.String("preview-fingerprint", "", "fingerprint returned by insight check") |
| 59 | + jsonOutput := flags.Bool("json", false, "print the structured capture") |
| 60 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 61 | + return 2 |
| 62 | + } |
| 63 | + value, err := readInsightInput(*input) |
| 64 | + if err != nil { |
| 65 | + return fail(err) |
| 66 | + } |
| 67 | + view, err := boatstack.SaveInsightCapture(*repo, value, *nonce, *fingerprint) |
| 68 | + if err != nil { |
| 69 | + return fail(err) |
| 70 | + } |
| 71 | + return printInsightView(view, *jsonOutput) |
| 72 | + case "list": |
| 73 | + flags := flag.NewFlagSet("insight list", flag.ContinueOnError) |
| 74 | + repo := flags.String("repo", ".", "repository whose captures should be listed") |
| 75 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 76 | + return 2 |
| 77 | + } |
| 78 | + views, err := boatstack.ListInsights(*repo) |
| 79 | + if err != nil { |
| 80 | + return fail(err) |
| 81 | + } |
| 82 | + return emitJSON(views) |
| 83 | + case "show": |
| 84 | + flags := flag.NewFlagSet("insight show", flag.ContinueOnError) |
| 85 | + repo := flags.String("repo", ".", "repository whose capture should be shown") |
| 86 | + id := flags.String("id", "", "insight capture id") |
| 87 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 88 | + return 2 |
| 89 | + } |
| 90 | + view, err := boatstack.ShowInsight(*repo, *id) |
| 91 | + if err != nil { |
| 92 | + return fail(err) |
| 93 | + } |
| 94 | + return emitJSON(view) |
| 95 | + case "associate": |
| 96 | + flags := flag.NewFlagSet("insight associate", flag.ContinueOnError) |
| 97 | + repo := flags.String("repo", ".", "repository whose capture should be associated") |
| 98 | + id := flags.String("id", "", "insight capture id") |
| 99 | + primary := flags.String("primary-topic", "", "human-confirmed primary feature topic") |
| 100 | + var related stringList |
| 101 | + flags.Var(&related, "related-topic", "related feature topic (repeatable)") |
| 102 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 103 | + return 2 |
| 104 | + } |
| 105 | + view, err := boatstack.AssociateInsight(*repo, *id, *primary, related) |
| 106 | + if err != nil { |
| 107 | + return fail(err) |
| 108 | + } |
| 109 | + return emitJSON(view) |
| 110 | + case "bind": |
| 111 | + flags := flag.NewFlagSet("insight bind", flag.ContinueOnError) |
| 112 | + repo := flags.String("repo", ".", "repository whose capture should be bound") |
| 113 | + id := flags.String("id", "", "insight capture id") |
| 114 | + feature := flags.String("feature", "", "managed feature id") |
| 115 | + var criteria stringList |
| 116 | + flags.Var(&criteria, "criterion", "mapped acceptance criterion id (repeatable)") |
| 117 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 118 | + return 2 |
| 119 | + } |
| 120 | + view, err := boatstack.BindInsight(*repo, *id, *feature, criteria) |
| 121 | + if err != nil { |
| 122 | + return fail(err) |
| 123 | + } |
| 124 | + return emitJSON(view) |
| 125 | + case "evaluate": |
| 126 | + flags := flag.NewFlagSet("insight evaluate", flag.ContinueOnError) |
| 127 | + repo := flags.String("repo", ".", "repository whose capture should be evaluated") |
| 128 | + id := flags.String("id", "", "insight capture id") |
| 129 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 130 | + return 2 |
| 131 | + } |
| 132 | + result, err := boatstack.EvaluateInsight(*repo, *id) |
| 133 | + if err != nil { |
| 134 | + return fail(err) |
| 135 | + } |
| 136 | + return emitJSON(result) |
| 137 | + case "frontier": |
| 138 | + flags := flag.NewFlagSet("insight frontier", flag.ContinueOnError) |
| 139 | + repo := flags.String("repo", ".", "repository whose pending insight frontier should be shown") |
| 140 | + jsonOutput := flags.Bool("json", false, "print the structured frontier") |
| 141 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 142 | + return 2 |
| 143 | + } |
| 144 | + report, err := boatstack.InsightFrontier(*repo) |
| 145 | + if err != nil { |
| 146 | + return fail(err) |
| 147 | + } |
| 148 | + if *jsonOutput { |
| 149 | + return emitJSON(report) |
| 150 | + } |
| 151 | + fmt.Print(boatstack.FormatInsightFrontier(report)) |
| 152 | + return 0 |
| 153 | + case "disposition": |
| 154 | + flags := flag.NewFlagSet("insight disposition", flag.ContinueOnError) |
| 155 | + repo := flags.String("repo", ".", "repository whose capture should be dispositioned") |
| 156 | + id := flags.String("id", "", "insight capture id") |
| 157 | + outcome := flags.String("outcome", "", "completed, deferred, rejected, or duplicate") |
| 158 | + reason := flags.String("reason", "", "human reason, required for non-ready completion and non-complete outcomes") |
| 159 | + duplicateOf := flags.String("duplicate-of", "", "original capture id for duplicate outcomes") |
| 160 | + if err := flags.Parse(arguments[1:]); err != nil { |
| 161 | + return 2 |
| 162 | + } |
| 163 | + view, err := boatstack.DisposeInsight(*repo, *id, *outcome, *reason, *duplicateOf) |
| 164 | + if err != nil { |
| 165 | + return fail(err) |
| 166 | + } |
| 167 | + return emitJSON(view) |
| 168 | + default: |
| 169 | + fmt.Fprintln(os.Stderr, "unknown insight subcommand:", arguments[0]) |
| 170 | + return 2 |
| 171 | + } |
| 172 | +} |
0 commit comments