Skip to content

Commit

Permalink
Add flags to list command
Browse files Browse the repository at this point in the history
- Edit readme
  • Loading branch information
trgeiger committed Apr 21, 2024
1 parent ae113d5 commit 9bc283b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 33 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

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
Expand Down
74 changes: 58 additions & 16 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion cmd/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 20 additions & 11 deletions internal/app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -127,27 +127,36 @@ 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 {
return nil, err
}

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)
Expand Down

0 comments on commit 9bc283b

Please sign in to comment.