Created
December 30, 2016 23:02
-
-
Save jbenet/7007202501fc1c4eb623327e5328cb9d to your computer and use it in GitHub Desktop.
ipfs-cluster-addr.go
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
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nvm, this doesn't work