mirror of
https://github.com/onyx-and-iris/exclude.git
synced 2026-03-30 23:19:09 +00:00
add tests for all commands wrap entry point with fang add --version flag target to Taskfile
59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
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 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) {
|
|
if _, err := os.Stat(".git"); os.IsNotExist(err) {
|
|
cmd.Println("Error: This command must be run in a Git repository.")
|
|
os.Exit(1)
|
|
}
|
|
|
|
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 := createContext(f, cmd.OutOrStdout())
|
|
cmd.SetContext(ctx)
|
|
},
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
if obj, ok := ContextObjectFromContext(cmd.Context()); ok {
|
|
defer obj.File.Close()
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.PersistentFlags().
|
|
StringP("path", "p", ".git/info/", "Path the exclude file resides in (default is .git/info/)")
|
|
}
|