MCPcopy Create free account
hub / github.com/cli/cli / NewCmdReadFile

Function NewCmdReadFile

pkg/cmd/repo/read-file/read_file.go:51–126  ·  view source on GitHub ↗

NewCmdReadFile creates the `gh repo read-file` command.

(f *cmdutil.Factory, runF func(*ReadFileOptions) error)

Source from the content-addressed store, hash-verified

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

Callers 1

TestNewCmdReadFileFunction · 0.85

Calls 4

MutuallyExclusiveFunction · 0.92
AddJSONFlagsFunction · 0.92
EnableRepoOverrideFunction · 0.92
readFileRunFunction · 0.85

Tested by 1

TestNewCmdReadFileFunction · 0.68