func NewCmdView()

in commands/project/view/project_view.go [33:130]


func NewCmdView(f *cmdutils.Factory) *cobra.Command {
	opts := ViewOptions{
		IO: f.IO,
	}

	projectViewCmd := &cobra.Command{
		Use:   "view [repository] [flags]",
		Short: "View a project or repository.",
		Long: heredoc.Doc(`Display the description and README of a project, or open it in the browser.
		`),
		Args: cobra.MaximumNArgs(1),
		Example: heredoc.Doc(`
			# View project information for the current directory.
			# Must be a Git repository.
			$ glab repo view

			# View project information of specified name.
			# glab repo view my-project
			$ glab repo view user/repo
			$ glab repo view group/namespace/repo

			# Specify repository by full [Git] URL.
			$ glab repo view git@gitlab.com:user/repo.git
			$ glab repo view https://gitlab.company.org/user/repo
			$ glab repo view https://gitlab.company.org/user/repo.git
		`),
		RunE: func(cmd *cobra.Command, args []string) error {
			var err error

			cfg, err := f.Config()
			if err != nil {
				return err
			}

			opts.Branch = strings.TrimSpace(opts.Branch)

			if len(args) == 1 {
				opts.ProjectID = args[0]
			}

			// No project argument - use current repository
			if opts.ProjectID == "" {
				opts.Repo, err = f.BaseRepo()
				if err != nil {
					return cmdutils.WrapError(err, "`repository` is required when not running in a Git repository.")
				}

				// Configure client to have host of current repository
				client, err := api.NewClientWithCfg(opts.Repo.RepoHost(), cfg, false)
				if err != nil {
					return err
				}
				opts.APIClient = client.Lab()

				if opts.Branch == "" {
					opts.Branch, _ = f.Branch()
				}
			} else {
				// If the ProjectID is a single token, use current user's namespace
				if !strings.Contains(opts.ProjectID, "/") {
					apiClient, err := f.HttpClient()
					if err != nil {
						return err
					}
					currentUser, err := api.CurrentUser(apiClient)
					if err != nil {
						return cmdutils.WrapError(err, "Failed to retrieve your current user.")
					}

					opts.ProjectID = currentUser.Username + "/" + opts.ProjectID
				}

				// Get the repo full name from the ProjectID which can be a full URL or a group/repo format
				opts.Repo, err = glrepo.FromFullName(opts.ProjectID)
				if err != nil {
					return err
				}
				client, err := api.NewClientWithCfg(opts.Repo.RepoHost(), cfg, false)
				if err != nil {
					return err
				}
				opts.APIClient = client.Lab()
			}

			browser, _ := cfg.Get(opts.Repo.RepoHost(), "browser")
			opts.Browser = browser

			opts.GlamourStyle, _ = cfg.Get(opts.Repo.RepoHost(), "glamour_style")
			return runViewProject(&opts)
		},
	}

	projectViewCmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open a project in the browser.")
	projectViewCmd.Flags().StringVarP(&opts.OutputFormat, "output", "F", "text", "Format output as: text, json.")
	projectViewCmd.Flags().StringVarP(&opts.Branch, "branch", "b", "", "View a specific branch of the repository.")

	return projectViewCmd
}