Skip to content

Commit

Permalink
Merge pull request #73 from PlakarKorp/gchehade/plakar-tags
Browse files Browse the repository at this point in the history
Gchehade/plakar tags
  • Loading branch information
poolpOrg authored Nov 13, 2024
2 parents 25874b1 + d043d4f commit 244c866
Show file tree
Hide file tree
Showing 25 changed files with 272 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/plakar/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "github.com/PlakarKorp/plakar/cmd/plakar/subcommands/server"
_ "github.com/PlakarKorp/plakar/cmd/plakar/subcommands/stdio"
_ "github.com/PlakarKorp/plakar/cmd/plakar/subcommands/sync"
_ "github.com/PlakarKorp/plakar/cmd/plakar/subcommands/tags"
_ "github.com/PlakarKorp/plakar/cmd/plakar/subcommands/ui"
_ "github.com/PlakarKorp/plakar/cmd/plakar/subcommands/verify"
_ "github.com/PlakarKorp/plakar/cmd/plakar/subcommands/version"
Expand Down
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/archive.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ The **plakar archive** utility exits 0 on success, and >0 if an err

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ The **plakar backup** utility exits 0 on success, and >0 if an erro

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/cat.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ The **plakar cat** utility exits 0 on success, and >0 if an error o

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/checksum.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ The **plakar checksum** utility exits 0 on success, and >0 if an er

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ The **plakar cleanup** utility exits 0 on success, and >0 if an err

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/clone.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ The **plakar clone** utility exits 0 on success, and >0 if an error

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ The **plakar create** utility exits 0 on success, and >0 if an erro
plakar(1),
plakar-repository(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/diff.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ The **plakar diff** utility exits 0 on success, and >0 if an error

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ The **plakar exec** utility exits 0 on success, and >0 if an error

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ The **plakar find** utility exits 0 on success, and >0 if an error

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/info.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ The **plakar info** utility exits 0 on success, and >0 if an error
plakar(1),
plakar-snapshot(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/ls.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ The **plakar ls** utility exits 0 on success, and >0 if an error oc

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ The **plakar mount** utility exits 0 on success, and >0 if an error

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ The **plakar restore** utility exits 0 on success, and >0 if an err

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/rm.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ The **plakar rm** utility exits 0 on success, and >0 if an error oc

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ The **plakar server** utility exits 0 on success, and >0 if an erro

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/stdio.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ The **plakar stdio** utility exits 0 on success, and >0 if an error

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ The **plakar sync** utility exits 0 on success, and >0 if an error

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
87 changes: 87 additions & 0 deletions cmd/plakar/subcommands/man/docs/tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
PLAKAR(TAGS) - TAGS (1)

# NAME

**plakar\_tags** - Display tags or related information in a Plakar repository

# SYNOPSIS

**plakar\_tags**
\[**-display**]
\[*tags|count|snapshots*]

# DESCRIPTION

The
**plakar\_tags**
command is used to display information about tags in a Plakar repository, including a list of tags, a count of their occurrences, or associated snapshots.

**-display**

> Specifies the type of output for the tag display. Valid options are:
> tags
> > Lists all tags (default).
> count
> > Displays each tag with a count of associated snapshots.
> snapshots
> > Shows each tag with a list of associated snapshot IDs.
# ARGUMENTS

*tags|count|snapshots*

> This argument, when used with the
> **-display**
> flag, controls the output type:
> tags
> > Outputs all tags in alphabetical order.
> count
> > Displays tags with a count of associated snapshots.
> snapshots
> > Lists each tag with associated snapshot IDs.
# EXAMPLES

To illustrate usage:

Basic example:

> > plakar tags -display tags
Example showing tag counts:

> > plakar tags -display count
Example with tag snapshots:

> > plakar tags -display snapshots
# DIAGNOSTICS

The **plakar\_tags** utility exits 0 on success, and >0 if an error occurs.

0

> Command completed successfully.
1

> An unsupported display option was specified.
# SEE ALSO

plakar(1)

macOS 15.0 - November 13, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ The **plakar ui** utility exits 0 on success, and >0 if an error oc

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/verify.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ The **plakar verify** utility exits 0 on success, and >0 if an erro

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
2 changes: 1 addition & 1 deletion cmd/plakar/subcommands/man/docs/version.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ The **plakar version** utility exits 0 on success, and >0 if an err

plakar(1)

OpenBSD 7.6 - November 12, 2024
macOS 15.0 - November 12, 2024
75 changes: 75 additions & 0 deletions cmd/plakar/subcommands/tags/tags.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
.Dd November 13, 2024
.Dt PLAKAR TAGS 1
.Os
.Sh NAME
.Nm plakar_tags
.Nd Display tags or related information in a Plakar repository
.Sh SYNOPSIS
.Nm
.Op Fl display
.Op Ar tags|count|snapshots
.Sh DESCRIPTION
The
.Nm
command is used to display information about tags in a Plakar repository, including a list of tags, a count of their occurrences, or associated snapshots.

.Bl -tag -width Ds
.It Fl display
Specifies the type of output for the tag display. Valid options are:
.Bl -tag -width tags -compact
.It tags
Lists all tags (default).
.It count
Displays each tag with a count of associated snapshots.
.It snapshots
Shows each tag with a list of associated snapshot IDs.
.El
.El

.Sh ARGUMENTS
.Bl -tag -width Ds
.It Ar tags|count|snapshots
This argument, when used with the
.Fl display
flag, controls the output type:
.Bl -tag -width tags -compact
.It tags
Outputs all tags in alphabetical order.
.It count
Displays tags with a count of associated snapshots.
.It snapshots
Lists each tag with associated snapshot IDs.
.El
.El

.Sh EXAMPLES
To illustrate usage:

.Bl -tag -width Ds
.It Basic example:
.Bd -literal -offset indent
plakar tags -display tags
.Ed

.It Example showing tag counts:
.Bd -literal -offset indent
plakar tags -display count
.Ed

.It Example with tag snapshots:
.Bd -literal -offset indent
plakar tags -display snapshots
.Ed
.El

.Sh DIAGNOSTICS
.Ex -std
.Bl -tag -width Ds
.It 0
Command completed successfully.
.It 1
An unsupported display option was specified.
.El

.Sh SEE ALSO
.Xr plakar 1
88 changes: 88 additions & 0 deletions cmd/plakar/subcommands/tags/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2021 Gilles Chehade <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package ls

import (
"flag"
"fmt"
"os"
"sort"
"strings"

"github.com/PlakarKorp/plakar/cmd/plakar/subcommands"
"github.com/PlakarKorp/plakar/context"
"github.com/PlakarKorp/plakar/repository"
"github.com/PlakarKorp/plakar/snapshot"
)

func init() {
subcommands.Register("tags", cmd_tags)
}

func cmd_tags(ctx *context.Context, repo *repository.Repository, args []string) int {
var opt_display string
flags := flag.NewFlagSet("tags", flag.ExitOnError)
flags.StringVar(&opt_display, "display", "tags", "display tags")
flags.Parse(args)

if opt_display != "tags" && opt_display != "count" && opt_display != "snapshots" {
fmt.Fprintf(os.Stderr, "unsupported display option: %s\n", opt_display)
return 1
}

list_tags(repo, opt_display)
return 0
}

func list_tags(repo *repository.Repository, display string) {
tags := make(map[string][][32]byte)
for snapshotID := range repo.ListSnapshots() {
snap, err := snapshot.Load(repo, snapshotID)
if err != nil {
continue
}
for _, tag := range snap.Header.Tags {
if _, ok := tags[tag]; !ok {
tags[tag] = make([][32]byte, 0)
}
tags[tag] = append(tags[tag], snapshotID)
}
}

tagsList := make([]string, 0, len(tags))
for tag := range tags {
tagsList = append(tagsList, tag)
}
sort.Slice(tagsList, func(i, j int) bool {
return tagsList[i] < tagsList[j]
})

if display == "tags" {
fmt.Println(strings.Join(tagsList, "\n"))
} else if display == "count" {
for _, tag := range tagsList {
fmt.Printf("%s: %d\n", tag, len(tags[tag]))
}
} else if display == "snapshots" {
for _, tag := range tagsList {
fmt.Printf("%s:\n", tag)
for _, snapshotID := range tags[tag] {
fmt.Printf(" - %x\n", snapshotID[:4])
}
}
}
}

0 comments on commit 244c866

Please sign in to comment.