diff --git a/cmd/group.go b/cmd/group.go index 075a842..3a4e30e 100644 --- a/cmd/group.go +++ b/cmd/group.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "strings" "github.com/spf13/cobra" ) @@ -15,123 +16,71 @@ var groupListCmd = &cobra.Command{ Use: "list", Short: "List server groups", RunE: func(cmd *cobra.Command, args []string) error { - servers, err := appDB.ListServers() + groups, err := appDB.GetGroups() if err != nil { - return fmt.Errorf("list servers: %w", err) - } - - groups := make(map[string]int) - for _, s := range servers { - g := s.GroupName - if g == "" { - g = "(no group)" - } - groups[g]++ + return fmt.Errorf("list groups: %w", err) } if len(groups) == 0 { - fmt.Println("No servers.") + fmt.Println("No groups. Use 'sshkeeper add --group ' to create one.") return nil } - for name, count := range groups { - fmt.Printf(" %-20s %d servers\n", name, count) + for _, g := range groups { + fmt.Printf(" %s\n", g) } return nil }, } -var templateCmd = &cobra.Command{ - Use: "template", - Short: "Command template management", -} - -var templateListCmd = &cobra.Command{ - Use: "list ", - Short: "List command templates for a server", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - alias := args[0] - _, err := appDB.GetServer(alias) - if err != nil { - return fmt.Errorf("server not found: %s", alias) - } - - templates, err := appDB.GetCommandTemplates(alias) - if err != nil { - return fmt.Errorf("list templates: %w", err) - } - - if len(templates) == 0 { - fmt.Println("No command templates.") - return nil - } - - for _, t := range templates { - fmt.Printf(" %-15s %s\n", t.Name, t.Command) - } - return nil - }, -} - -var templateAddCmd = &cobra.Command{ - Use: "add ", - Short: "Add a command template", - Args: cobra.ExactArgs(3), - RunE: func(cmd *cobra.Command, args []string) error { - alias := args[0] - name := args[1] - command := args[2] - - server, err := appDB.GetServer(alias) - if err != nil { - return fmt.Errorf("server not found: %s", alias) - } - - if err := appDB.AddCommandTemplate(server.ID, name, command); err != nil { - return fmt.Errorf("add template: %w", err) - } - - fmt.Println("Template added.") - return nil - }, -} - -var runTemplateCmd = &cobra.Command{ - Use: "run-template