Skip to content

Commit 3c0e8bf

Browse files
authored
make directory flag required (#137)
* make directory flag required Signed-off-by: sivchari <shibuuuu5@gmail.com> * handle CustomFileSystemFunc and add test cases Signed-off-by: sivchari <shibuuuu5@gmail.com> --------- Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent b3763be commit 3c0e8bf

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

cmd/root.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ var rootCmd = &cobra.Command{
5959
ctx = wrenchfs.WithContext(ctx, CustomFileSystemFunc())
6060
cmd.SetContext(ctx)
6161
}
62-
62+
if CustomFileSystemFunc == nil {
63+
if err := cobra.MarkFlagRequired(cmd.PersistentFlags(), flagNameDirectory); err != nil {
64+
panic(err)
65+
}
66+
}
6367
return nil
6468
},
6569
}
@@ -83,11 +87,11 @@ func init() {
8387
rootCmd.AddCommand(truncateCmd)
8488
rootCmd.AddCommand(instanceCmd)
8589

86-
rootCmd.PersistentFlags().StringVar(&project, flagNameProject, spannerProjectID(), "GCP project id (optional. if not set, will use $SPANNER_PROJECT_ID or $GOOGLE_CLOUD_PROJECT value)")
87-
rootCmd.PersistentFlags().StringVar(&instance, flagNameInstance, spannerInstanceID(), "Cloud Spanner instance name (optional. if not set, will use $SPANNER_INSTANCE_ID value)")
88-
rootCmd.PersistentFlags().StringVar(&database, flagNameDatabase, spannerDatabaseID(), "Cloud Spanner database name (optional. if not set, will use $SPANNER_DATABASE_ID value)")
89-
rootCmd.PersistentFlags().StringVar(&directory, flagNameDirectory, "", "Directory that schema file placed (required)")
90-
rootCmd.PersistentFlags().StringVar(&schemaFile, flagNameSchemaFile, "", "Name of schema file (optional. if not set, will use default 'schema.sql' file name)")
90+
rootCmd.PersistentFlags().StringVar(&project, flagNameProject, spannerProjectID(), "GCP project id. If not set, will use $SPANNER_PROJECT_ID or $GOOGLE_CLOUD_PROJECT value")
91+
rootCmd.PersistentFlags().StringVar(&instance, flagNameInstance, spannerInstanceID(), "Cloud Spanner instance name. If not set, will use $SPANNER_INSTANCE_ID value")
92+
rootCmd.PersistentFlags().StringVar(&database, flagNameDatabase, spannerDatabaseID(), "Cloud Spanner database name If not set, will use $SPANNER_DATABASE_ID value")
93+
rootCmd.PersistentFlags().StringVar(&directory, flagNameDirectory, "", "Directory that schema file placed")
94+
rootCmd.PersistentFlags().StringVar(&schemaFile, flagNameSchemaFile, "", "Name of schema file If not set, will use default 'schema.sql' file name")
9195
rootCmd.PersistentFlags().StringVar(&credentialsFile, flagCredentialsFile, "", "Specify Credentials File")
9296
rootCmd.PersistentFlags().DurationVar(&timeout, flagTimeout, time.Hour, "Context timeout")
9397

cmd/root_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func TestDirectoryFlagNilAndCustomFileSystemFuncIsNil(t *testing.T) {
10+
rootCmd.RunE = func(cmd *cobra.Command, args []string) error {
11+
return nil
12+
}
13+
14+
testcases := []struct {
15+
name string
16+
flags []string
17+
want bool
18+
}{
19+
{
20+
name: "directory flag is nil and CustomFileSystemFunc is nil",
21+
flags: []string{},
22+
want: true,
23+
},
24+
{
25+
name: "directory flag is set and CustomFileSystemFunc is nil",
26+
flags: []string{"--directory", "test"},
27+
want: false,
28+
},
29+
{
30+
name: "directory flag is nil and CustomFileSystemFunc is set",
31+
flags: []string{},
32+
want: false,
33+
},
34+
}
35+
36+
for _, tc := range testcases {
37+
t.Run(tc.name, func(t *testing.T) {
38+
rootCmd.SetArgs(tc.flags)
39+
err := rootCmd.Execute()
40+
if (err != nil) != tc.want {
41+
t.Errorf("Execute() error = %v, wantErr %v", err, tc.want)
42+
}
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)