File: complete.go

package info (click to toggle)
golang-github-colinmarc-hdfs 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,760 kB
  • sloc: sh: 130; xml: 40; makefile: 31
file content (130 lines) | stat: -rw-r--r-- 2,293 bytes parent folder | download
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
package main

import (
	"fmt"
	"io"
	"os"
	"path"
	"strings"
)

var knownCommands = []string{
	"ls",
	"rm",
	"mv",
	"mkdir",
	"touch",
	"chmod",
	"chown",
	"cat",
	"head",
	"tail",
	"du",
	"checksum",
	"get",
	"getmerge",
	"put",
	"df",
}

func complete(args []string) {
	if len(args) == 2 {
		words := strings.Split(args[1], " ")[1:]

		if len(words) <= 1 {
			fmt.Println(strings.Join(knownCommands, " "))
		} else if isKnownCommand(words[0]) {
			position := countPosition(words)
			completeArg(words[0], words[len(words)-1], position)
		}
	} else {
		fmt.Println(strings.Join(knownCommands, " "))
	}
}

func completeArg(command, fragment string, position int) {
	if (command == "put" && position == 1) ||
		((command == "get" || command == "getmerge") && position == 2) {
		fmt.Println("_FILE_") // The bash_completion bit knows about this special string.
	} else if (command == "chmod" || command == "chown") && position == 1 {
		return
	} else if !strings.HasPrefix(fragment, "-") {
		completePath(fragment)
	}
}

func completePath(fragment string) {
	paths, namenode, err := normalizePaths([]string{fragment})
	if err != nil {
		return
	}

	client, err := getClient(namenode)
	if err != nil {
		return
	}

	fullPath := paths[0]
	if fullPath == "" {
		fullPath = userDir(client) + "/"
	} else if hasGlob(fullPath) {
		return
	}

	var dir, prefix string
	if strings.HasSuffix(fragment, "/") {
		dir = fullPath
		prefix = ""
	} else {
		dir, prefix = path.Split(fullPath)
	}

	dirReader, err := client.Open(dir)
	if err != nil {
		return
	}

	// 1000 entries should align with what HDFS returns. If not, well, it was
	// going to be slow anyway.
	var partial []os.FileInfo
	for ; err != io.EOF; partial, err = dirReader.Readdir(1000) {
		if err != nil {
			return
		}

		for _, fi := range partial {
			name := fi.Name()

			if strings.HasPrefix(name, prefix) {
				p := path.Join(dir, name)
				if fi.IsDir() {
					p += "/"
				}

				fmt.Print(" " + p)
			}
		}
	}

	fmt.Println()
}

func isKnownCommand(command string) bool {
	for _, c := range knownCommands {
		if command == c {
			return true
		}
	}
	return false
}

func countPosition(words []string) int {
	var position int
	for _, w := range words {
		if !strings.HasPrefix(w, "-") {
			position++
		}
	}

	return position - 1
}