Skip to content

Commit a458c8b

Browse files
committed
added fs tool shell screen for DFS FileSystem
1 parent ca36844 commit a458c8b

17 files changed

+1593
-113
lines changed

fs-tool/common/animation.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package common
22

33
import (
4-
"fmt"
54
"sync"
65
"time"
6+
7+
"github.com/freakmaxi/kertish-dfs/fs-tool/terminal"
78
)
89

910
type animation struct {
@@ -12,20 +13,22 @@ type animation struct {
1213
stop chan bool
1314
cancel chan bool
1415

16+
output terminal.Output
1517
header string
1618
}
1719

18-
func NewAnimation(header string) *animation {
20+
func NewAnimation(output terminal.Output, header string) *animation {
1921
return &animation{
2022
waitGroup: sync.WaitGroup{},
2123
stop: make(chan bool),
2224
cancel: make(chan bool),
25+
output: output,
2326
header: header,
2427
}
2528
}
2629

2730
func (p *animation) Start() {
28-
fmt.Printf("%s |", p.header)
31+
p.output.Printf("%s |", p.header)
2932

3033
p.waitGroup.Add(1)
3134
go func() {
@@ -37,17 +40,17 @@ func (p *animation) Start() {
3740
for {
3841
select {
3942
case <-p.stop:
40-
fmt.Printf("\033[%dD", 1)
41-
fmt.Printf("ok.\n")
43+
p.output.Remove(1)
44+
p.output.Printf("ok.\n")
4245
return
4346
case <-p.cancel:
44-
fmt.Printf("\033[%dD", 1)
45-
fmt.Printf("failed.\n")
47+
p.output.Remove(1)
48+
p.output.Printf("failed.\n")
4649
return
4750
default:
4851
c := chars[i%len(chars)]
49-
fmt.Printf("\033[%dD", 1)
50-
fmt.Printf(string(c))
52+
p.output.Remove(1)
53+
p.output.Printf(string(c))
5154
time.Sleep(time.Millisecond * 100)
5255
}
5356
i++

fs-tool/common/helpers.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
"path"
6+
"strings"
7+
)
8+
9+
const pathSeparator = "/"
10+
11+
func CorrectPaths(paths []string) []string {
12+
for i := range paths {
13+
folderPath := paths[i]
14+
15+
if strings.Index(folderPath, pathSeparator) != 0 {
16+
folderPath = fmt.Sprintf("%s%s", pathSeparator, folderPath)
17+
}
18+
folderPath = path.Clean(folderPath)
19+
20+
if len(folderPath) == 0 {
21+
folderPath = pathSeparator
22+
}
23+
24+
paths[i] = folderPath
25+
}
26+
return paths
27+
}
28+
29+
func CorrectPath(folderPath string) string {
30+
return CorrectPaths([]string{folderPath})[0]
31+
}
32+
33+
func PathTree(folderPath string) []string {
34+
folderPath = CorrectPath(folderPath)
35+
36+
folderTree := make([]string, 0)
37+
split := strings.Split(folderPath, pathSeparator)
38+
for len(split) > 0 {
39+
p := strings.Join(split, pathSeparator)
40+
folderTree = append([]string{CorrectPath(p)}, folderTree...)
41+
42+
split = split[:len(split)-1]
43+
}
44+
45+
return folderTree
46+
}
47+
48+
func Split(path string) (string, string) {
49+
path = CorrectPath(path)
50+
if strings.Compare(path, pathSeparator) == 0 {
51+
return pathSeparator, ""
52+
}
53+
54+
idx := strings.LastIndex(path, pathSeparator)
55+
if idx == 0 {
56+
return pathSeparator, path[1:]
57+
}
58+
59+
return path[:idx], path[idx+1:]
60+
}
61+
62+
func Join(inputs ...string) string {
63+
for i, p := range inputs {
64+
if strings.Index(p, pathSeparator) == 0 {
65+
p = p[1:]
66+
}
67+
inputs[i] = p
68+
}
69+
return CorrectPath(strings.Join(inputs, pathSeparator))
70+
}
71+
72+
func DivideParts(folderPath string) []string {
73+
return strings.Split(folderPath, pathSeparator)
74+
}
75+
76+
func Absolute(basePath string, folderPath string) string {
77+
if strings.Index(folderPath, pathSeparator) == 0 {
78+
basePath = pathSeparator
79+
}
80+
81+
targetParts := DivideParts(folderPath)
82+
for len(targetParts) > 0 {
83+
if strings.Compare(targetParts[0], "..") != 0 {
84+
basePath = Join(basePath, targetParts[0])
85+
targetParts = targetParts[1:]
86+
continue
87+
}
88+
parentPath, _ := Split(basePath)
89+
basePath = parentPath
90+
targetParts = targetParts[1:]
91+
}
92+
return basePath
93+
}

fs-tool/dfs/head.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func Change(headAddresses []string, sources []string, target string, overwrite b
100100
case 412:
101101
return fmt.Errorf("%s have conflicts between file(s)/folder(s)", sourcesErrorString(sources))
102102
case 422:
103-
return fmt.Errorf("%s and %s should be absolute paths", sourcesErrorString(sources), target)
103+
return fmt.Errorf("%s and %s should be full and absolute paths", sourcesErrorString(sources), target)
104104
case 500:
105105
if strings.Compare(action, "m") == 0 {
106106
action = "move"

fs-tool/flags/change_directory.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package flags
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/freakmaxi/kertish-dfs/fs-tool/common"
8+
"github.com/freakmaxi/kertish-dfs/fs-tool/dfs"
9+
"github.com/freakmaxi/kertish-dfs/fs-tool/terminal"
10+
)
11+
12+
type changeDirectoryCommand struct {
13+
headAddresses []string
14+
output terminal.Output
15+
args []string
16+
17+
target string
18+
19+
CurrentFolder *common.Folder
20+
}
21+
22+
func NewChangeDirectory(headAddresses []string, output terminal.Output, args []string) execution {
23+
return &changeDirectoryCommand{
24+
headAddresses: headAddresses,
25+
output: output,
26+
args: args,
27+
}
28+
}
29+
30+
func (c *changeDirectoryCommand) Parse() error {
31+
if len(c.args) != 1 {
32+
return fmt.Errorf("cd command needs only target parameter")
33+
}
34+
35+
c.target = c.args[0]
36+
37+
return nil
38+
}
39+
40+
func (c *changeDirectoryCommand) PrintUsage() {
41+
c.output.Println(" cd Change folders.")
42+
c.output.Println(" Ex: cd [target]")
43+
c.output.Println("")
44+
c.output.Refresh()
45+
}
46+
47+
func (c *changeDirectoryCommand) Name() string {
48+
return "cd"
49+
}
50+
51+
func (c *changeDirectoryCommand) Execute() error {
52+
if strings.Index(c.target, local) == 0 {
53+
return fmt.Errorf("cd works only for dfs folder(s)")
54+
}
55+
56+
anim := common.NewAnimation(c.output, "processing...")
57+
anim.Start()
58+
59+
folder, err := dfs.List(c.headAddresses, c.target, false)
60+
if err != nil {
61+
anim.Cancel()
62+
return err
63+
}
64+
anim.Stop()
65+
66+
c.CurrentFolder = folder
67+
68+
return nil
69+
}

fs-tool/flags/command.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package flags
22

33
import (
44
"fmt"
5+
"os"
56
"path"
7+
8+
"github.com/freakmaxi/kertish-dfs/fs-tool/terminal"
69
)
710

811
const local = "local:"
@@ -52,6 +55,7 @@ func (c *Command) printUsage() {
5255
fmt.Println(" cp Copy file or folder.")
5356
fmt.Println(" mv Move file or folder.")
5457
fmt.Println(" rm Remove files and/or folders.")
58+
fmt.Println(" sh Enter shell mode of fs-tool.")
5559
fmt.Println()
5660
}
5761

@@ -85,14 +89,14 @@ func (c *Command) Parse() bool {
8589
}
8690

8791
switch arg {
88-
case "mkdir", "ls", "cp", "mv", "rm":
92+
case "mkdir", "ls", "cp", "mv", "rm", "sh":
8993
mrArgs := make([]string, 0)
9094
if i+1 < len(c.args) {
9195
mrArgs = c.args[i+1:]
9296
}
9397

9498
var err error
95-
c.command, err = newExecution([]string{c.headAddress}, arg, mrArgs)
99+
c.command, err = newExecution([]string{c.headAddress}, terminal.NewStdOut(), arg, string(os.PathSeparator), mrArgs, c.version)
96100
if err != nil {
97101
fmt.Println(err.Error())
98102
fmt.Println()

0 commit comments

Comments
 (0)