first commit
This commit is contained in:
181
cmd/hosts.go
Normal file
181
cmd/hosts.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"playbookctl/internal/space_worker"
|
||||
"playbookctl/internal/types"
|
||||
"playbookctl/internal/utils/logger"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewCommandHosts() *cobra.Command {
|
||||
hostsCmd := &cobra.Command{
|
||||
Use: "hosts",
|
||||
Short: "работа с хостами",
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
return cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
hostsCmd.AddCommand(newCommandHostsList())
|
||||
hostsCmd.AddCommand(newCommandHostsAdd())
|
||||
hostsCmd.AddCommand(newCommandHostsRemove())
|
||||
hostsCmd.AddCommand(newCommandHostsDefault())
|
||||
|
||||
return hostsCmd
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func newCommandHostsList() *cobra.Command {
|
||||
listCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Short: "список хостов",
|
||||
RunE: hostsListRunE,
|
||||
}
|
||||
|
||||
return listCmd
|
||||
}
|
||||
|
||||
func hostsListRunE(_ *cobra.Command, _ []string) error {
|
||||
workDir, err := filepath.Abs(flagWorkdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
spaceWorker := space_worker.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
|
||||
return spaceWorker.ListHosts()
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func newCommandHostsAdd() *cobra.Command {
|
||||
addCmd := &cobra.Command{
|
||||
Use: "add <name> <host[:port]> [user]",
|
||||
Aliases: []string{"new"},
|
||||
Short: "добавить новый хост",
|
||||
Args: cobra.MinimumNArgs(2),
|
||||
RunE: hostsAddRunE,
|
||||
}
|
||||
|
||||
return addCmd
|
||||
}
|
||||
|
||||
func hostsAddRunE(_ *cobra.Command, args []string) error {
|
||||
var (
|
||||
name string
|
||||
host string
|
||||
port uint16
|
||||
user string
|
||||
)
|
||||
|
||||
name = args[0]
|
||||
|
||||
if idx := strings.Index(args[1], ":"); idx > 0 {
|
||||
split := strings.Split(args[1], ":")
|
||||
host = split[0]
|
||||
|
||||
parsedInt, err := strconv.ParseInt(split[1], 0, 16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
port = uint16(parsedInt)
|
||||
} else {
|
||||
host = args[1]
|
||||
port = 22
|
||||
}
|
||||
|
||||
if len(args) == 2 {
|
||||
user = "root"
|
||||
} else if len(args) == 3 {
|
||||
user = args[2]
|
||||
}
|
||||
|
||||
workDir, err := filepath.Abs(flagWorkdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
spaceWorker := space_worker.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
|
||||
return spaceWorker.HostsAdd(name, host, port, user)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func newCommandHostsRemove() *cobra.Command {
|
||||
listCmd := &cobra.Command{
|
||||
Use: "remove <name>",
|
||||
Aliases: []string{"delete", "del", "rm"},
|
||||
Short: "удалить хост",
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: hostsRemoveValidateArgs,
|
||||
RunE: hostsRemoveRunE,
|
||||
}
|
||||
|
||||
return listCmd
|
||||
}
|
||||
|
||||
func hostsRemoveValidateArgs(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
exitApp := func(err error) {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var suggestions []string
|
||||
|
||||
workDir, err := filepath.Abs(flagWorkdir)
|
||||
if err != nil {
|
||||
exitApp(err)
|
||||
}
|
||||
|
||||
hosts, err := types.ReadHosts(workDir)
|
||||
if err != nil {
|
||||
exitApp(err)
|
||||
}
|
||||
|
||||
for host := range *hosts {
|
||||
if toComplete == "" || len(host) >= len(toComplete) && host[:len(toComplete)] == toComplete {
|
||||
suggestions = append(suggestions, host)
|
||||
}
|
||||
}
|
||||
|
||||
return suggestions, cobra.ShellCompDirectiveDefault
|
||||
}
|
||||
|
||||
func hostsRemoveRunE(_ *cobra.Command, args []string) error {
|
||||
workDir, err := filepath.Abs(flagWorkdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
spaceWorker := space_worker.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
|
||||
return spaceWorker.HostRemove(args[0])
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func newCommandHostsDefault() *cobra.Command {
|
||||
command := &cobra.Command{
|
||||
Use: "set-default",
|
||||
Short: "установка хоста по-умолчанию",
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: ArgHostCompletion,
|
||||
RunE: hostsDefaultRunE,
|
||||
}
|
||||
|
||||
return command
|
||||
}
|
||||
|
||||
func hostsDefaultRunE(_ *cobra.Command, args []string) error {
|
||||
workDir, err := filepath.Abs(flagWorkdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
spaceWorker := space_worker.NewSpaceWorker(logger.LogVerbose(flagVerbose), workDir)
|
||||
return spaceWorker.SetDefaultHost(args[0])
|
||||
}
|
||||
Reference in New Issue
Block a user