summaryrefslogtreecommitdiff
path: root/cmd/tag/remove.go
blob: ca0f1b4ea386b6a216b951594126f45333048747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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)
	//}
}