From 17fb6add26291b31f7020e3551a7c8487130a747 Mon Sep 17 00:00:00 2001 From: "F.O." Date: Sun, 16 Feb 2025 17:56:08 +0100 Subject: genesi --- cmd/tag/remove.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 cmd/tag/remove.go (limited to 'cmd/tag/remove.go') 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 + +*/ +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(®ex, "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) + //} +} -- cgit v1.2.3