Skip to content

Commit

Permalink
Changed the library that was open to the public to the internal library.
Browse files Browse the repository at this point in the history
The internal library is an mb package. I respected BusyBox's libbb.
  • Loading branch information
nao1215 committed Nov 18, 2021
1 parent 4014175 commit 7b350b8
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 48 deletions.
11 changes: 5 additions & 6 deletions cmd/mimixbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ package main
import (
"fmt"
"mimixbox/internal/applets"
check "mimixbox/internal/lib"
"mimixbox/pkg/fileutils"
mb "mimixbox/internal/lib"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -51,7 +50,7 @@ type options struct {

var osExit = os.Exit

const version = "0.3.0"
const version = "0.4.1"

const (
ExitSuccess int = iota // 0
Expand Down Expand Up @@ -225,7 +224,7 @@ func __install(mimixboxPath string, installPath string, full bool) error {
}

for applet := range applets.Applets {
if !full && check.ExistCmd(applet) {
if !full && mb.ExistCmd(applet) {
fmt.Printf("Same name command(%s) already exists. Not create symbolic link.\n", applet)
continue // if same name command already exists, not install for safety.
}
Expand All @@ -236,7 +235,7 @@ func __install(mimixboxPath string, installPath string, full bool) error {
// created by mimixbox, while the latter may be binaries
// provided by other packages.
newPath := filepath.Join(installPath, applet)
if fileutils.IsSymlink(newPath) {
if mb.IsSymlink(newPath) {
err := os.Remove(newPath) // Remove even BusyBox's symbolic link
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -274,7 +273,7 @@ func remove(installPath string) error {
for name := range applets.Applets {
symbolicPath := filepath.Join(installPath, name)

if !fileutils.IsSymlink(symbolicPath) {
if !mb.IsSymlink(symbolicPath) {
continue
}

Expand Down
31 changes: 15 additions & 16 deletions internal/applets/fileutils/mv/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ package mv
import (
"errors"
"fmt"
utils "mimixbox/internal/lib"
"mimixbox/pkg/fileutils"
mb "mimixbox/internal/lib"
"os"
"path/filepath"

Expand Down Expand Up @@ -98,7 +97,7 @@ func validArgs(srcPaths []string, destPath string, opts options) error {

func move(srcPaths []string, dest string, opts options) error {
for _, src := range srcPaths {
if !fileutils.Exists(src) {
if !mb.Exists(src) {
return errors.New(src + " doesn't exist")
}

Expand Down Expand Up @@ -140,7 +139,7 @@ func noclobberMove(src string, dest string) error {
if isSameNameFileOrDir(src, dest) {
return nil // Nothing to do. Say nothing.
}
if fileutils.IsFile(src) && fileutils.IsFile(dest) {
if mb.IsFile(src) && mb.IsFile(dest) {
if filepath.Base(src) == filepath.Base(dest) {
return nil // Nothing to do. Say nothing.
}
Expand All @@ -152,18 +151,18 @@ func noclobberMove(src string, dest string) error {
}

func isSameNameFileOrDir(src string, dest string) bool {
if fileutils.IsDir(src) && fileutils.IsDir(dest) {
if mb.IsDir(src) && mb.IsDir(dest) {
if filepath.Base(src) == filepath.Base(dest) {
return true
}
}
if fileutils.IsFile(src) && fileutils.IsFile(dest) {
if mb.IsFile(src) && mb.IsFile(dest) {
if filepath.Base(src) == filepath.Base(dest) {
return true
}
} else if fileutils.IsFile(src) && fileutils.IsDir(dest) {
} else if mb.IsFile(src) && mb.IsDir(dest) {
destPath := filepath.Join(dest, filepath.Base(src))
if fileutils.Exists(destPath) {
if mb.Exists(destPath) {
return true
}
}
Expand All @@ -180,7 +179,7 @@ func forceMove(src string, dest string, opts options) error {

func interactiveMove(src string, dest string, opts options) error {
if isSameNameFileOrDir(src, dest) {
if !utils.Question("Overwrite " + filepath.Base(src)) {
if !mb.Question("Overwrite " + filepath.Base(src)) {
return nil
}
}
Expand All @@ -195,15 +194,15 @@ func interactiveMove(src string, dest string, opts options) error {

func decideDestAbsPath(src string, dest string, opts options) string {
destPath := dest
if fileutils.IsDir(src) && fileutils.IsDir(destPath) {
if mb.IsDir(src) && mb.IsDir(destPath) {
if (filepath.Base(src) == filepath.Base(destPath)) && opts.Backup {
destPath = decideBackupFileName(destPath)
}
} else if fileutils.IsFile(src) && fileutils.IsFile(dest) && opts.Backup {
} else if mb.IsFile(src) && mb.IsFile(dest) && opts.Backup {
destPath = decideBackupFileName(destPath)
} else if fileutils.IsFile(src) && fileutils.IsDir(dest) {
} else if mb.IsFile(src) && mb.IsDir(dest) {
destPath = filepath.Join(dest, filepath.Base(src))
if fileutils.IsFile(destPath) && opts.Backup {
if mb.IsFile(destPath) && opts.Backup {
destPath = decideBackupFileName(destPath)
}
}
Expand All @@ -212,10 +211,10 @@ func decideDestAbsPath(src string, dest string, opts options) string {

func decideBackupFileName(path string) string {
var backupPath string
if fileutils.Exists(path) {
backupPath = path + utils.SimpleBackupSuffix()
if mb.Exists(path) {
backupPath = path + mb.SimpleBackupSuffix()
}
if fileutils.Exists(backupPath) {
if mb.Exists(backupPath) {
return decideBackupFileName(backupPath)
}
return backupPath
Expand Down
21 changes: 10 additions & 11 deletions internal/applets/fileutils/rm/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ package rm
import (
"errors"
"fmt"
utils "mimixbox/internal/lib"
"mimixbox/pkg/fileutils"
mb "mimixbox/internal/lib"
"os"
"sort"

Expand Down Expand Up @@ -57,7 +56,7 @@ func Run() (int, error) {
return ExitFailuer, nil
}

// Coreutils will continue to delete files as much as possible.
// Coremb will continue to delete files as much as possible.
// MimixBox stops processing if an error occurs even once.
for _, path := range args {
if status, err := rm(path, opts); err != nil {
Expand All @@ -73,8 +72,8 @@ func rm(path string, opts options) (int, error) {
return status, err
}

if fileutils.IsFile(path) {
if opts.Interactive && !utils.Question("Remove "+path+"?") {
if mb.IsFile(path) {
if opts.Interactive && !mb.Question("Remove "+path+"?") {
return ExitSuccess, nil // Skip this file
}
if err := os.Remove(path); err != nil {
Expand Down Expand Up @@ -104,7 +103,7 @@ func removeDir(dir string, interactive bool) error {
}

func interactiveRemoveDir(dir string) error {
dirs, files, err := fileutils.Walk(dir)
dirs, files, err := mb.Walk(dir)
if err != nil {
return err
}
Expand All @@ -114,7 +113,7 @@ func interactiveRemoveDir(dir string) error {
sort.Sort(sort.Reverse(sort.StringSlice(files)))

for _, file := range files {
if !utils.Question("Remove " + file + "?") {
if !mb.Question("Remove " + file + "?") {
continue
}
err := os.Remove(file)
Expand All @@ -123,7 +122,7 @@ func interactiveRemoveDir(dir string) error {
}
}
for _, dir := range dirs {
if !utils.Question("Remove " + dir + "?") {
if !mb.Question("Remove " + dir + "?") {
continue
}
err := os.Remove(dir)
Expand All @@ -135,18 +134,18 @@ func interactiveRemoveDir(dir string) error {
}

func validBeforeRemove(path string, opts options) (int, error) {
if utils.IsRootDir(path) && !opts.NoPreserve {
if mb.IsRootDir(path) && !opts.NoPreserve {
return ExitFailuer, errors.New("do not remove the root directory")
}

if !fileutils.Exists(path) {
if !mb.Exists(path) {
if !opts.Force {
return ExitFailuer, errors.New("can't remove " + path + ": No such file or directory exists")
}
return ExitFailuer, nil
}

if fileutils.IsDir(path) && !opts.Recursive {
if mb.IsDir(path) && !opts.Recursive {
return ExitFailuer, errors.New("can't remove " + path + ": It's directory")
}

Expand Down
4 changes: 2 additions & 2 deletions internal/applets/fileutils/touch/touch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package touch

import (
"fmt"
"mimixbox/pkg/fileutils"
mb "mimixbox/internal/lib"
"os"
"time"

Expand Down Expand Up @@ -63,7 +63,7 @@ func Run() (int, error) {
// ctime = change time
// mtime = modify time
func touch(file string, opts options) error {
if !fileutils.Exists(file) && !opts.NoCreate {
if !mb.Exists(file) && !opts.NoCreate {
file, err := os.Create(file)
if err != nil {
return err
Expand Down
15 changes: 7 additions & 8 deletions internal/applets/shellutils/serial/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package serial
import (
"fmt"
"io/ioutil"
mb "mimixbox/internal/lib"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

"mimixbox/pkg/fileutils"

"github.com/jessevdk/go-flags"
)

Expand Down Expand Up @@ -57,7 +56,7 @@ func Run() (int, error) {
var args = parseArgs(&opts)
var dirPath = args[0]

if !fileutils.Exists(dirPath) {
if !mb.Exists(dirPath) {
err := fmt.Errorf("%s doesn't exist.", dirPath)
return ExitFailuer, err
}
Expand Down Expand Up @@ -124,7 +123,7 @@ func copy(newFileNames map[string]string, dryRun bool) {
// If this function is running, it will force the file to be overwritten.
// If there is the file with the same name in the copy destination,
// delete it before copy the file.
if fileutils.Exists(dest) {
if mb.Exists(dest) {
if err := os.Remove(dest); err != nil {
fmt.Fprintf(os.Stderr, "Can't copy %s to %s\n", org, dest)
osExit(ExitFailuer)
Expand Down Expand Up @@ -202,7 +201,7 @@ func getFilePathsInDir(dir string) []string {
var paths []string
for _, file := range files {
path = filepath.Join(dir, file.Name())
if fileutils.IsFile(path) && !fileutils.IsHiddenFile(path) {
if mb.IsFile(path) && !mb.IsHiddenFile(path) {
paths = append(paths, filepath.Clean(path))
}
}
Expand All @@ -221,7 +220,7 @@ func newNames(opts options, path []string) map[string]string {
ext := filepath.Ext(file)

if len(opts.Name) == 0 {
format = fileNameFormat(opts.Prefix, opts.Suffix, fileutils.BaseNameWithoutExt(file), len(path))
format = fileNameFormat(opts.Prefix, opts.Suffix, mb.BaseNameWithoutExt(file), len(path))
} else {
format = fileNameFormat(opts.Prefix, opts.Suffix, opts.Name, len(path))
}
Expand Down Expand Up @@ -260,7 +259,7 @@ func dieIfExistSameNameFile(force bool, fileNames map[string]string) {
}

for _, file := range fileNames {
if fileutils.Exists(file) {
if mb.Exists(file) {
fmt.Fprintf(os.Stderr, "%s (file name which is after renaming) is already exists.\n", file)
fmt.Fprintf(os.Stderr, "Renaming may erase the contents of the file. ")
fmt.Fprintf(os.Stderr, "So, nothing to do.\n")
Expand All @@ -272,7 +271,7 @@ func dieIfExistSameNameFile(force bool, fileNames map[string]string) {
func makeDirIfNeeded(filePath string) {
dirPath := filepath.Dir(filePath)

if fileutils.Exists(dirPath) {
if mb.Exists(dirPath) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/fileutils/file.go → internal/lib/file.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// mimixbox/pkg/fileutils/file.go
// mimixbox/internal/lib/file.go
//
// Copyright 2021 Naohiro CHIKAMATSU
//
Expand All @@ -14,7 +14,7 @@
// 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 fileutils
package mb

import (
"io/fs"
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/gnu.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// 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 utils
package mb

import "os"

Expand Down
4 changes: 2 additions & 2 deletions internal/lib/check.go → internal/lib/shell.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// mimixbox/internal/lib/check.go
// mimixbox/internal/lib/shell.go
//
// Copyright 2021 Naohiro CHIKAMATSU
//
Expand All @@ -14,7 +14,7 @@
// 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 utils
package mb

import (
"fmt"
Expand Down

0 comments on commit 7b350b8

Please sign in to comment.