| 36 | } |
| 37 | |
| 38 | func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { |
| 39 | opts := &ViewOptions{ |
| 40 | IO: f.IOStreams, |
| 41 | Config: f.Config, |
| 42 | HttpClient: f.HttpClient, |
| 43 | Browser: f.Browser, |
| 44 | Prompter: f.Prompter, |
| 45 | } |
| 46 | |
| 47 | cmd := &cobra.Command{ |
| 48 | Use: "view [<id> | <url>]", |
| 49 | Short: "View a gist", |
| 50 | Long: `View the given gist or select from recent gists.`, |
| 51 | Args: cobra.MaximumNArgs(1), |
| 52 | RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | if len(args) == 1 { |
| 54 | opts.Selector = args[0] |
| 55 | } |
| 56 | |
| 57 | if !opts.IO.IsStdoutTTY() { |
| 58 | opts.Raw = true |
| 59 | } |
| 60 | |
| 61 | if runF != nil { |
| 62 | return runF(opts) |
| 63 | } |
| 64 | return viewRun(opts) |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "Print raw instead of rendered gist contents") |
| 69 | cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open gist in the browser") |
| 70 | cmd.Flags().BoolVar(&opts.ListFiles, "files", false, "List file names from the gist") |
| 71 | cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Display a single file from the gist") |
| 72 | |
| 73 | return cmd |
| 74 | } |
| 75 | |
| 76 | func viewRun(opts *ViewOptions) error { |
| 77 | gistID := opts.Selector |