Skip to content

Commit

Permalink
Compute bucket and mountpoint for cobra command
Browse files Browse the repository at this point in the history
This adds the logic to compute the bucket and mountpoint for cobra root
command.
  • Loading branch information
kislaykishore committed Jul 6, 2024
1 parent f2107c6 commit 683f825
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
12 changes: 6 additions & 6 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,19 @@ func mountWithArgs(
return
}

func populateArgs(c *cli.Context) (
func populateArgs(args []string) (
bucketName string,
mountPoint string,
err error) {
// Extract arguments.
switch len(c.Args()) {
switch len(args) {
case 1:
bucketName = ""
mountPoint = c.Args()[0]
mountPoint = args[0]

case 2:
bucketName = c.Args()[0]
mountPoint = c.Args()[1]
bucketName = args[0]
mountPoint = args[1]

default:
err = fmt.Errorf(
Expand Down Expand Up @@ -284,7 +284,7 @@ func runCLIApp(c *cli.Context) (err error) {

var bucketName string
var mountPoint string
bucketName, mountPoint, err = populateArgs(c)
bucketName, mountPoint, err = populateArgs(c.Args())
if err != nil {
return
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// NewRootCmd accepts the mountFn that it executes with the parsed configuration
func NewRootCmd(mountFn func(config cfg.Config) error) (*cobra.Command, error) {
func NewRootCmd(mountFn func(*cfg.Config, string, string) error) (*cobra.Command, error) {
var (
configObj cfg.Config
cfgFile string
Expand All @@ -45,7 +45,11 @@ of Cloud Storage FUSE, see https://cloud.google.com/storage/docs/gcs-fuse.`,
if cfgErr != nil {
return fmt.Errorf("error while parsing config: %w", cfgErr)
}
return mountFn(configObj)
if bucket, mountpoint, err := populateArgs(args[1:]); err != nil {
return fmt.Errorf("error occurred while extracting the bucket and mountpoint: %w", err)
} else {
return mountFn(&configObj, bucket, mountpoint)
}
},
}
initConfig := func() {
Expand Down
72 changes: 67 additions & 5 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"os"
"path"
"testing"

"github.com/googlecloudplatform/gcsfuse/v2/cfg"
Expand All @@ -24,7 +26,7 @@ import (
)

func TestInvalidConfig(t *testing.T) {
cmd, err := NewRootCmd(func(config cfg.Config) error { return nil })
cmd, err := NewRootCmd(func(*cfg.Config, string, string) error { return nil })
if err != nil {
t.Fatalf("Error while creating the root command: %v", err)
}
Expand All @@ -39,7 +41,7 @@ func TestInvalidConfig(t *testing.T) {
}

func TestValidConfig(t *testing.T) {
cmd, err := NewRootCmd(func(config cfg.Config) error { return nil })
cmd, err := NewRootCmd(func(*cfg.Config, string, string) error { return nil })
if err != nil {
t.Fatalf("Error while creating the root command: %v", err)
}
Expand All @@ -49,8 +51,8 @@ func TestValidConfig(t *testing.T) {
}

func TestDefaultMaxParallelDownloads(t *testing.T) {
var actual cfg.Config
cmd, err := NewRootCmd(func(c cfg.Config) error {
var actual *cfg.Config
cmd, err := NewRootCmd(func(c *cfg.Config, _, _ string) error {
actual = c
return nil
})
Expand Down Expand Up @@ -92,7 +94,7 @@ func TestCobraArgsNumInRange(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cmd, err := NewRootCmd(func(config cfg.Config) error { return nil })
cmd, err := NewRootCmd(func(*cfg.Config, string, string) error { return nil })
require.Nil(t, err)
cmd.SetArgs(tc.args)

Expand All @@ -106,3 +108,63 @@ func TestCobraArgsNumInRange(t *testing.T) {
})
}
}

func TestArgsParsing(t *testing.T) {
wd, err := os.Getwd()
require.Nil(t, err)
hd, err := os.UserHomeDir()
require.Nil(t, err)
tests := []struct {
name string
args []string
expectedBucket string
expectedMountpoint string
}{
{
name: "Both bucket and mountpoint specified.",
args: []string{"gcsfuse", "abc", "pqr"},
expectedBucket: "abc",
expectedMountpoint: path.Join(wd, "pqr"),
},
{
name: "Only mountpoint specified",
args: []string{"gcsfuse", "pqr"},
expectedBucket: "",
expectedMountpoint: path.Join(wd, "pqr"),
},
{
name: "Absolute path for mountpoint specified",
args: []string{"gcsfuse", "/pqr"},
expectedBucket: "",
expectedMountpoint: "/pqr",
},
{
name: "Relative path from user's home specified as mountpoint",
args: []string{"gcsfuse", "~/pqr"},
expectedBucket: "",
expectedMountpoint: path.Join(hd, "pqr"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var bucketName, mountPoint string
cmd, err := NewRootCmd(func(_ *cfg.Config, b string, m string) error {
bucketName = b
mountPoint = m
return nil
})
require.Nil(t, err)
cmd.SetArgs(tc.args)

err = cmd.Execute()

if assert.Nil(t, err) {
assert.Equal(t, tc.expectedBucket, bucketName)
if assert.Nil(t, err) {
assert.Equal(t, tc.expectedMountpoint, mountPoint)
}
}
})
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
if strings.ToLower(os.Getenv("ENABLE_GCSFUSE_VIPER_CONFIG")) == "true" {
// TODO: implement the mount logic instead of simply returning nil.
rootCmd, err := cmd.NewRootCmd(func(config cfg.Config) error { return nil })
rootCmd, err := cmd.NewRootCmd(func(*cfg.Config, string, string) error { return nil })
if err != nil {
log.Fatalf("Error occurred while creating the root command: %v", err)
}
Expand Down

0 comments on commit 683f825

Please sign in to comment.