func NewCmdView()

in commands/ci/view/view.go [101:198]


func NewCmdView(f *cmdutils.Factory) *cobra.Command {
	opts := ViewOpts{}
	pipelineCIView := &cobra.Command{
		Use:   "view [branch/tag]",
		Short: "View, run, trace, log, and cancel CI/CD job's current pipeline.",
		Long: heredoc.Doc(`Supports viewing, running, tracing, and canceling jobs.

		Use arrow keys to navigate jobs and logs.

		- 'Enter' to toggle through a job's logs / traces, or display a child pipeline. Trigger jobs are marked with a '»'.
		- 'Esc' or 'q' to close the logs or trace, or return to the parent pipeline.
		- 'Ctrl+R', 'Ctrl+P' to run, retry, or play a job. Use 'Tab' or arrow keys to navigate the modal, and 'Enter' to confirm.
		- 'Ctrl+D' to cancel a job. If the selected job isn't running or pending, quits the CI/CD view.
		- 'Ctrl+Q' to quit the CI/CD view.
		- 'Ctrl+Space' to suspend application and view the logs. Similar to 'glab pipeline ci trace'.
		Supports vi style bindings and arrow keys for navigating jobs and logs.
	`),
		Example: heredoc.Doc(`
			# Uses current branch
			$ glab pipeline ci view

			# Get latest pipeline on master branch
			$ glab pipeline ci view master

			# just like the second example
			$ glab pipeline ci view -b master

			# Get latest pipeline on master branch of profclems/glab repo
			$ glab pipeline ci view -b master -R profclems/glab
		`),
		Args: cobra.MaximumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			opts.Output = f.IO.StdOut

			var err error
			opts.ApiClient, err = f.HttpClient()
			if err != nil {
				return err
			}

			repo, err := f.BaseRepo()
			if err != nil {
				return err
			}

			opts.ProjectID = repo.FullName()

			if opts.RefName == "" {
				if len(args) == 1 {
					opts.RefName = args[0]
				} else {
					opts.RefName, err = git.CurrentBranch()
					if err != nil {
						return err
					}
				}
			}

			opts.Commit, err = api.GetCommit(opts.ApiClient, opts.ProjectID, opts.RefName)
			if err != nil {
				return err
			}

			opts.CommitSHA = opts.Commit.ID
			if opts.Commit.LastPipeline == nil {
				return fmt.Errorf("Can't find pipeline for commit: %s", opts.CommitSHA)
			}

			cfg, _ := f.Config()

			if opts.OpenInBrowser { // open in browser if --web flag is specified
				webURL := opts.Commit.LastPipeline.WebURL

				if f.IO.IsOutputTTY() {
					fmt.Fprintf(f.IO.StdErr, "Opening %s in your browser.\n", utils.DisplayURL(webURL))
				}

				browser, _ := cfg.Get(repo.RepoHost(), "browser")
				return utils.OpenInBrowser(webURL, browser)
			}

			p, err := api.GetSinglePipeline(opts.ApiClient, opts.Commit.LastPipeline.ID, opts.ProjectID)
			if err != nil {
				return fmt.Errorf("Can't get pipeline #%d info: %s", opts.Commit.LastPipeline.ID, err)
			}
			opts.PipelineUser = p.User
			pipelines = make([]gitlab.PipelineInfo, 0, 10)

			return drawView(opts)
		},
	}

	pipelineCIView.Flags().
		StringVarP(&opts.RefName, "branch", "b", "", "Check pipeline status for a branch or tag. Defaults to the current branch.")
	pipelineCIView.Flags().BoolVarP(&opts.OpenInBrowser, "web", "w", false, "Open pipeline in a browser. Uses default browser, or browser specified in BROWSER variable.")

	return pipelineCIView
}