/
actions.go
152 lines (136 loc) · 3.57 KB
/
actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/xwjdsh/manssh"
"github.com/xwjdsh/manssh/utils"
"github.com/urfave/cli"
)
var (
path string
)
func listCmd(c *cli.Context) error {
hosts, err := manssh.List(path, manssh.ListOption{
Keywords: c.Args(),
IgnoreCase: c.Bool("ignorecase"),
})
if err != nil {
fmt.Printf(utils.ErrorFlag)
return cli.NewExitError(err, 1)
}
fmt.Printf("%s total records: %d\n\n", utils.SuccessFlag, len(hosts))
printHosts(c.Bool("path"), hosts)
return nil
}
func addCmd(c *cli.Context) error {
// Check arguments count
if err := utils.ArgumentsCheck(c.NArg(), 1, 2); err != nil {
return printErrorWithHelp(c, err)
}
ao := &manssh.AddOption{
Alias: c.Args().Get(0),
Connect: c.Args().Get(1),
Path: c.String("addpath"),
}
if ao.Path != "" {
var err error
if ao.Path, err = filepath.Abs(ao.Path); err != nil {
fmt.Printf(utils.ErrorFlag)
return cli.NewExitError(err, 1)
}
}
if kvConfig := c.Generic("config"); kvConfig != nil {
ao.Config = kvConfig.(*kvFlag).m
}
if ao.Config == nil {
ao.Config = make(map[string]string)
}
if identityfile := c.String("identityfile"); identityfile != "" {
ao.Config["identityfile"] = identityfile
}
if len(ao.Config) == 0 && ao.Connect == "" {
return printErrorWithHelp(c, errors.New("param error"))
}
host, err := manssh.Add(path, ao)
if err != nil {
fmt.Printf(utils.ErrorFlag)
return cli.NewExitError(err, 1)
}
fmt.Printf("%s added successfully\n", utils.SuccessFlag)
if host != nil {
fmt.Println()
printHost(c.Bool("path"), host)
}
return nil
}
func updateCmd(c *cli.Context) error {
if err := utils.ArgumentsCheck(c.NArg(), 1, 2); err != nil {
return printErrorWithHelp(c, err)
}
uo := &manssh.UpdateOption{
Alias: c.Args().Get(0),
Connect: c.Args().Get(1),
NewAlias: c.String("rename"),
}
if kvConfig := c.Generic("config"); kvConfig != nil {
uo.Config = kvConfig.(*kvFlag).m
}
if uo.Config == nil {
uo.Config = make(map[string]string)
}
if identityfile := c.String("identityfile"); identityfile != "" || c.IsSet("identityfile") {
uo.Config["identityfile"] = identityfile
}
if !uo.Valid() {
return cli.NewExitError("the update option is invalid", 1)
}
host, err := manssh.Update(path, uo)
if err != nil {
fmt.Printf(utils.ErrorFlag)
return cli.NewExitError(err, 1)
}
fmt.Printf("%s updated successfully\n\n", utils.SuccessFlag)
printHost(c.Bool("path"), host)
return nil
}
func deleteCmd(c *cli.Context) error {
if err := utils.ArgumentsCheck(c.NArg(), 1, -1); err != nil {
return printErrorWithHelp(c, err)
}
hosts, err := manssh.Delete(path, c.Args()...)
if err != nil {
fmt.Printf(utils.ErrorFlag)
return cli.NewExitError(err, 1)
}
fmt.Printf("%s deleted successfully\n\n", utils.SuccessFlag)
printHosts(c.Bool("path"), hosts)
return nil
}
func backupCmd(c *cli.Context) error {
if err := utils.ArgumentsCheck(c.NArg(), 1, 1); err != nil {
return printErrorWithHelp(c, err)
}
backupPath := c.Args().First()
os.MkdirAll(backupPath, os.ModePerm)
paths, err := manssh.GetFilePaths(path)
if err != nil {
return cli.NewExitError(err, 1)
}
pathDir := filepath.Dir(path)
for _, p := range paths {
bp := backupPath
if p != path && strings.HasPrefix(p, pathDir) {
bp = filepath.Join(bp, strings.Replace(p, pathDir, "", 1))
os.MkdirAll(filepath.Dir(bp), os.ModePerm)
}
if err := exec.Command("cp", p, bp).Run(); err != nil {
return cli.NewExitError(err, 1)
}
}
fmt.Printf("%s backup ssh config to [%s] successfully\n", utils.SuccessFlag, backupPath)
return nil
}