mirror of
https://github.com/onyx-and-iris/exclude.git
synced 2026-04-16 14:13:39 +00:00
exclude initial commit
This commit is contained in:
41
cmd/add.go
Normal file
41
cmd/add.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// addCmd represents the add command
|
||||
var addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add patterns to the exclude file",
|
||||
Long: `The add command allows you to add one or more patterns to the .git/info/exclude file.
|
||||
This is useful for excluding files or directories from version control without modifying the .gitignore file.`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
f, ok := FileFromContext(cmd.Context())
|
||||
if !ok {
|
||||
return fmt.Errorf("no exclude file found in context")
|
||||
}
|
||||
return runAddCommand(f, args)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(addCmd)
|
||||
}
|
||||
|
||||
func runAddCommand(f io.Writer, args []string) error {
|
||||
for _, pattern := range args {
|
||||
if _, err := fmt.Fprintln(f, pattern); err != nil {
|
||||
return fmt.Errorf("error writing to exclude file: %w", err)
|
||||
}
|
||||
}
|
||||
fmt.Println("Patterns added to .git/info/exclude file:")
|
||||
for _, pattern := range args {
|
||||
fmt.Println(" -", pattern)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
21
cmd/context.go
Normal file
21
cmd/context.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const fileContextKey contextKey = "excludeFile"
|
||||
|
||||
// ContextWithFile returns a new context with the given file set.
|
||||
func ContextWithFile(ctx context.Context, file *os.File) context.Context {
|
||||
return context.WithValue(ctx, fileContextKey, file)
|
||||
}
|
||||
|
||||
// FileFromContext retrieves the file from the context, if it exists.
|
||||
func FileFromContext(ctx context.Context) (*os.File, bool) {
|
||||
file, ok := ctx.Value(fileContextKey).(*os.File)
|
||||
return file, ok
|
||||
}
|
||||
59
cmd/list.go
Normal file
59
cmd/list.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// listCmd represents the list command
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all patterns in the exclude file",
|
||||
Long: `The list command reads the .git/info/exclude file and prints all patterns
|
||||
that are not empty and do not start with a comment (#).
|
||||
This is useful for reviewing which files or directories are currently excluded from version control.`,
|
||||
Args: cobra.NoArgs, // No arguments expected
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
f, ok := FileFromContext(cmd.Context())
|
||||
if !ok {
|
||||
return fmt.Errorf("no exclude file found in context")
|
||||
}
|
||||
return runListCommand(f, args)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
// runListCommand is the function that will be executed when the list command is called
|
||||
func runListCommand(f io.Reader, _ []string) error {
|
||||
// Read from the exclude file line by line
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line != "" && !strings.HasPrefix(line, "#") {
|
||||
fmt.Println(line) // Print each non-empty, non-comment line
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("error reading exclude file: %w", err)
|
||||
}
|
||||
|
||||
// You can add more functionality as needed
|
||||
return nil
|
||||
}
|
||||
50
cmd/reset.go
Normal file
50
cmd/reset.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// resetCmd represents the reset command
|
||||
var resetCmd = &cobra.Command{
|
||||
Use: "reset",
|
||||
Short: "Reset the exclude file",
|
||||
Long: `The reset command clears all patterns from the .git/info/exclude file.
|
||||
This is useful for starting fresh or removing all exclusions at once.`,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
f, ok := FileFromContext(cmd.Context())
|
||||
if !ok {
|
||||
return fmt.Errorf("no exclude file found in context")
|
||||
}
|
||||
return runResetCommand(f)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(resetCmd)
|
||||
}
|
||||
|
||||
//go:embed template/exclude
|
||||
var defaultExcludeFile string
|
||||
|
||||
// runResetCommand clears the exclude file
|
||||
func runResetCommand(f *os.File) error {
|
||||
// Clear the exclude file by truncating it
|
||||
if err := f.Truncate(0); err != nil {
|
||||
return fmt.Errorf("error truncating exclude file: %w", err)
|
||||
}
|
||||
// Reset the file pointer to the beginning
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
return fmt.Errorf("error seeking to the beginning of exclude file: %w", err)
|
||||
}
|
||||
|
||||
// Write the default exclude patterns to the file
|
||||
if _, err := f.WriteString(defaultExcludeFile); err != nil {
|
||||
return fmt.Errorf("error writing default exclude file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
65
cmd/root.go
Normal file
65
cmd/root.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// 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.
|
||||
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)
|
||||
if err != nil {
|
||||
cmd.Println("Error opening .git/info/exclude file:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(context.Background(), fileContextKey, f)
|
||||
cmd.SetContext(ctx)
|
||||
},
|
||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||
if f, ok := FileFromContext(cmd.Context()); ok {
|
||||
defer f.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() {
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.exclude.yaml)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
6
cmd/template/exclude
Normal file
6
cmd/template/exclude
Normal file
@@ -0,0 +1,6 @@
|
||||
# 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]
|
||||
# *~
|
||||
Reference in New Issue
Block a user