-
-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify CLI commands by saving rpc-addr and project to context #592
Closed
Closed
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5fb9282
feat : add viper libraray & login & save context
Wu22e 6fe0f6e
add : context command
Wu22e be7ac3b
fix : update context variable using viper
Wu22e dbee189
fix : add err check
Wu22e 51467ad
fix : add error wrapping
Wu22e 4e36603
fix : goimports
Wu22e aff8dad
refactor : remove duplicate preRunE function
Wu22e d9334c1
fix : add comment for exported function
Wu22e ee803ae
fix : exported comment with function full name
Wu22e f8256de
fix : downgrading viper & grpc dependency
Wu22e fbdbf2a
fix : change copyright text 2022 to 2023
Wu22e 2dea9fd
fix : change ReadConfig name to Preload & fix logic
Wu22e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2022 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Package context provides the context(configuration) command. | ||
package context | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// SubCmd represents the document command | ||
SubCmd = &cobra.Command{ | ||
Use: "context", | ||
Short: "Manage contexts", | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2022 The Yorkie Authors. All rights reserved. | ||
Wu22e marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package context | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/yorkie-team/yorkie/cmd/yorkie/config" | ||
) | ||
|
||
func newListCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "ls", | ||
Short: "List all contexts from configuration", | ||
PreRunE: config.ReadConfig, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
conf, err := config.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Current Context:") | ||
fmt.Printf(" rpcAddr: %s\n isInsecure: %v\n", viper.GetString("rpcAddr"), viper.GetBool("isInsecure")) | ||
|
||
fmt.Println("All Auth Contexts:") | ||
tw := table.NewWriter() | ||
tw.AppendHeader(table.Row{"RpcAddr", "Token"}) | ||
for addr, auth := range conf.Auths { | ||
tw.AppendRow(table.Row{addr, auth}) | ||
} | ||
fmt.Println(tw.Render()) | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func init() { | ||
SubCmd.AddCommand(newListCommand()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2022 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package context | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/yorkie-team/yorkie/cmd/yorkie/config" | ||
) | ||
|
||
func newRemoveCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "remove [rpcAddr]", | ||
Short: "Remove the context for the specified rpcAddr", | ||
Args: cobra.ExactArgs(1), | ||
PreRunE: config.ReadConfig, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
conf, err := config.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rpcAddr := args[0] | ||
|
||
// Check if auth exists for the rpcAddr | ||
if _, ok := conf.Auths[rpcAddr]; !ok { | ||
return fmt.Errorf("no context exists for the rpcAddr: %s", rpcAddr) | ||
} | ||
|
||
// Delete the context for the rpcAddr | ||
delete(conf.Auths, rpcAddr) | ||
|
||
err = config.Save(conf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Context for rpcAddr: %s has been removed.\n", rpcAddr) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func init() { | ||
SubCmd.AddCommand(newRemoveCmd()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2022 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package context | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/yorkie-team/yorkie/cmd/yorkie/config" | ||
) | ||
|
||
func newSetContextCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "set", | ||
Short: "Set the current global flags as the context", | ||
PreRunE: config.ReadConfig, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
conf, err := config.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rpcAddr := viper.GetString("rpcAddr") | ||
conf.RPCAddr = rpcAddr | ||
conf.IsInsecure = viper.GetBool("isInsecure") | ||
|
||
// check if auth exists for the rpcAddr | ||
if _, ok := conf.Auths[rpcAddr]; !ok { | ||
conf.Auths[rpcAddr] = "" | ||
fmt.Println("A new RPC address has been set. Please log in again.") | ||
} | ||
|
||
err = config.Save(conf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Context has been updated.") | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func init() { | ||
SubCmd.AddCommand(newSetContextCmd()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this
file
do?Is this required for
viper.ReadInConfig()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some concerns about this part.
Actually, I don't use the file variable in the section you mentioned.
In the existing CLI command, the Load function of
config.go
is used, and the Load function checks for the existence of the config path and config.json file and creates one if it doesn't exist.viper.ReadInConfig()
primarily reads the configuration values based on the configname, configtype, and config path set incommands.go.
During this process, for users who have installed the yorkie program for the first time, the
config.json
does not exist in the~/.yorkie
directory. Therefore, if simply callviper.ReadInConfig()
, an error occurs.To resolve this, I utilized the logic of the Load function from
config.go
. Do you think there's a need to modify the logic, or would it be better to remove the duplicated logic from the Load method?(Additionally, the statement
path.Join(os.Getenv("HOME"), ".yorkie")
in commands.go seems not to properly find the HOME directory in a Windows environment. Should this also be taken into consideration?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be better to refactor duplicate codes in
Load()
andReadConfig()
.Also, since
ReadConfig()
name is confusing withLoad()
, how about changing this function name to something likePreload()
?We need to cover Windows as well. We might need to code like this: https://gist.github.com/miguelmota/f30a04a6d64bd52d7ab59ea8d95e54da
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback. I will make the changes.