summaryrefslogtreecommitdiff
path: root/cmd/tag/remove.go
diff options
context:
space:
mode:
authorF.O. <scrotadamus@insiberia.net>2025-02-16 17:56:08 +0100
committerF.O. <scrotadamus@insiberia.net>2025-02-16 17:56:57 +0100
commit17fb6add26291b31f7020e3551a7c8487130a747 (patch)
treed4559a7339ed181393ff921909e6ce05b7c2cf18 /cmd/tag/remove.go
genesi
Diffstat (limited to 'cmd/tag/remove.go')
-rw-r--r--cmd/tag/remove.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/cmd/tag/remove.go b/cmd/tag/remove.go
new file mode 100644
index 0000000..ca0f1b4
--- /dev/null
+++ b/cmd/tag/remove.go
@@ -0,0 +1,87 @@
+/*
+Copyright © 2025 NAME HERE <EMAIL ADDRESS>
+
+*/
+package tag
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/spf13/cobra"
+ "github.com/scrotadamus/ghligh/document"
+)
+
+var tagRemoveCmd = &cobra.Command{
+ Use: "remove",
+ Short: "remove ghligh tags from a pdf files using regex",
+ Long: `A longer description that spans multiple lines and likely contains examples
+and usage of using your command. For example:
+
+Cobra is a CLI library for Go that empowers applications.
+This application is a tool to generate the needed files
+to quickly create a Cobra application.`,
+ Run: func(cmd *cobra.Command, args []string) {
+ if len(args) == 0 {
+ cmd.Help()
+ return
+ }
+
+ if regex == "" && exact == "" {
+ fmt.Fprintf(os.Stderr, "either regex or exact must be set with --regex or --exact\n")
+ os.Exit(1)
+ }
+
+ nosafe, err := cmd.Flags().GetBool("nosafe")
+ if err != nil {
+ cmd.Help()
+ return
+ }
+
+ // just a little hack -> boundaries = nosafe ? "" : `\b`
+ boundaries := map[bool]string{true: "", false: `\b`}[nosafe]
+ regex = formatRegex(regex, boundaries)
+
+ // if exact set overwrite regex
+ if exact != "" {
+ regex = `^` + exact + `$`
+ }
+
+ for _, file := range(args){
+ doc, err := document.Open(file)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%v\n", err)
+ continue
+ }
+
+ tags := regexSlice(regex, doc.GetTags())
+ removedTags := doc.RemoveTags(tags)
+ doc.Save()
+ doc.Close()
+
+ fmt.Printf("removed %d tags from %s\n", removedTags, doc.Path)
+ }
+
+ },
+}
+
+func init() {
+ TagCmd.AddCommand(tagRemoveCmd)
+
+ // Here you will define your flags and configuration settings.
+
+ // Cobra supports Persistent Flags which will work for this command
+ // and all subcommands, e.g.:
+ // removetagsCmd.PersistentFlags().String("foo", "", "A help for foo")
+
+ // Cobra supports local flags which will only run when this command
+ // is called directly, e.g.:
+ // removetagsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+ tagRemoveCmd.Flags().StringVarP(&regex, "regex", "r", "", "regex")
+ tagRemoveCmd.Flags().StringVarP(&exact, "exact", "e", "", "exact")
+ tagRemoveCmd.Flags().BoolP("nosafe", "", false, "don't use safe boundaries around regex")
+
+ //if err := tagRemoveCmd.MarkFlagRequired("regex"); err != nil {
+ //panic(err)
+ //}
+}