Clean up code add some log lines and tidy up expandglobs
This commit is contained in:
51
main.go
51
main.go
@@ -43,6 +43,7 @@ type ModifyCommand struct {
|
||||
Reset bool `yaml:"reset"`
|
||||
LogLevel string `yaml:"loglevel"`
|
||||
}
|
||||
type CookFile []ModifyCommand
|
||||
|
||||
func (c *ModifyCommand) Validate() error {
|
||||
if c.Pattern == "" {
|
||||
@@ -89,13 +90,28 @@ func main() {
|
||||
logger.Info("Initializing with log level: %s", level.String())
|
||||
|
||||
commands := []ModifyCommand{}
|
||||
var err error
|
||||
commands, err = LoadCommandsFromCookFiles(*cookfile, commands)
|
||||
logger.Info("Loading commands from cook files: %s", *cookfile)
|
||||
newcommands, err := LoadCommandsFromCookFiles(*cookfile)
|
||||
if err != nil {
|
||||
logger.Error("Failed to load commands from cook files: %v", err)
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
logger.Info("Successfully loaded %d commands from cook files", len(newcommands))
|
||||
commands = append(commands, newcommands...)
|
||||
logger.Info("Now total commands: %d", len(commands))
|
||||
|
||||
logger.Info("Loading commands from arguments: %v", args)
|
||||
newcommands, err = LoadCommandFromArgs(args)
|
||||
if err != nil {
|
||||
logger.Error("Failed to load commands from args: %v", err)
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
logger.Info("Successfully loaded %d commands from args", len(newcommands))
|
||||
commands = append(commands, newcommands...)
|
||||
logger.Info("Now total commands: %d", len(commands))
|
||||
|
||||
// The plan is:
|
||||
// Load all commands
|
||||
// Then aggregate all the globs and deduplicate them
|
||||
@@ -117,14 +133,21 @@ func main() {
|
||||
// TODO: Maybe even figure out how to run individual commands...?
|
||||
// TODO: What to do with git? Figure it out ....
|
||||
|
||||
commands, err = LoadCommandFromArgs(args, commands)
|
||||
logger.Info("Expanding file patterns for %d commands", len(commands))
|
||||
globs := make(map[string]struct{})
|
||||
for _, command := range commands {
|
||||
for _, glob := range command.Files {
|
||||
globs[glob] = struct{}{}
|
||||
}
|
||||
}
|
||||
logger.Info("Found %d unique file patterns", len(globs))
|
||||
files, err := ExpandGLobs(globs)
|
||||
if err != nil {
|
||||
logger.Error("Failed to load commands from args: %v", err)
|
||||
logger.Error("Failed to expand file patterns: %v", err)
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
// Use this to clone loggers for individual commands
|
||||
// logger.WithField("loglevel", level.String())
|
||||
logger.Info("Found %d files to process", len(files))
|
||||
|
||||
// if *gitFlag {
|
||||
// logger.Info("Git integration enabled, setting up git repository")
|
||||
@@ -215,7 +238,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func LoadCommandFromArgs(args []string, commands []ModifyCommand) ([]ModifyCommand, error) {
|
||||
func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
||||
// Cannot reset without git, right?
|
||||
if *resetFlag {
|
||||
*gitFlag = true
|
||||
@@ -224,7 +247,7 @@ func LoadCommandFromArgs(args []string, commands []ModifyCommand) ([]ModifyComma
|
||||
if len(args) < 3 {
|
||||
logger.Error("At least %d arguments are required", 3)
|
||||
flag.Usage()
|
||||
return commands, fmt.Errorf("at least %d arguments are required", 3)
|
||||
return nil, fmt.Errorf("at least %d arguments are required", 3)
|
||||
}
|
||||
|
||||
command := ModifyCommand{
|
||||
@@ -239,14 +262,14 @@ func LoadCommandFromArgs(args []string, commands []ModifyCommand) ([]ModifyComma
|
||||
if err := command.Validate(); err != nil {
|
||||
logger.Error("Invalid command: %v", err)
|
||||
flag.Usage()
|
||||
return commands, fmt.Errorf("invalid command: %v", err)
|
||||
return nil, fmt.Errorf("invalid command: %v", err)
|
||||
}
|
||||
|
||||
return append(commands, command), nil
|
||||
return []ModifyCommand{command}, nil
|
||||
}
|
||||
|
||||
func LoadCommandsFromCookFiles(s string, commands []ModifyCommand) ([]ModifyCommand, error) {
|
||||
return commands, nil
|
||||
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func setupGit() error {
|
||||
@@ -278,7 +301,7 @@ func setupGit() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func expandFilePatterns(patterns []string) ([]string, error) {
|
||||
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
||||
var files []string
|
||||
filesMap := make(map[string]bool)
|
||||
|
||||
@@ -288,7 +311,7 @@ func expandFilePatterns(patterns []string) ([]string, error) {
|
||||
}
|
||||
|
||||
logger.Debug("Expanding patterns from directory: %s", cwd)
|
||||
for _, pattern := range patterns {
|
||||
for pattern, _ := range patterns {
|
||||
logger.Trace("Processing pattern: %s", pattern)
|
||||
matches, _ := doublestar.Glob(os.DirFS(cwd), pattern)
|
||||
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
|
||||
|
Reference in New Issue
Block a user