zutils/main.go

94 lines
2.3 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"github.com/zemenawi/zutils/cmd"
"github.com/zemenawi/zutils/pkg/types"
)
func main() {
registry := types.NewCommandRegistry()
// Register all commands
registry.Register(&types.Command{
Name: "read",
Description: "Read and display file with syntax highlighting",
Handler: cmd.ReadCommand,
})
registry.Register(&types.Command{
Name: "md",
Description: "Render markdown file to terminal",
Handler: cmd.MarkdownCommand,
})
cmd.RegisterInfoCommands(registry)
registry.Register(&types.Command{
Name: "version",
Description: "Show version information",
Handler: cmd.VersionCommand,
})
// Parse command line arguments
args := os.Args[1:]
if len(args) == 0 {
printUsage(registry)
os.Exit(1)
}
// Handle legacy 'info' subcommand pattern for backwards compatibility
if args[0] == "info" {
infoCmd, exists := registry.Get("info")
if !exists {
printUsage(registry)
os.Exit(1)
}
err := infoCmd.Handler(args[1:])
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
return
}
// Direct command: z info <args>
commandName := args[0]
cmdArgs := args[1:]
if command, exists := registry.Get(commandName); exists {
err := command.Handler(cmdArgs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
} else {
// If no command matches, try auto-detect as info command
err := cmd.InfoCommand(args)
if err != nil {
fmt.Printf("Error: %v\n", err)
printUsage(registry)
os.Exit(1)
}
}
}
func printUsage(registry *types.CommandRegistry) {
fmt.Println("z - User-friendly terminal utilities")
fmt.Println()
fmt.Println("Usage: z <command> [arguments]")
fmt.Println()
fmt.Println("Commands:")
for _, command := range registry.GetAll() {
fmt.Printf(" %-12s %s\n", command.Name, command.Description)
}
fmt.Println()
fmt.Println("Quick Examples:")
fmt.Println(" z read go.mod # Display file with line numbers")
fmt.Println(" z info main.go # File information")
fmt.Println(" z info . # Directory information")
fmt.Println(" z info network # Network information")
fmt.Println(" z main.go # Auto-detect file info")
fmt.Println()
}