-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
ipfs config command with git-style option value getters and setters #16
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jbenet/commander" | ||
config "github.com/jbenet/go-ipfs/config" | ||
u "github.com/jbenet/go-ipfs/util" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
var cmdIpfsConfig = &commander.Command{ | ||
UsageLine: "config", | ||
Short: "See and Edit ipfs options", | ||
Long: `ipfs config - See or Edit ipfs configuration. | ||
|
||
See specific config's values with: | ||
ipfs config datastore.path | ||
Assign a new value with: | ||
ipfs config datastore.path ~/.go-ipfs/datastore | ||
|
||
Open the config file in your editor(from $EDITOR): | ||
ipfs config edit | ||
`, | ||
Run: configCmd, | ||
Subcommands: []*commander.Command{ | ||
cmdIpfsConfigEdit, | ||
}, | ||
} | ||
|
||
var cmdIpfsConfigEdit = &commander.Command{ | ||
UsageLine: "edit", | ||
Short: "Opens the configuration file in the editor.", | ||
Long: `Looks up environment variable $EDITOR and | ||
attempts to open the config file with it. | ||
`, | ||
Run: configEditCmd, | ||
} | ||
|
||
func configCmd(c *commander.Command, inp []string) error { | ||
if len(inp) == 0 { | ||
// "ipfs config" run without parameters | ||
u.POut(c.Long + "\n") | ||
return nil | ||
} | ||
|
||
if len(inp) == 1 { | ||
// "ipfs config" run without one parameter, so this is a value getter | ||
value, err := config.GetValueInConfigFile(inp[0]) | ||
if err != nil { | ||
u.POut("Failed to get config value: " + err.Error() + "\n") | ||
} else { | ||
u.POut(value + "\n") | ||
} | ||
return nil | ||
} | ||
|
||
// "ipfs config" run without two parameter, so this is a value setter | ||
err := config.SetValueInConfigFile(inp[0], inp[1:]) | ||
if err != nil { | ||
u.POut("Failed to set config value: " + err.Error() + "\n") | ||
} | ||
return nil | ||
} | ||
|
||
func configEditCmd(c *commander.Command, _ []string) error { | ||
if editor := os.Getenv("EDITOR"); editor == "" { | ||
u.POut("ENVIRON variable $EDITOR is not assigned \n") | ||
} else { | ||
exec.Command("sh", "-c", editor+" "+config.DefaultConfigFilePath).Start() | ||
} | ||
return nil | ||
} |
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
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.
The more idiomatic go way of doing this would be to just open the file
os.Open
and then usejson.NewDecoder
to do the decoding.(obviously written cleaner than my chicken scratch)
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 can rewrite it using
json.Decoder
, but I don't really see that much of a difference.Could you please comment on why this way is preferable / more idiomatic?
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.
@jmoiron does a good job explaining it in one of his articles: http://jmoiron.net/blog/crossing-streams-a-love-letter-to-ioreader/
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.
That's a good post!
@dborzov I can rewrite it-- there's a few more stylistic things i'll fix that don't want to annoy with.
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.
Thanks for the link, that article makes good sense to me.