-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_show_config.go
55 lines (45 loc) · 947 Bytes
/
cmd_show_config.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
flag "github.com/spf13/pflag"
)
func showHostConfig(cfg *Config) (Host, error) {
args := flag.Args()
if len(args) == 0 {
return Host{}, errors.New("Need specify connection name")
}
if len(args) != 1 {
return Host{}, errors.New("Too much input arguments. Need specify only one connection name")
}
h, err := cfg.getHost(args[0])
if err != nil {
return h, err
}
home, err := homeDir()
if err != nil {
return h, err
}
fileName := filepath.Join(home, h.fileName)
file, err := os.Open(fileName)
if err != nil {
return h, fmt.Errorf("Can't open config file %q: %w", fileName, err)
}
var text []string
s := bufio.NewScanner(file)
s.Split(bufio.ScanLines)
for i := 1; s.Scan(); i++ {
if i >= h.lineStart {
text = append(text, s.Text())
}
if i > h.lineEnd {
break
}
}
h.text = strings.Join(text, "\n")
return h, nil
}