/
suggest.go
119 lines (100 loc) · 3.2 KB
/
suggest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cmd
import (
"context"
"fmt"
"log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/runme/v3/internal/client/graphql"
"github.com/stateful/runme/v3/internal/tui"
"github.com/stateful/runme/v3/internal/tui/prompt"
)
func suggestCmd() *cobra.Command {
var repoUser string
cmd := &cobra.Command{
Use: "suggest DESCRIPTION",
Hidden: true,
Short: "Suggest a branch name",
Long: `Suggest a branch name for a description.
Remember to wrap the DESCRIPTION in double quotes as otherwise
it will be interpreted as multiple arguments.
Disclaimer: This uses AI, so the suggestions may be biased, wrong,
or just bad. Please use with discretion.
`,
Deprecated: "Please note this command does not receive updates and will be removed in the future.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := graphql.ContextWithTrackInput(cmd.Context(), trackInputFromCmd(cmd, args))
auth := newAuth()
_, err := auth.GetToken(cmd.Context())
if err != nil {
if err := checkAuthenticated(ctx, cmd, auth, !recoverableWithLogin(err)); err != nil {
return err
}
}
client, err := graphql.New(graphqlEndpoint(), newAPIClient(cmd.Context(), auth))
if err != nil {
return err
}
// Run a query to get user's data exclusively in order to force authentication flow.
err = runSuggestAndRetry(ctx, cmd, func(ctx context.Context) error {
_, err := client.GetUser(ctx, false)
return err
})
if err != nil {
return err
}
var description string
switch len(args) {
case 0:
description, err = promptForDescription(cmd)
if err != nil {
return err
}
case 1:
description = args[0]
}
if len(description) == 0 {
return errors.New("description cannot be empty")
}
model := tui.NewListModel(ctx, description, repoUser, client)
m, err := newProgram(cmd, model).Start()
if err != nil {
return err
}
if mm, ok := m.(tui.ListModel); ok && mm.Confirmed() {
_, err = fmt.Fprintf(cmd.OutOrStdout(), "Great choice! Learn how to run your markdown files with https://runme.dev/\n\n")
}
return err
},
}
cmd.Flags().StringVar(&repoUser, "repo-user", "", "Overwrite git user's email address used for user branch filtering.")
return cmd
}
func promptForDescription(cmd *cobra.Command) (string, error) {
model := tui.NewStandaloneInputModel(prompt.InputParams{Label: "Enter a description:"}, tui.MinimalKeyMap, tui.DefaultStyles)
finalModel, err := newProgram(cmd, model).Run()
if err != nil {
return "", err
}
val, ok := finalModel.(tui.StandaloneInputModel).Value()
if !ok {
return "", errors.New("canceled")
}
return val, nil
}
func runSuggestAndRetry(ctx context.Context, _ *cobra.Command, runF runFunc) error {
err := runF(ctx)
switch {
case err == nil:
return nil
case errors.Is(err, graphql.ErrNoData):
return err
case !recoverableWithLogin(err):
log.Print("Unexpected error occurred. Please try again later or report the issue at https://github.com/stateful/runme or our Discord at https://discord.gg/stateful")
return errors.Wrap(err, "failed to run command")
default:
// continue as it likely was an auth problem
}
return runF(ctx)
}