diff --git a/README.md b/README.md index bd11dac..f0c98a9 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,26 @@ CLI app for managing Copr repos, written in Go. -```shell -Usage: copr-tool [OPTION] [REPO...] +## Usage +`copr-tool [OPTION] [REPO(s)...]` +``` Options: enable Add or enable one or more Copr repositories. remove Remove one or more Copr repositories. - list List all (enabled and disabled) Copr repositories in your repo folder. + list List Copr repositories in your repo folder. + --enabled List all enabled repositories (default). + --disabled List all disabled repositories. + --all List both disabled and enabled repositories. disable Disable one or more Copr repositories without deleting the repository files. help Display help text. Arguments: - [REPO...] One or more repository names formatted as `author/repo` + [REPO(s)...] One or more repository names formatted as `author/repo` Examples: - copr-tool enable kylegospo/bazzite + copr-tool enable kylegospo/bazzite yalter/niri copr-tool disable kylegospo/bazzite copr-tool remove kylegospo/bazzite - copr-tool list + copr-tool list --all ``` \ No newline at end of file diff --git a/cmd/list.go b/cmd/list.go index 1dd0fbd..b8798e1 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -12,8 +12,33 @@ import ( "github.com/trgeiger/copr-tool/internal/app" ) +var ( + enabled bool + disabled bool + showAll bool + verbose bool +) + +func printReposList(fs afero.Fs, out io.Writer, repoList []*app.CoprRepo) { + if len(repoList) == 0 { + return + } else { + showDupesMessage := false + for _, r := range repoList { + r.FindLocalFiles(fs) + if len(r.LocalFiles) > 1 { + showDupesMessage = true + } + fmt.Fprintln(out, r.Name()) + } + if showDupesMessage { + fmt.Fprintln(out, "\nDuplicate entries found. Consider running the prune command.") + } + } +} + func NewListCmd(fs afero.Fs, out io.Writer) *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "list", Short: "List installed Copr repositories", Long: `A longer description that spans multiple lines and likely contains examples @@ -23,25 +48,42 @@ func NewListCmd(fs afero.Fs, out io.Writer) *cobra.Command { This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { - repos, err := app.GetAllRepos(fs, out) - if err != nil { - fmt.Fprintf(out, "Error when retrieving locally installed repositories: %s", err) + var lists []app.RepoState + if !disabled { + lists = append(lists, app.Enabled) } - if len(repos) == 0 { - fmt.Fprintln(out, "No installed Copr repositories.") - } else { - showDupesMessage := false - for _, r := range repos { - r.FindLocalFiles(fs) - if len(r.LocalFiles) > 1 { - showDupesMessage = true - } - fmt.Fprintln(out, r.Name()) + if disabled || showAll { + lists = append(lists, app.Disabled) + } + + for i, list := range lists { + var listState string + if list == app.Disabled { + listState = "disabled" + } else { + listState = "enabled" } - if showDupesMessage { - fmt.Fprintln(out, "\nDuplicate entries found. Consider running the prune command.") + printList, err := app.GetReposList(fs, out, list) + if err != nil { + fmt.Fprintf(out, "Error when retrieving local repositories: %s", err) + } + if len(printList) == 0 { + fmt.Fprintf(out, "- No %s repositories.\n", listState) + } else { + fmt.Fprintf(out, "- List of %s repositories:\n", listState) + printReposList(fs, out, printList) + if i == 0 && len(lists) > 1 { + fmt.Fprintf(out, "\n") + } } } + }, } + + cmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "show enabled Copr repositories (default)") + cmd.Flags().BoolVarP(&disabled, "disabled", "d", false, "show disabled Copr repositories") + cmd.Flags().BoolVarP(&showAll, "all", "A", false, "show enabled and disabled Copr repositories") + + return cmd } diff --git a/cmd/prune.go b/cmd/prune.go index c1d93a5..9f77f42 100644 --- a/cmd/prune.go +++ b/cmd/prune.go @@ -33,7 +33,7 @@ func NewPruneCmd(fs afero.Fs, out io.Writer) *cobra.Command { Use: "prune", Short: "Remove duplicate repository configurations.", Run: func(cmd *cobra.Command, args []string) { - repos, err := app.GetAllRepos(fs, out) + repos, err := app.GetReposList(fs, out, app.Enabled) if err != nil { fmt.Fprintf(out, "Error when retrieving locally installed repositories: %s", err) os.Exit(1) diff --git a/internal/app/util.go b/internal/app/util.go index d0dd4c9..be99080 100644 --- a/internal/app/util.go +++ b/internal/app/util.go @@ -118,7 +118,7 @@ func DeleteRepo(r *CoprRepo, fs afero.Fs, out io.Writer) error { return nil } -func GetAllRepos(fs afero.Fs, out io.Writer) ([]*CoprRepo, error) { +func GetReposList(fs afero.Fs, out io.Writer, state RepoState) ([]*CoprRepo, error) { files, err := afero.ReadDir(fs, ReposDir) if err != nil { return nil, err @@ -127,6 +127,9 @@ func GetAllRepos(fs afero.Fs, out io.Writer) ([]*CoprRepo, error) { var repos []*CoprRepo for _, file := range files { if !file.IsDir() { + var repoName string + addToResult := false + isCoprRepo := false ioFile, err := fs.Open(ReposDir + file.Name()) if err != nil { @@ -134,20 +137,26 @@ func GetAllRepos(fs afero.Fs, out io.Writer) ([]*CoprRepo, error) { } scanner := bufio.NewScanner(ioFile) + for scanner.Scan() { + // If we see Copr repo, store name in repoName if strings.Contains(scanner.Text(), "[copr:copr") { t := strings.Split(strings.Trim(scanner.Text(), "[]"), ":") - repoName := t[len(t)-2] + "/" + t[len(t)-1] - if !slices.Contains(reposStrings, repoName) { - r, err := NewCoprRepo(repoName) - if err != nil { - return nil, err - } - repos = append(repos, r) - reposStrings = append(reposStrings, repoName) - } - break + repoName = t[len(t)-2] + "/" + t[len(t)-1] + isCoprRepo = true + } + // If we see our desired state, flip our flag + if strings.Contains(scanner.Text(), string(state)) && isCoprRepo { + addToResult = true + } + } + if addToResult && !slices.Contains(reposStrings, repoName) { + r, err := NewCoprRepo(repoName) + if err != nil { + return nil, err } + repos = append(repos, r) + reposStrings = append(reposStrings, repoName) } if err := scanner.Err(); err != nil { fmt.Fprintln(out, "Issue reading repo files: ", err)