forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreserve.go
More file actions
68 lines (54 loc) · 1.47 KB
/
preserve.go
File metadata and controls
68 lines (54 loc) · 1.47 KB
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
package shared
import (
"encoding/json"
"fmt"
"os"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
)
func PreserveInput(io *iostreams.IOStreams, state *IssueMetadataState, createErr *error) func() {
return func() {
if !state.IsDirty() {
return
}
if *createErr == nil {
return
}
if cmdutil.IsUserCancellation(*createErr) {
// these errors are user-initiated cancellations
return
}
out := io.ErrOut
// this extra newline guards against appending to the end of a survey line
fmt.Fprintln(out)
data, err := json.Marshal(state)
if err != nil {
fmt.Fprintf(out, "failed to save input to file: %s\n", err)
fmt.Fprintln(out, "would have saved:")
fmt.Fprintf(out, "%v\n", state)
return
}
tmpfile, err := io.TempFile(os.TempDir(), "gh*.json")
if err != nil {
fmt.Fprintf(out, "failed to save input to file: %s\n", err)
fmt.Fprintln(out, "would have saved:")
fmt.Fprintf(out, "%v\n", state)
return
}
_, err = tmpfile.Write(data)
if err != nil {
fmt.Fprintf(out, "failed to save input to file: %s\n", err)
fmt.Fprintln(out, "would have saved:")
fmt.Fprintln(out, string(data))
return
}
cs := io.ColorScheme()
issueType := "pr"
if state.Type == IssueMetadata {
issueType = "issue"
}
fmt.Fprintf(out, "%s operation failed. To restore: gh %s create --recover %s\n", cs.FailureIcon(), issueType, tmpfile.Name())
// some whitespace before the actual error
fmt.Fprintln(out)
}
}