GetEditorInput gets the user input by launching a text editor and waiting for it to exit
(ctx context.DnoteCtx, fpath string)
| 84 | // GetEditorInput gets the user input by launching a text editor and waiting for |
| 85 | // it to exit |
| 86 | func GetEditorInput(ctx context.DnoteCtx, fpath string) (string, error) { |
| 87 | ok, err := utils.FileExists(fpath) |
| 88 | if err != nil { |
| 89 | return "", errors.Wrapf(err, "checking if the file exists at %s", fpath) |
| 90 | } |
| 91 | if !ok { |
| 92 | f, err := os.Create(fpath) |
| 93 | if err != nil { |
| 94 | return "", errors.Wrap(err, "creating a temporary content file") |
| 95 | } |
| 96 | err = f.Close() |
| 97 | if err != nil { |
| 98 | return "", errors.Wrap(err, "closing the temporary content file") |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | cmd, err := newEditorCmd(ctx, fpath) |
| 103 | if err != nil { |
| 104 | return "", errors.Wrap(err, "creating an editor command") |
| 105 | } |
| 106 | |
| 107 | cmd.Stdin = os.Stdin |
| 108 | cmd.Stdout = os.Stdout |
| 109 | cmd.Stderr = os.Stderr |
| 110 | |
| 111 | err = cmd.Start() |
| 112 | if err != nil { |
| 113 | return "", errors.Wrapf(err, "launching an editor") |
| 114 | } |
| 115 | |
| 116 | err = cmd.Wait() |
| 117 | if err != nil { |
| 118 | return "", errors.Wrap(err, "waiting for the editor") |
| 119 | } |
| 120 | |
| 121 | b, err := os.ReadFile(fpath) |
| 122 | if err != nil { |
| 123 | return "", errors.Wrap(err, "reading the temporary content file") |
| 124 | } |
| 125 | |
| 126 | err = os.Remove(fpath) |
| 127 | if err != nil { |
| 128 | return "", errors.Wrap(err, "removing the temporary content file") |
| 129 | } |
| 130 | |
| 131 | raw := string(b) |
| 132 | |
| 133 | return raw, nil |
| 134 | } |
no test coverage detected