Skip to content
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

WIP Repl commands #144

Closed

Conversation

lucasalcantara
Copy link
Contributor

@lucasalcantara lucasalcantara commented Jun 5, 2018

Related issue #122

What has been done

  • Implemented commands in Spacemesh REPL
  • Changed abort signal from os/signal to go-prompt lib(now to abort the user needs to press Control + D)
  • Mocked node to implement the new commands
  • Added ENV variable

Commands added

  • create account
  • unlock account
  • lock account
  • account
  • transfer coins
  • setup
  • restart node
  • set
  • echo

@lucasalcantara
Copy link
Contributor Author

@y0sher please, take a look at this new PR.

@y0sher
Copy link
Contributor

y0sher commented Jun 5, 2018

hi please gofmt and golint and push again before I review :)

@lucasalcantara
Copy link
Contributor Author

lucasalcantara commented Jun 5, 2018

@y0sher sorry for the problems with gofmt and golint. Now the problem is inappropriate ioctl for device and apparently the lib has an issue related with it but is happening just when we execute the tests. So I think you can review it and I've subscribed at the issue to keep tracking any news.

Copy link
Contributor

@y0sher y0sher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comments, I'm changing title to WIP.

ENV Outdated
@@ -0,0 +1,2 @@
DataPath=
NetworkID=
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant these should be exposed to the repl from the node's config. they are already presented there

p2p/localnode.go Outdated
SetVariables(params, flags []string) error
Restart(params, flags []string) error
NeedRestartNode(params, flags []string) bool
Setup(allocation string) error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather you create an interface inside repl that has these methods and then in the future we could use LocalNode as this interface if we implement them.

// Setup setup POST.
func (n *localNodeImp) Setup(allocation string) error {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said above, please take these out. you can name it a REPLClient interface

}

func echo(key string) {
fmt.Println(os.Getenv(key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should implement normal echo that replaces ENV variables with the stored env key.

"strings"
)

func setVariables(path string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reading from a file the node (or interface that will represent a node in the future) should expose a function that return the variables that are exported and their values.

repl/repl.go Outdated
}

// StartRepl starts REPL.
func StartRepl(node p2p.LocalNode, envPath string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace node with an interface that holds the needed functions and that LocalNode can implement.

@y0sher y0sher changed the title Repl commands WIP Repl commands Jun 6, 2018
Copy link
Contributor

@y0sher y0sher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, please check my comments.

repl/repl.go Outdated
if err != nil {
r.node.Debug(err.Error())
log.Debug(err.Error())
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In go a good pattern is,

if err != nil {
   log.Error(err)
   retutn
}
r.setup()

Instead of using if/else just return inside the error condition

repl/repl.go Outdated
err := r.client.Unlock(passphrase)
if err != nil {
log.Debug(err.Error())
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again , in idomatic go

if err != nil { 
  log.Debug(err)
  return
}
// The code that was on the else

Note: this command refers to rest of the places you do the same.

Please check https://golang.org/doc/effective_go.html#errors


func setVariables(values map[string]string) error {
for key, value := range values {
err := os.Setenv(key, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By environment variables we meant variables contained inside the node running and not the OS level. This might be problematic if one runs more than one node on a machine and the variables clash

value = strings.TrimSpace(fields[1])
}

err := os.Setenv(key, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting variables shouldn't happen from the echo command. Instead you should create a function that translate variables to their values (recognize variables starting with $. Then every command could use ENV variables.
Setting a variable should be another command (set/export) and should be contained inside the node software.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry if this is a silly question but what is ENV variable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment variables are variables variables KEY/VALUE pairs that are exposed widely in operating systems so software can read and use them.
We want to simulate this feature inside the spacemesh node without affecting the actual operating system environment variables.
The repl should be able read such variables from the node. (As NetworkID and DataFilePath for example)

Copy link
Contributor Author

@lucasalcantara lucasalcantara Jun 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so let's see if I understood right. All that I have to do is get the variables that the user puts on console (e.g. $NetworkID), ask the node the variable and print the value on the console, right? there is no need for a method like setVariables like I created.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now you can start with this. in the future we might want to enable users to set variables through the console aswell.

@lucasalcantara
Copy link
Contributor Author

@y0sher, should I do any change or is it ok now?

@y0sher
Copy link
Contributor

y0sher commented Jun 22, 2018

Great job @lucasalcantara , please write unit tests before we can merge it.

@lucasalcantara
Copy link
Contributor Author

@y0sher I don't know if I can add unit test for it. Since the lib doesn't return the messages that it receives. So, if you have any idea how I can do it I will be glad of try.

@antonlerner
Copy link
Contributor

Closed due to inactivity

@antonlerner
Copy link
Contributor

Thanks for contributing this code! we are about to use it as our first CLI wallet!

@antonlerner antonlerner reopened this Jan 30, 2019
@antonlerner
Copy link
Contributor

Merged opied code int CLIWallet project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants