mirror of
https://github.com/onyx-and-iris/exclude.git
synced 2026-04-16 14:13:39 +00:00
add del command
add tests for all commands wrap entry point with fang add --version flag target to Taskfile
This commit is contained in:
17
cmd/list.go
17
cmd/list.go
@@ -18,31 +18,36 @@ 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())
|
||||
ctx, ok := ContextObjectFromContext(cmd.Context())
|
||||
if !ok {
|
||||
return fmt.Errorf("no exclude file found in context")
|
||||
}
|
||||
return runListCommand(f, args)
|
||||
return runListCommand(ctx.Out, ctx.File)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
RootCmd.AddCommand(listCmd)
|
||||
}
|
||||
|
||||
// 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
|
||||
func runListCommand(out io.Writer, f io.Reader) error {
|
||||
var count int
|
||||
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
|
||||
fmt.Fprintln(out, line)
|
||||
count++
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("error reading exclude file: %w", err)
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
fmt.Fprintln(out, "No patterns found in the exclude file.")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user