Skip to content

Instantly share code, notes, and snippets.

@jbenet
Created December 30, 2016 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbenet/7007202501fc1c4eb623327e5328cb9d to your computer and use it in GitHub Desktop.
Save jbenet/7007202501fc1c4eb623327e5328cb9d to your computer and use it in GitHub Desktop.
ipfs-cluster-addr.go
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
)
type Options struct {
ConfigPath string
}
type RelevantConfig struct {
ID string `json:"id"`
ClusterAddr string `json:"cluster_addr"`
ClusterPort int `json:"cluster_port"`
}
func globalConfigPath() (string, error) {
if path := os.Getenv("IPFSCLUSTER_PATH"); path != "" {
return path, nil
} else {
usr, err := user.Current()
if err != nil {
return "", errors.New("cannot guess the current user")
}
return filepath.Join(usr.HomeDir, ".ipfs-cluster", "server.json"), nil
}
}
func parseOptions() (Options, error) {
var defaultOpts Options
var runtimeOpts Options
// need to set path dynamically because of user.
cp, err := globalConfigPath()
if err != nil {
return Options{}, err
}
defaultOpts.ConfigPath = cp
flag.StringVar(&runtimeOpts.ConfigPath, "config", defaultOpts.ConfigPath,
"path to the ipfscluster-server configuration file")
flag.Parse()
return runtimeOpts, nil
}
func loadConfig(cp string, config *RelevantConfig) error {
file, err := ioutil.ReadFile(cp)
if err != nil {
return err
}
if err := json.Unmarshal(file, config); err != nil {
return err
}
return nil
}
func run() error {
o, err := parseOptions()
if err != nil {
return err
}
var c RelevantConfig
if err := loadConfig(o.ConfigPath, &c); err != nil {
return err
}
fmt.Printf("/ip4/%s/tcp/%d/ipfs/%s\n", c.ClusterAddr, c.ClusterPort, c.ID)
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(-1)
}
}
@jbenet
Copy link
Author

jbenet commented Dec 31, 2016

nvm, this doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment