Skip to content

Commit

Permalink
fix : change ReadConfig name to Preload & fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu22e committed Sep 16, 2023
1 parent fbdbf2a commit 2dea9fd
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 22 deletions.
20 changes: 9 additions & 11 deletions cmd/yorkie/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,24 @@ func Delete() error {
return nil
}

// ReadConfig read configuration file for viper before runnning command
func ReadConfig(cmd *cobra.Command, args []string) error {
// Preload read configuration file for viper before running command
func Preload(cmd *cobra.Command, args []string) error {
configPathValue, err := configPath()
if err != nil {
fmt.Fprintln(os.Stderr, "get config path: %w", err)
os.Exit(1)
}

file, err := os.Open(filepath.Clean(configPathValue))
if err != nil {
if _, err := os.Stat(filepath.Clean(configPathValue)); err != nil {
if os.IsNotExist(err) {
New()
return nil
if saveErr := Save(New()); saveErr != nil {
return fmt.Errorf("save default config: %w", saveErr)
}
} else {
return fmt.Errorf("check config file: %w", err)
}

return fmt.Errorf("open config file: %w", err)
}
defer func() {
_ = file.Close()
}()

if err := viper.ReadInConfig(); err != nil {
return fmt.Errorf("failed to read in config: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newListCommand() *cobra.Command {
return &cobra.Command{
Use: "ls",
Short: "List all contexts from configuration",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Load()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/context/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newRemoveCmd() *cobra.Command {
Use: "remove [rpcAddr]",
Short: "Remove the context for the specified rpcAddr",
Args: cobra.ExactArgs(1),
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Load()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/context/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newSetContextCmd() *cobra.Command {
return &cobra.Command{
Use: "set",
Short: "Set the current global flags as the context",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Load()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/document/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newListCommand() *cobra.Command {
return &cobra.Command{
Use: "ls [project name]",
Short: "List all documents in the project",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("project is required")
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/document/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newRemoveCommand() *cobra.Command {
Use: "remove [project name] [document key]",
Short: "Remove documents in the project",
Example: "yorkie document remove sample-project sample-document [options]",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("project name and document key are required")
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func newHistoryCmd() *cobra.Command {
return &cobra.Command{
Use: "history [project name] [document key]",
Short: "Show the history of a document",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("project name and document key are required")
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newLoginCmd() *cobra.Command {
return &cobra.Command{
Use: "login",
Short: "Log in to the Yorkie server",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
cli, err := admin.Dial(viper.GetString("rpcAddr"), admin.WithInsecure(viper.GetBool("isInsecure")))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newLogoutCmd() *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Log out from the Yorkie server",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Load()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newCreateCommand() *cobra.Command {
Use: "create [name]",
Short: "Create a new project",
Example: "yorkie project create sample-project",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("name is required")
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newListCommand() *cobra.Command {
return &cobra.Command{
Use: "ls",
Short: "List all projects",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
rpcAddr := viper.GetString("rpcAddr")
token, err := config.LoadToken(rpcAddr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/yorkie/project/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newUpdateCommand() *cobra.Command {
Use: "update [name]",
Short: "Update a project",
Example: "yorkie project update name [options]",
PreRunE: config.ReadConfig,
PreRunE: config.Preload,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("name is required")
Expand Down

0 comments on commit 2dea9fd

Please sign in to comment.