/
applet.go
210 lines (197 loc) · 10 KB
/
applet.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// mimixbox/internal/applets/applet.go
//
// Copyright 2021 Naohiro CHIKAMATSU
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package applets
import (
"fmt"
"go/doc"
"os"
"sort"
"strconv"
gzipCmd "github.com/nao1215/mimixbox/internal/applets/archival/gzip"
"github.com/nao1215/mimixbox/internal/applets/console-tools/clear"
"github.com/nao1215/mimixbox/internal/applets/console-tools/reset"
addShell "github.com/nao1215/mimixbox/internal/applets/debianutils/add-shell"
"github.com/nao1215/mimixbox/internal/applets/debianutils/ischroot"
removeShell "github.com/nao1215/mimixbox/internal/applets/debianutils/remove-shell"
"github.com/nao1215/mimixbox/internal/applets/debianutils/which"
"github.com/nao1215/mimixbox/internal/applets/fileutils/chgrp"
"github.com/nao1215/mimixbox/internal/applets/fileutils/chown"
"github.com/nao1215/mimixbox/internal/applets/fileutils/cp"
"github.com/nao1215/mimixbox/internal/applets/fileutils/ln"
"github.com/nao1215/mimixbox/internal/applets/fileutils/mkdir"
"github.com/nao1215/mimixbox/internal/applets/fileutils/mkfifo"
"github.com/nao1215/mimixbox/internal/applets/fileutils/mv"
"github.com/nao1215/mimixbox/internal/applets/fileutils/rm"
"github.com/nao1215/mimixbox/internal/applets/fileutils/rmdir"
"github.com/nao1215/mimixbox/internal/applets/fileutils/touch"
"github.com/nao1215/mimixbox/internal/applets/games/lifegame"
"github.com/nao1215/mimixbox/internal/applets/jokeutils/cowsay"
"github.com/nao1215/mimixbox/internal/applets/jokeutils/fakemovie"
"github.com/nao1215/mimixbox/internal/applets/jokeutils/sl"
//"github.com/nao1215/mimixbox/internal/applets/loginutils/chsh"
validShell "github.com/nao1215/mimixbox/internal/applets/debianutils/valid-shell"
"github.com/nao1215/mimixbox/internal/applets/pmutils/halt"
"github.com/nao1215/mimixbox/internal/applets/shellutils/base64"
"github.com/nao1215/mimixbox/internal/applets/shellutils/basename"
"github.com/nao1215/mimixbox/internal/applets/shellutils/chroot"
"github.com/nao1215/mimixbox/internal/applets/shellutils/dirname"
"github.com/nao1215/mimixbox/internal/applets/shellutils/echo"
"github.com/nao1215/mimixbox/internal/applets/shellutils/false"
"github.com/nao1215/mimixbox/internal/applets/shellutils/ghrdc"
"github.com/nao1215/mimixbox/internal/applets/shellutils/groups"
"github.com/nao1215/mimixbox/internal/applets/shellutils/hostid"
"github.com/nao1215/mimixbox/internal/applets/shellutils/id"
"github.com/nao1215/mimixbox/internal/applets/shellutils/kill"
"github.com/nao1215/mimixbox/internal/applets/shellutils/mbsh"
"github.com/nao1215/mimixbox/internal/applets/shellutils/path"
"github.com/nao1215/mimixbox/internal/applets/shellutils/printenv"
"github.com/nao1215/mimixbox/internal/applets/shellutils/pwd"
"github.com/nao1215/mimixbox/internal/applets/shellutils/sddf"
"github.com/nao1215/mimixbox/internal/applets/shellutils/seq"
"github.com/nao1215/mimixbox/internal/applets/shellutils/serial"
"github.com/nao1215/mimixbox/internal/applets/shellutils/sleep"
"github.com/nao1215/mimixbox/internal/applets/shellutils/sync"
"github.com/nao1215/mimixbox/internal/applets/shellutils/true"
"github.com/nao1215/mimixbox/internal/applets/shellutils/uuidgen"
"github.com/nao1215/mimixbox/internal/applets/shellutils/wget"
"github.com/nao1215/mimixbox/internal/applets/shellutils/whoami"
"github.com/nao1215/mimixbox/internal/applets/textutils/cat"
"github.com/nao1215/mimixbox/internal/applets/textutils/dos2unix"
"github.com/nao1215/mimixbox/internal/applets/textutils/expand"
"github.com/nao1215/mimixbox/internal/applets/textutils/head"
"github.com/nao1215/mimixbox/internal/applets/textutils/md5sum"
"github.com/nao1215/mimixbox/internal/applets/textutils/nl"
"github.com/nao1215/mimixbox/internal/applets/textutils/sha1sum"
"github.com/nao1215/mimixbox/internal/applets/textutils/sha256sum"
"github.com/nao1215/mimixbox/internal/applets/textutils/sha512sum"
"github.com/nao1215/mimixbox/internal/applets/textutils/tac"
"github.com/nao1215/mimixbox/internal/applets/textutils/tail"
"github.com/nao1215/mimixbox/internal/applets/textutils/tr"
"github.com/nao1215/mimixbox/internal/applets/textutils/unexpand"
"github.com/nao1215/mimixbox/internal/applets/textutils/unix2dos"
"github.com/nao1215/mimixbox/internal/applets/textutils/wc"
)
type EntryPoint func() (int, error)
type Applet struct {
Ep EntryPoint
Desc string
}
var Applets map[string]Applet
func init() {
Applets = map[string]Applet{
"add-shell": {addShell.Run, "Add shell name to /etc/shells"},
"base64": {base64.Run, "Base64 encode/decode from FILR(or STDIN) to STDOUT"},
"basename": {basename.Run, "Print basename (PATH without\"/\") from file path"},
"cat": {cat.Run, "Concatenate files and print on the standard output"},
"cowsay": {cowsay.Run, "Print message with cow's ASCII art"},
"chgrp": {chgrp.Run, "Change the group of each FILE to GROUP"},
"chown": {chown.Run, "Change the owner and/or group of each FILE to OWNER and/or GROUP"},
"chroot": {chroot.Run, "Run command or interactive shell with special root directory"},
//"chsh": {chsh.Run, "Cqhange login shell"},
"clear": {clear.Run, "Clear terminal"},
"cp": {cp.Run, "Copy file(s) otr Directory(s)"},
"dirname": {dirname.Run, "Print only directory path"},
"dos2unix": {dos2unix.Run, "Change CRLF to LF"},
"echo": {echo.Run, "Display a line of text"},
"expand": {expand.Run, "Convert TAB to N space (default:N=8)"},
"fakemovie": {fakemovie.Run, "Adds a video playback button to the image"},
"false": {false.Run, "Do nothing. Return unsuccess(1)"},
"ghrdc": {ghrdc.Run, "GitHub Relase Download Counter"},
"groups": {groups.Run, "Print the groups to which USERNAME belongs"},
"gzip": {gzipCmd.Run, "Compress or uncompress FILEs (by default, compress FILES in-place)"},
"halt": {halt.Run, "Halt the system"},
"head": {head.Run, "Print the first NUMBER(default=10) lines"},
"hostid": {hostid.Run, "Print hostid (Host Identity Number, hex)!!!Does not work properly!!!"},
"id": {id.Run, "Print User ID and Group ID"},
"ischroot": {ischroot.Run, "Detect if running in a chroot"},
"kill": {kill.Run, "Kill process or send signal to process"},
"lifegame": {lifegame.Run, "Life game (Conway's Game of Life)"},
"ln": {ln.Run, "Create hard or symbolic link"},
"mbsh": {mbsh.Run, "Mimix Box Shell"},
"md5sum": {md5sum.Run, "Calculate or Check md5sum message digest"},
"mkdir": {mkdir.Run, "Make directories"},
"mkfifo": {mkfifo.Run, "Make FIFO (named pipe)"},
"mv": {mv.Run, "Rename SOURCE to DESTINATION, or move SOURCE(s) to DIRECTORY"},
"nl": {nl.Run, "Write each FILE to standard output with line numbers added"},
"path": {path.Run, "Manipulate filename path"},
"poweroff": {halt.Run, "Power off the system"},
"printenv": {printenv.Run, "Print environment variable"},
"pwd": {pwd.Run, "Print Working Directory"},
"remove-shell": {removeShell.Run, "Remove shell name from /etc/shells"},
"reboot": {halt.Run, "Reboot the system"},
"reset": {reset.Run, "Reset terminal"},
"rm": {rm.Run, "Remove file(s) or directory(s)"},
"rmdir": {rmdir.Run, "Remove directory"},
"sddf": {sddf.Run, "Search & Delete Duplicated File"},
"serial": {serial.Run, "Rename the file to the name with a serial number"},
"sha1sum": {sha1sum.Run, "alculate or Check sercure hash 1 algorithm"},
"sha256sum": {sha256sum.Run, "alculate or Check sercure hash 256 algorithm"},
"sha512sum": {sha512sum.Run, "alculate or Check sercure hash 512 algorithm"},
"seq": {seq.Run, "Print a column of numbers"},
"sl": {sl.Run, "Cure your bad habit of mistyping"},
"sleep": {sleep.Run, "Pause for NUMBER seconds(minutes, hours, days)"},
"sync": {sync.Run, "Synchronize cached writes to persistent storage"},
"tac": {tac.Run, "Print the file contents from the end to the beginning"},
"tail": {tail.Run, "Print the last NUMBER(default=10) lines"},
"touch": {touch.Run, "Update the access and modification times of each FILE to the current time"},
"tr": {tr.Run, "Translate or delete characters"},
"true": {true.Run, "Do nothing. Return success(0)"},
"unexpand": {unexpand.Run, "Convert N space to TAB(default:N=8)"},
"unix2dos": {unix2dos.Run, "Change LF to CRLF"},
"uuidgen": {uuidgen.Run, "Print UUID (Universal Unique IDentifier"},
"valid-shell": {validShell.Run, "Verify if /etc/shells is valid"},
"wc": {wc.Run, "Word Count"},
"wget": {wget.Run, "The non-interactive network downloader"},
"which": {which.Run, "Returns the file path which would be executed in the current environment"},
"whoami": {whoami.Run, "Print login user name"},
}
}
func HasApplet(target string) bool {
_, ok := Applets[target]
return ok
}
func ListApplets() {
format := "%" + strconv.Itoa(longestAppletLength()) + "s - %s\n"
for _, key := range SortApplet() {
fmt.Fprintf(os.Stdout, format, key, Applets[key].Desc)
}
}
func ShowAppletsBySpaceSeparated() {
var app string
for _, key := range SortApplet() {
app += key
app += " "
}
doc.ToText(os.Stdout, app, "", "", 60)
}
func SortApplet() []string {
var keys []string
for applet := range Applets {
keys = append(keys, applet)
}
sort.Strings(keys)
return keys
}
func longestAppletLength() int {
var max int = 0
for _, key := range SortApplet() {
if max < len(key) {
max = len(key)
}
}
return max
}