Skip to content

Commit

Permalink
cmd: print usage if not enough args (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandyXSD authored Feb 28, 2022
1 parent 53ada6c commit 632953a
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 107 deletions.
6 changes: 1 addition & 5 deletions cmd/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,11 @@ func (bm *benchmark) printResult(result [][3]string) {
}

func bench(ctx *cli.Context) error {
setLoggerLevel(ctx)

setup(ctx, 1)
/* --- Pre-check --- */
if ctx.Uint("block-size") == 0 || ctx.Uint("threads") == 0 {
return os.ErrInvalid
}
if ctx.NArg() < 1 {
logger.Fatalln("PATH must be provided")
}
tmpdir, err := filepath.Abs(ctx.Args().First())
if err != nil {
logger.Fatalf("Failed to get absolute path of %s: %s", ctx.Args().First(), err)
Expand Down
5 changes: 1 addition & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ func userConfirmed() bool {
}

func config(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
return fmt.Errorf("META-URL is needed")
}
setup(ctx, 1)
removePassword(ctx.Args().Get(0))
m := meta.NewClient(ctx.Args().Get(0), &meta.Config{Retries: 10, Strict: true})

Expand Down
5 changes: 1 addition & 4 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ func printSessions(ss [][3]string) string {
}

func destroy(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 2 {
return fmt.Errorf("META-URL and UUID are required")
}
setup(ctx, 2)
uri := ctx.Args().Get(0)
if !strings.Contains(uri, "://") {
uri = "redis://" + uri
Expand Down
6 changes: 1 addition & 5 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"fmt"
"io"
"os"

Expand Down Expand Up @@ -54,10 +53,7 @@ Details: https://juicefs.com/docs/community/metadata_dump_load`,
}

func dump(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
return fmt.Errorf("META-URL is needed")
}
setup(ctx, 1)
var fp io.WriteCloser
if ctx.Args().Len() == 1 {
fp = os.Stdout
Expand Down
9 changes: 1 addition & 8 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,9 @@ func test(store object.ObjectStorage) error {
}

func format(c *cli.Context) error {
setLoggerLevel(c)
if c.Args().Len() < 1 {
logger.Fatalf("Meta URL and name are required")
}
setup(c, 2)
removePassword(c.Args().Get(0))
m := meta.NewClient(c.Args().Get(0), &meta.Config{Retries: 2})

if c.Args().Len() < 2 {
logger.Fatalf("Please give it a name")
}
name := c.Args().Get(1)
validName := regexp.MustCompile(`^[a-z0-9][a-z0-9\-]{1,61}[a-z0-9]$`)
if !validName.MatchString(name) {
Expand Down
5 changes: 1 addition & 4 deletions cmd/fsck.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ $ juicefs fsck redis://localhost`,
}

func fsck(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
return fmt.Errorf("META-URL is needed")
}
setup(ctx, 1)
removePassword(ctx.Args().Get(0))
m := meta.NewClient(ctx.Args().Get(0), &meta.Config{Retries: 10, Strict: true})
format, err := m.Load(true)
Expand Down
7 changes: 1 addition & 6 deletions cmd/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ Details: https://juicefs.com/docs/community/s3_gateway`,
}

func gateway(c *cli.Context) error {
setLoggerLevel(c)

if c.Args().Len() < 2 {
logger.Fatalf("Meta URL and listen address are required")
}

setup(c, 2)
ak := os.Getenv("MINIO_ROOT_USER")
if ak == "" {
ak = os.Getenv("MINIO_ACCESS_KEY")
Expand Down
6 changes: 1 addition & 5 deletions cmd/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"fmt"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -80,10 +79,7 @@ type dChunk struct {
}

func gc(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
return fmt.Errorf("META-URL is needed")
}
setup(ctx, 1)
removePassword(ctx.Args().Get(0))
m := meta.NewClient(ctx.Args().Get(0), &meta.Config{
Retries: 10,
Expand Down
5 changes: 1 addition & 4 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,11 @@ $ juicefs info -i 100`,
}

func info(ctx *cli.Context) error {
setup(ctx, 1)
if runtime.GOOS == "windows" {
logger.Infof("Windows is not supported")
return nil
}
if ctx.Args().Len() < 1 {
logger.Infof("DIR or FILE is needed")
return nil
}
var recursive uint8
if ctx.Bool("recursive") {
recursive = 1
Expand Down
6 changes: 1 addition & 5 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"fmt"
"io"
"os"

Expand Down Expand Up @@ -45,10 +44,7 @@ Details: https://juicefs.com/docs/community/metadata_dump_load`,
}

func load(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
return fmt.Errorf("META-URL is needed")
}
setup(ctx, 1)
var fp io.ReadCloser
if ctx.Args().Len() == 1 {
fp = os.Stdin
Expand Down
34 changes: 19 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,14 @@ func reorderOptions(app *cli.App, args []string) []string {
return append(newArgs, others...)
}

func setupAgent(c *cli.Context) {
if !c.Bool("no-agent") {
go func() {
for port := 6060; port < 6100; port++ {
_ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), nil)
}
}()
go func() {
for port := 6070; port < 6100; port++ {
_ = agent.Listen(agent.Options{Addr: fmt.Sprintf("127.0.0.1:%d", port)})
}
}()
// Check number of positional arguments, set logger level and setup agent if needed
func setup(c *cli.Context, n int) {
if c.NArg() < n {
logger.Errorf("This command requires at lease %d arguments", n)
fmt.Printf("USAGE:\n juicefs %s [command options] %s\n", c.Command.Name, c.Command.ArgsUsage)
os.Exit(1)
}
}

func setLoggerLevel(c *cli.Context) {
if c.Bool("trace") {
utils.SetLogLevel(logrus.TraceLevel)
} else if c.Bool("verbose") {
Expand All @@ -264,7 +256,19 @@ func setLoggerLevel(c *cli.Context) {
if c.Bool("no-color") {
utils.DisableLogColor()
}
setupAgent(c)

if !c.Bool("no-agent") {
go func() {
for port := 6060; port < 6100; port++ {
_ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), nil)
}
}()
go func() {
for port := 6070; port < 6100; port++ {
_ = agent.Listen(agent.Options{Addr: fmt.Sprintf("127.0.0.1:%d", port)})
}
}()
}
}

func removePassword(uri string) {
Expand Down
8 changes: 1 addition & 7 deletions cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,8 @@ func initBackgroundTasks(c *cli.Context, vfsConf *vfs.Config, metaConf *meta.Con
}

func mount(c *cli.Context) error {
setLoggerLevel(c)
if c.Args().Len() < 1 {
logger.Fatalf("Meta URL and mountpoint are required")
}
setup(c, 2)
addr := c.Args().Get(0)
if c.Args().Len() < 2 {
logger.Fatalf("MOUNTPOINT is required")
}
mp := c.Args().Get(1)

prepareMp(mp)
Expand Down
5 changes: 1 addition & 4 deletions cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,7 @@ func (p *profiler) flusher() {
}

func profile(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
logger.Fatalln("Mount point or log file must be provided!")
}
setup(ctx, 1)
logPath := ctx.Args().First()
st, err := os.Stat(logPath)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions cmd/rmr.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ func openController(path string) *os.File {
}

func rmr(ctx *cli.Context) error {
setup(ctx, 1)
if runtime.GOOS == "windows" {
logger.Infof("Windows is not supported")
return nil
}
if ctx.Args().Len() < 1 {
logger.Infof("PATH is needed")
return nil
}
for i := 0; i < ctx.Args().Len(); i++ {
path := ctx.Args().Get(i)
p, err := filepath.Abs(path)
Expand Down
5 changes: 1 addition & 4 deletions cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,7 @@ func readStats(path string) map[string]float64 {
}

func stats(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
logger.Fatalln("mount point must be provided")
}
setup(ctx, 1)
mp := ctx.Args().First()
inode, err := utils.GetFileInode(mp)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ func printJson(v interface{}) {
}

func status(ctx *cli.Context) error {
setLoggerLevel(ctx)
if ctx.Args().Len() < 1 {
return fmt.Errorf("META-URL is needed")
}
setup(ctx, 1)
removePassword(ctx.Args().Get(0))
m := meta.NewClient(ctx.Args().Get(0), &meta.Config{Retries: 10, Strict: true})
format, err := m.Load(true)
Expand Down
11 changes: 2 additions & 9 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func cmdSync() *cli.Command {
ArgsUsage: "SRC DST",
Description: `
This tool spawns multiple threads to concurrently syncs objects of two data storages.
SRC and DST should be [NAME://][ACCESS_KEY:SECRET_KEY@]BUCKET[.ENDPOINT][/PREFIX].
Examples:
# Sync object from OSS to S3
Expand Down Expand Up @@ -278,16 +279,8 @@ func isS3PathType(endpoint string) bool {
return regexp.MustCompile(pattern).MatchString(endpoint)
}

const USAGE = `juicefs [options] sync [options] SRC DST
SRC and DST should be [NAME://][ACCESS_KEY:SECRET_KEY@]BUCKET[.ENDPOINT][/PREFIX]`

func doSync(c *cli.Context) error {
setLoggerLevel(c)

if c.Args().Len() != 2 {
logger.Errorf(USAGE)
return nil
}
setup(c, 2)
config := sync.NewConfigFromCli(c)
go func() { _ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", config.HTTPPort), nil) }()

Expand Down
4 changes: 1 addition & 3 deletions cmd/umount.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func doUmount(mp string, force bool) error {
}

func umount(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return fmt.Errorf("MOUNTPOINT is needed")
}
setup(ctx, 1)
mp := ctx.Args().Get(0)
force := ctx.Bool("force")
return doUmount(mp, force)
Expand Down
1 change: 1 addition & 0 deletions cmd/warmup.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func sendCommand(cf *os.File, batch []string, count int, threads uint, backgroun
}

func warmup(ctx *cli.Context) error {
setup(ctx, 0)
fname := ctx.String("file")
paths := ctx.Args().Slice()
if fname != "" {
Expand Down
8 changes: 1 addition & 7 deletions cmd/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,8 @@ $ juicefs webdav redis://localhost localhost:9007`,
}

func webdav(c *cli.Context) error {
setLoggerLevel(c)
if c.Args().Len() < 1 {
logger.Fatalf("meta url are required")
}
setup(c, 2)
metaUrl := c.Args().Get(0)
if c.Args().Len() < 2 {
logger.Fatalf("listen address is required")
}
listenAddr := c.Args().Get(1)
m, store, conf := initForSvc(c, "webdav", metaUrl)
jfs, err := fs.NewFileSystem(conf, m, store)
Expand Down

0 comments on commit 632953a

Please sign in to comment.