zutils/main.go

152 lines
4.3 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"strings"
"github.com/zemenawi/zutils/cmd"
"github.com/zemenawi/zutils/pkg/colors"
"github.com/zemenawi/zutils/pkg/types"
)
func main() {
registry := types.NewCommandRegistry()
registry.Register(&types.Command{
Name: "read",
Description: "Read and display file with smart detection",
Handler: cmd.ReadCommand,
})
registry.Register(&types.Command{
Name: "sys",
Description: "Display system information overview",
Handler: cmd.SystemCommand,
})
registry.Register(&types.Command{
Name: "find",
Description: "Find files with pattern and filter support",
Handler: cmd.FindCommand,
})
registry.Register(&types.Command{
Name: "search",
Description: "Search for text pattern in files",
Handler: cmd.SearchCommand,
})
registry.Register(&types.Command{
Name: "usage",
Description: "Show resource usage for a process",
Handler: cmd.UsageCommand,
})
cmd.RegisterInfoCommands(registry)
registry.Register(&types.Command{
Name: "ports",
Description: "Show listening ports and processes using them",
Handler: cmd.PortsCommand,
})
registry.Register(&types.Command{
Name: "usages",
Description: "Show all processes sorted by resource usage",
Handler: cmd.UsagesCommand,
})
registry.Register(&types.Command{
Name: "network",
Description: "Show network information (IP, connections, DNS)",
Handler: cmd.NetworkCommand,
})
registry.Register(&types.Command{
Name: "junks",
Description: "Find and clean junk files (caches, temporary files)",
Handler: cmd.JunksCommand,
})
registry.Register(&types.Command{
Name: "version",
Description: "Show version information",
Handler: cmd.VersionCommand,
})
args := os.Args[1:]
if len(args) == 0 {
printUsage(registry)
os.Exit(1)
}
if args[0] == "info" {
infoCmd, exists := registry.Get("info")
if !exists {
printUsage(registry)
os.Exit(1)
}
err := infoCmd.Handler(args[1:])
if err != nil {
cmd.PrintErrorAndHelp("info", err)
os.Exit(1)
}
return
}
commandName := args[0]
cmdArgs := args[1:]
if command, exists := registry.Get(commandName); exists {
err := command.Handler(cmdArgs)
if err != nil {
cmd.PrintErrorAndHelp(commandName, err)
os.Exit(1)
}
} else {
fmt.Printf("\n %sCommand not found: %s%s\n\n", colors.Red, args[0], colors.Reset)
fmt.Printf(" Did you mean one of these?\n\n")
suggestions := findSimilarCommands(args[0], registry.GetAll())
for _, s := range suggestions {
fmt.Printf(" %s%s%s - %s\n", colors.Yellow, s.Name, colors.Reset, s.Description)
}
fmt.Printf("\n")
printUsage(registry)
os.Exit(1)
}
}
func printUsage(registry *types.CommandRegistry) {
fmt.Printf("\n %s%sz%s - User-friendly terminal utilities\n\n", colors.Bold, colors.Cyan, colors.Reset)
fmt.Printf(" %sUsage:%s z <command> [arguments]\n\n", colors.Bold, colors.Reset)
fmt.Printf(" %sAvailable Commands:%s\n", colors.Bold, colors.Reset)
for _, command := range registry.GetAll() {
fmt.Printf(" %s%-10s%s %s\n", colors.Green, command.Name, colors.Reset, command.Description)
}
fmt.Printf("\n %sQuick Examples:%s\n", colors.Bold, colors.Reset)
fmt.Printf(" %sz read README.md%s # Display file with smart formatting\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz sys%s # Show system overview\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz find . -name '*.go'%s # Find files\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz search pattern path%s # Search in files\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz info .%s # Directory info with gitignore support\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz info main.go%s # File information\n", colors.Gray, colors.Reset)
fmt.Printf("\n Type %sz <command>%s without arguments to see help\n\n", colors.Gray, colors.Reset)
}
func findSimilarCommands(input string, commands []*types.Command) []*types.Command {
var suggestions []*types.Command
for _, cmd := range commands {
if strings.HasPrefix(cmd.Name, input[:1]) {
suggestions = append(suggestions, cmd)
}
}
if len(suggestions) > 3 {
suggestions = suggestions[:3]
}
return suggestions
}