mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-11-24 16:53:14 +08:00
Shell: Added a helper function isHelpRequest() (#7380)
* Added a helper function `isHelpRequest()` * also handles combined short flags like -lh or -hl * Created handleHelpRequest() helper function encapsulates both: Checking for help flags Printing the help message * Limit to reasonable length (2-4 chars total) to avoid matching long options like -verbose
This commit is contained in:
@@ -34,6 +34,10 @@ func (c *commandFsCat) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ func (c *commandFsCd) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsCd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsCd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ func (c *commandFsDu) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -40,6 +40,10 @@ func (c *commandFsLs) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsLs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsLs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var isLongFormat, showHidden bool
|
var isLongFormat, showHidden bool
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if !strings.HasPrefix(arg, "-") {
|
if !strings.HasPrefix(arg, "-") {
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ package shell
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
||||||
"google.golang.org/protobuf/proto"
|
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||||
)
|
)
|
||||||
@@ -37,6 +38,10 @@ func (c *commandFsMetaCat) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ func (c *commandFsMetaNotify) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsMetaNotify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsMetaNotify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ package shell
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
||||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -33,6 +34,10 @@ func (c *commandFsMkdir) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsMkdir) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsMkdir) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -40,6 +40,10 @@ func (c *commandFsMv) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsMv) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsMv) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
return fmt.Errorf("need to have 2 arguments")
|
return fmt.Errorf("need to have 2 arguments")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ func (c *commandFsPwd) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsPwd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsPwd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprintf(writer, "%s\n", commandEnv.option.Directory)
|
fmt.Fprintf(writer, "%s\n", commandEnv.option.Directory)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -39,6 +39,11 @@ func (c *commandFsRm) HasTag(CommandTag) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandFsRm) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsRm) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
isRecursive := false
|
isRecursive := false
|
||||||
ignoreRecursiveError := false
|
ignoreRecursiveError := false
|
||||||
var entries []string
|
var entries []string
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ func (c *commandFsTree) HasTag(CommandTag) bool {
|
|||||||
|
|
||||||
func (c *commandFsTree) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
func (c *commandFsTree) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
||||||
|
|
||||||
|
if handleHelpRequest(c, args, writer) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -3,13 +3,15 @@ package shell
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/operation"
|
"io"
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
||||||
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
||||||
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||||
@@ -147,6 +149,37 @@ func findInputDirectory(args []string) (input string) {
|
|||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isHelpRequest checks if the args contain a help flag (-h, --help, or -help)
|
||||||
|
// It also handles combined short flags like -lh or -hl
|
||||||
|
func isHelpRequest(args []string) bool {
|
||||||
|
for _, arg := range args {
|
||||||
|
// Check for exact matches
|
||||||
|
if arg == "-h" || arg == "--help" || arg == "-help" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Check for combined short flags (e.g., -lh, -hl, -rfh)
|
||||||
|
// Limit to reasonable length (2-4 chars total) to avoid matching long options like -verbose
|
||||||
|
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 1 && len(arg) <= 4 {
|
||||||
|
for _, char := range arg[1:] {
|
||||||
|
if char == 'h' {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleHelpRequest checks for help flags and prints the help message if requested.
|
||||||
|
// It returns true if the help message was printed, indicating the command should exit.
|
||||||
|
func handleHelpRequest(c command, args []string, writer io.Writer) bool {
|
||||||
|
if isHelpRequest(args) {
|
||||||
|
fmt.Fprintln(writer, c.Help())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func readNeedleMeta(grpcDialOption grpc.DialOption, volumeServer pb.ServerAddress, volumeId uint32, needleValue needle_map.NeedleValue) (resp *volume_server_pb.ReadNeedleMetaResponse, err error) {
|
func readNeedleMeta(grpcDialOption grpc.DialOption, volumeServer pb.ServerAddress, volumeId uint32, needleValue needle_map.NeedleValue) (resp *volume_server_pb.ReadNeedleMetaResponse, err error) {
|
||||||
err = operation.WithVolumeServerClient(false, volumeServer, grpcDialOption,
|
err = operation.WithVolumeServerClient(false, volumeServer, grpcDialOption,
|
||||||
func(client volume_server_pb.VolumeServerClient) error {
|
func(client volume_server_pb.VolumeServerClient) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user