-
Notifications
You must be signed in to change notification settings - Fork 177
Add DefaultShell as required by #69 #106
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,42 @@ func NewShellWithClient(url string, c *gohttp.Client) *Shell { | |
} | ||
} | ||
|
||
// Get shell from environmental variables, default api path or gateway. | ||
func DefaultShell() (*Shell, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will DefaultShell be called more than once? I think people will normally call it for one shot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see people calling |
||
urls := make([]string, 1) | ||
if ipfsAPI := os.Getenv("IPFS_API"); ipfsAPI != "" { | ||
urls = append(urls, ipfsAPI) | ||
} | ||
ipfsPath := os.Getenv("IPFS_PATH") | ||
if ipfsPath == "" { | ||
if homePath, err := homedir.Dir(); err == nil { | ||
ipfsPath = homePath | ||
} | ||
} | ||
if ipfsPath != "" { | ||
apifile := path.Join(ipfsPath, ".ipfs", "api") | ||
if data, err := ioutil.ReadFile(apifile); err == nil { | ||
url := strings.Trim(string(data), "\n\t ") | ||
urls = append(urls, url) | ||
} | ||
} | ||
urls = append(urls, "/ip4/127.0.0.1/tcp/5001", "https://ipfs.io") | ||
|
||
// do not repeat encountered addresses | ||
encountered := map[string]bool{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idiom is to just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your advise. I will revise it to use |
||
for _, url := range urls { | ||
if encountered[url] != true { | ||
encountered[url] = true | ||
sh := NewShell(url) | ||
_, _, err := sh.Version() | ||
if err == nil { | ||
return sh, nil | ||
} | ||
} | ||
} | ||
return nil, errors.New("No default node is working") | ||
} | ||
|
||
func (s *Shell) SetTimeout(d time.Duration) { | ||
s.httpcli.Timeout = d | ||
} | ||
|
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.
We should spell out exactly how we pick the default shell.
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 do you think if I add some functions which respectively obtain new shells from environmental variables, default gateway and the api file. I think the order of shells to try in the function DefautlShell is sensible, as normally the environmental variables are explicitly set by the user and I have tried to use gateway as the the last resort.
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.
Sounds good to me.