mirror of
https://github.com/onyx-and-iris/exclude.git
synced 2026-03-30 23:19:09 +00:00
return errors from pre/post hooks
This commit is contained in:
parent
dabb14f65c
commit
147f474986
22
cmd/root.go
22
cmd/root.go
@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -24,31 +25,32 @@ var RootCmd = &cobra.Command{
|
|||||||
It allows you to add, list, and delete 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
|
This tool is particularly useful for developers who want to keep their repository clean
|
||||||
by excluding certain files or directories from version control.`,
|
by excluding certain files or directories from version control.`,
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if _, err := os.Stat(".git"); os.IsNotExist(err) {
|
if _, err := os.Stat(".git"); os.IsNotExist(err) {
|
||||||
cmd.Println("Error: This command must be run in a Git repository.")
|
return fmt.Errorf("this command must be run in a Git repository")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := cmd.Flags().GetString("path")
|
path, err := cmd.Flags().GetString("path")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Println("Error getting path flag:", err)
|
return fmt.Errorf("error reading path flag: %w", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.OpenFile(filepath.Join(path, "exclude"), os.O_RDWR|os.O_APPEND, 0644)
|
f, err := os.OpenFile(filepath.Join(path, "exclude"), os.O_RDWR|os.O_APPEND, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Println("Error opening .git/info/exclude file:", err)
|
return fmt.Errorf("error opening exclude file: %w", err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := createContext(f, cmd.OutOrStdout())
|
ctx := createContext(f, cmd.OutOrStdout())
|
||||||
cmd.SetContext(ctx)
|
cmd.SetContext(ctx)
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if obj, ok := ContextObjectFromContext(cmd.Context()); ok {
|
if ctx, ok := ContextObjectFromContext(cmd.Context()); ok {
|
||||||
defer obj.File.Close()
|
defer ctx.File.Close()
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("unable to retrieve context after command execution")
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user