add del command

add tests for all commands

wrap entry point with fang

add --version flag
target to Taskfile
This commit is contained in:
2026-03-29 21:15:56 +01:00
parent 8f6a5d4472
commit 8059255d52
16 changed files with 520 additions and 80 deletions

View File

@@ -1,56 +1,58 @@
package cmd
import (
"context"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
const defaultExcludeFileContent = `# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
`
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "exclude",
Short: "A command line tool to manage .git/info/exclude files",
Long: `Exclude is a command line tool designed to help you manage your .git/info/exclude files.
It allows you to add, list, and remove patterns from the exclude file easily.
It allows you to add, list, and delete patterns from the exclude file easily.
This tool is particularly useful for developers who want to keep their repository clean
by excluding certain files or directories from version control.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// This function runs before any command is executed.
// You can use it to set up global configurations or checks.
// For example, you might want to check if the .git directory exists
if _, err := os.Stat(".git"); os.IsNotExist(err) {
cmd.Println("Error: This command must be run in a Git repository.")
os.Exit(1)
}
f, err := os.OpenFile(".git/info/exclude", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
path, err := cmd.Flags().GetString("path")
if err != nil {
cmd.Println("Error getting path flag:", err)
os.Exit(1)
}
f, err := os.OpenFile(filepath.Join(path, "exclude"), os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
cmd.Println("Error opening .git/info/exclude file:", err)
os.Exit(1)
}
ctx := context.WithValue(context.Background(), fileContextKey, f)
ctx := createContext(f, cmd.OutOrStdout())
cmd.SetContext(ctx)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if f, ok := FileFromContext(cmd.Context()); ok {
defer f.Close()
if obj, ok := ContextObjectFromContext(cmd.Context()); ok {
defer obj.File.Close()
}
},
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
RootCmd.PersistentFlags().
StringP("path", "p", ".git/info/", "Path the exclude file resides in (default is .git/info/)")
}