Skip to content

Commit

Permalink
Added remove-shell command.
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Dec 17, 2021
1 parent 25480cb commit b226592
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 67 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Changelog
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.31.00] - 2021-12-17
## [0.31.1] - 2021-12-17
### Added
- add-shell command
- clear command.
- halt command. However, this version can not shutdown system (halt have the bug).
- printenv command.
- pwd command.
- remove-shell command.
- reset command.
- sync command.
### Changed
- the classification of directories under internal/applets.
- Project
- the classification of directories under internal/applets.
- mimixbox command
- Fixed a bug that the mimixbox command causes a runtime error. A runtime error occurred when args[0] is an applet name that does not exist and args[1:] contains the applet name.
## [0.30.00] - 2021-12-15
### Added
- chown command.
Expand Down
20 changes: 14 additions & 6 deletions cmd/mimixbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type options struct {

var osExit = os.Exit

const version = "0.31.0"
const version = "0.31.1"

const (
ExitSuccess int = iota // 0
Expand Down Expand Up @@ -82,21 +82,29 @@ func main() {

// If the specified command(applet) is not built in mimixbox.
if !hasAppletName() {
fmt.Fprintf(os.Stderr, "%s is not provided by mimixbox.\n\n", os.Args[0])
fmt.Fprintln(os.Stderr, "[Commands supported by MimixBox]")
applets.ShowAppletsBySpaceSeparated()
osExit(ExitFailuer)
showSupportAppletAndExitFailure(os.Args[0])
}

applet := path.Base(os.Args[0])
app := applets.Applets[applet]
app, ok := applets.Applets[applet]
if !ok {
showSupportAppletAndExitFailure(os.Args[0])
}

if status, err = app.Ep(); err != nil {
fmt.Fprintln(os.Stderr, applet+": "+err.Error())
osExit(status)
}
osExit(status)
}

func showSupportAppletAndExitFailure(userInputCmdName string) {
fmt.Fprintf(os.Stderr, "%s is not provided by mimixbox.\n\n", userInputCmdName)
fmt.Fprintln(os.Stderr, "[Commands supported by MimixBox]")
applets.ShowAppletsBySpaceSeparated()
osExit(ExitFailuer)
}

// If the mimixbox option exists, execute the processing for the option and exit.
// TODO: Rewrite this function. This method is too complicated
func handleMimixBoxOptionsIfNeeded(parser *flags.Parser, opts *options) {
Expand Down
3 changes: 2 additions & 1 deletion docs/introduction/en/CommandAppletList.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Command (Applet) List
|Command (Applet) Name | Description|
|:--|:--|
| add-shell| Write down shell name to /etc/shells |
| add-shell| Add shell name to /etc/shells |
| base64| Base64 encode/decode from FILR(or STDIN) to STDOUT|
| basename| Print basename (PATH without "/") from file path |
| cat | Concatenate files and print on the standard output|
Expand Down Expand Up @@ -35,6 +35,7 @@
| path | Manipulate filename path|
| printenv| Print environment variable|
| pwd | Print Working Directory|
| remove-shell|Remove shell name from /etc/shells|
| reset| Reset terminal|
| rm | Remove file(s) or directory(s)|
| rmdir | Remove directory|
Expand Down
116 changes: 59 additions & 57 deletions internal/applets/applet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"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"
Expand Down Expand Up @@ -93,63 +94,64 @@ 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"},
"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"},
"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"},
"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"},
"printenv": {printenv.Run, "Print environment variable"},
"pwd": {pwd.Run, "Print Working Directory"},
"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"},
"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"},
"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"},
"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"},
"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"},
"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"},
"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"},
"printenv": {printenv.Run, "Print environment variable"},
"pwd": {pwd.Run, "Print Working Directory"},
"remove-shell": {removeShell.Run, "Remove shell name from /etc/shells"},
"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"},
"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"},
"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"},
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/applets/debianutils/add-shell/add-shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func parseArgs(opts *options) ([]string, error) {
func initParser(opts *options) *flags.Parser {
parser := flags.NewParser(opts, flags.Default)
parser.Name = cmdName
parser.Usage = "[OPTIONS] SHELL"
parser.Usage = "[OPTIONS] SHELL_NAME"

return parser
}
Expand Down
114 changes: 114 additions & 0 deletions internal/applets/debianutils/remove-shell/remove-shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// mimixbox/internal/applets/debianutils/remove-shell/remove-shell.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 removeShell

import (
"fmt"
"os"

"github.com/jessevdk/go-flags"
mb "github.com/nao1215/mimixbox/internal/lib"
)

const cmdName string = "remove-shell"

const version = "1.0.0"

var osExit = os.Exit

type options struct {
Version bool `short:"v" long:"version" description:"Show remove-shell command version"`
}

// Exit code
const (
ExitSuccess int = iota // 0
ExitFailuer
)

func Run() (int, error) {
var opts options
var args []string
var err error

if args, err = parseArgs(&opts); err != nil {
return ExitFailuer, nil
}
return removeShell(args)
}

func removeShell(args []string) (int, error) {
f, err := os.OpenFile(mb.TmpShellsFile(), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return ExitFailuer, err
}
defer f.Close()

lines, err := mb.ReadFileToStrList(mb.ShellsFilePath)
if err != nil {
return ExitFailuer, err
}

lines = mb.ChopAll(lines)
for _, shell := range args {
lines = mb.Remove(lines, shell)
}
for _, v := range lines {
fmt.Fprintln(f, v)
}

err = mb.Copy(mb.TmpShellsFile(), mb.ShellsFilePath)
if err != nil {
mb.RemoveFile(mb.TmpShellsFile(), false)
return ExitFailuer, err
}

mb.RemoveFile(mb.TmpShellsFile(), false)
return ExitSuccess, nil
}

func parseArgs(opts *options) ([]string, error) {
p := initParser(opts)

args, err := p.Parse()
if err != nil {
return nil, err
}

if opts.Version {
mb.ShowVersion(cmdName, version)
osExit(ExitSuccess)
}

if !isValidArgNr(args) {
fmt.Fprintln(os.Stderr, cmdName+": shellname [shellname ...]")
osExit(ExitFailuer)
}
return args, nil
}

func initParser(opts *options) *flags.Parser {
parser := flags.NewParser(opts, flags.Default)
parser.Name = cmdName
parser.Usage = "[OPTIONS] SHELL_NAME"

return parser
}

func isValidArgNr(args []string) bool {
return len(args) >= 1
}

0 comments on commit b226592

Please sign in to comment.