forked from blacknon/lssh
/
cmd_mkdir.go
152 lines (117 loc) · 3.02 KB
/
cmd_mkdir.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
// Copyright (c) 2019 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// This file describes the code of the built-in command used by lsftp.
// It is quite big in that relationship. Maybe it will be separated or repaired soon.
package sftp
import (
"fmt"
"os"
"path/filepath"
"github.com/bingoohuang/bssh/misc"
"github.com/bingoohuang/bssh/common"
"github.com/urfave/cli"
)
//
func (r *RunSftp) mkdir(args []string) {
// create app
app := cli.NewApp()
// app.UseShortOptionHandling = true
// set parameter
app.Flags = []cli.Flag{
cli.BoolFlag{Name: "p", Usage: "no error if existing, make parent directories as needed"},
}
// set help message
app.CustomAppHelpTemplate = helptext
app.Name = misc.Mkdir
app.Usage = "bssh ftp build-in command: mkdir [remote machine mkdir]"
app.ArgsUsage = misc.Path
app.HideHelp = true
app.HideVersion = true
app.EnableBashCompletion = true
// action
app.Action = r.mkdirAction
// parse short options
args = common.ParseArgs(app.Flags, args)
_ = app.Run(args)
}
func (r *RunSftp) mkdirAction(c *cli.Context) error {
// TDXX(blacknon): 複数のディレクトリ受付(v0.6.1以降)
if len(c.Args()) != 1 {
fmt.Println("Requires one arguments")
fmt.Println("mkdir [path]")
return nil
}
exit := make(chan bool)
for s, cl := range r.Client {
server, client := s, cl
path := c.Args()[0]
go func() {
defer func() { exit <- true }()
// get writer
client.Output.Create(server)
w := client.Output.NewWriter()
// set arg path
if !filepath.IsAbs(path) {
path = filepath.Join(client.Pwd, path)
}
// create directory
var err error
if c.Bool("p") {
err = client.Connect.MkdirAll(path)
} else {
err = client.Connect.Mkdir(path)
}
// check error
if err != nil {
fmt.Fprintf(w, "%s\n", err)
}
fmt.Fprintf(w, "make directory: %s\n", path)
}()
}
for range r.Client {
<-exit
}
return nil
}
//
func (r *RunSftp) lmkdir(args []string) {
// create app
app := cli.NewApp()
// app.UseShortOptionHandling = true
// set parameter
app.Flags = []cli.Flag{
cli.BoolFlag{Name: "p", Usage: "no error if existing, make parent directories as needed"},
}
// set help message
app.CustomAppHelpTemplate = helptext
app.Name = misc.Lmkdir
app.Usage = "bssh ftp build-in command: lmkdir [local machine mkdir]"
app.ArgsUsage = misc.Path
app.HideHelp = true
app.HideVersion = true
app.EnableBashCompletion = true
// action
app.Action = func(c *cli.Context) error {
// TDXX(blacknon): 複数のディレクトリ受付(v0.6.1以降)
if len(c.Args()) != 1 {
fmt.Println("Requires one arguments")
fmt.Println("lmkdir [path]")
return nil
}
path := c.Args()[0]
var err error
if c.Bool("p") {
err = os.MkdirAll(path, 0755)
} else {
err = os.Mkdir(path, 0755)
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
return nil
}
// parse short options
args = common.ParseArgs(app.Flags, args)
_ = app.Run(args)
}