NewCmdReadFile creates the `gh repo read-file` command.
(f *cmdutil.Factory, runF func(*ReadFileOptions) error)
| 50 | |
| 51 | // NewCmdReadFile creates the `gh repo read-file` command. |
| 52 | func NewCmdReadFile(f *cmdutil.Factory, runF func(*ReadFileOptions) error) *cobra.Command { |
| 53 | opts := &ReadFileOptions{ |
| 54 | IO: f.IOStreams, |
| 55 | HttpClient: f.HttpClient, |
| 56 | BaseRepo: f.BaseRepo, |
| 57 | } |
| 58 | |
| 59 | cmd := &cobra.Command{ |
| 60 | Use: "read-file <path> [flags]", |
| 61 | Short: "Read a file from a repository (preview)", |
| 62 | Long: heredoc.Docf(` |
| 63 | Read the contents of a file in a GitHub repository without cloning it. |
| 64 | |
| 65 | This command is in preview and subject to change without notice. |
| 66 | |
| 67 | By default, the file is read from the default branch. Use the %[1]s--ref%[1]s flag to |
| 68 | read from a specific branch, tag, or commit. |
| 69 | |
| 70 | When run in TTY mode, the content is shown through your pager. When stdout is piped or |
| 71 | redirected, the raw content is written directly. To save the file to disk instead, use |
| 72 | the %[1]s--output%[1]s flag. |
| 73 | |
| 74 | By default, the command refuses to output a file that contains terminal escape sequences, |
| 75 | since they could manipulate your terminal. Pass %[1]s--allow-escape-sequences%[1]s to read the file anyway. |
| 76 | This check applies only to terminal and piped output; writing to disk with %[1]s--output%[1]s always |
| 77 | includes the raw bytes, as if %[1]s--allow-escape-sequences%[1]s were given. |
| 78 | `, "`"), |
| 79 | Example: heredoc.Doc(` |
| 80 | # Read a file from the default branch |
| 81 | $ gh repo read-file README.md --repo cli/cli |
| 82 | |
| 83 | # Read a file at a specific ref |
| 84 | $ gh repo read-file README.md --repo cli/cli --ref v2.50.0 |
| 85 | |
| 86 | # Save a file to disk |
| 87 | $ gh repo read-file README.md --repo cli/cli --output download/README.md |
| 88 | |
| 89 | # Print selected fields as JSON |
| 90 | $ gh repo read-file README.md --repo cli/cli --json name,path,size,type |
| 91 | |
| 92 | # Read a file that contains terminal escape sequences |
| 93 | $ gh repo read-file path/to/file --repo OWNER/REPO --allow-escape-sequences |
| 94 | `), |
| 95 | Args: cobra.ExactArgs(1), |
| 96 | RunE: func(cmd *cobra.Command, args []string) error { |
| 97 | opts.BaseRepo = f.BaseRepo |
| 98 | |
| 99 | opts.Path = args[0] |
| 100 | |
| 101 | if err := cmdutil.MutuallyExclusive( |
| 102 | "specify only one of `--json` or `--output`", |
| 103 | opts.Exporter != nil, |
| 104 | opts.Output != "", |
| 105 | ); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | |
| 109 | if runF != nil { |