func GetLocalShell() (*sh.Shell, error) { apiAddress := os.Getenv("IPFS_API") if apiAddress != "" { // IPFS_API takes priority. return sh.NewShell(apiAddress), nil } apiFilePath := "" ipath := os.Getenv("IPFS_PATH") if ipath != "" { apiFilePath = filepath.Join(ipath, "api") } home, err := hd.Dir() // Get home directory if err == nil { apiFilePath = filepath.Join(home, ".ipfs", "api") } // Read the file (hopefully) containing the location of an ipfs daemon apiFileContent, err := ioutil.ReadFile(apiFilePath) if err != nil { return nil, err } multiAddr := strings.Trim(string(apiFileContent), "\n\t ") apiAddress, err = multiaddrToNormal(multiAddr) if err != nil { return nil, err } return sh.NewShell(apiAddress), nil }
func getLocalApiShell() (*sh.Shell, error) { ipath := os.Getenv("IPFS_PATH") if ipath == "" { home := os.Getenv("HOME") if home == "" { return nil, errors.New("neither IPFS_PATH nor home dir set") } ipath = filepath.Join(home, ".ipfs") } apifile := filepath.Join(ipath, "api") data, err := ioutil.ReadFile(apifile) if err != nil { return nil, err } addr := strings.Trim(string(data), "\n\t ") host, err := multiaddrToNormal(addr) if err != nil { return nil, err } return sh.NewShell(host), nil }
func GetCurrentVersion() (string, error) { fix := func(s string) string { if !strings.HasPrefix(s, "v") { s = "v" + s } return s } // try checking a locally running daemon first apiurl, err := util.ApiEndpoint(util.IpfsDir()) if err == nil { sh := api.NewShell(apiurl) v, _, err := sh.Version() if err == nil { return fix(v), nil } } stump.VLog("daemon check failed: %s", err) _, err = exec.LookPath("ipfs") if err != nil { return "none", nil } // try running the ipfs binary in the users path out, err := exec.Command("ipfs", "version", "-n").CombinedOutput() if err != nil { return "", fmt.Errorf("version check failed: %s - %s", string(out), err) } return fix(strings.Trim(string(out), " \n\t")), nil }
func Fetch(ipfspath string) (io.ReadCloser, error) { sh := api.NewShell("http://localhost:5001") if sh.IsUp() { return sh.Cat(ipfspath) } return httpFetch(gateway + ipfspath) }
func NewShell() *sh.Shell { os.Getenv("IPFS_API") ash, err := getLocalApiShell() if err == nil { return ash } return sh.NewShell("ipfs.io") }
func getApiShell() (Shell, error) { apiShell := api.NewShell("http://127.0.0.1:5001") _, _, err := apiShell.Version() if err != nil { return nil, err } return apiShell, nil }
func NewApiShell() (Shell, error) { addr, err := apiAddr() if err != nil { return nil, err } apiShell := api.NewShell(addr) _, _, err = apiShell.Version() if err != nil { return nil, err } return apiShell, nil }
func GetCurrentVersion() (string, error) { // try checking a locally running daemon first sh := api.NewShell("http://localhost:5001") v, _, err := sh.Version() if err == nil { return v, nil } fmt.Println("daemon check failed: ", err) // try running the ipfs binary in the users path out, err := exec.Command("ipfs", "version", "-n").CombinedOutput() if err != nil { return "", fmt.Errorf("version check failed: %s - %s", string(out), err) } return string(out), nil }
func Fetch(ipfspath string) (io.ReadCloser, error) { stump.VLog(" - fetching %q", ipfspath) ep, err := ApiEndpoint(IpfsDir()) if err == nil { sh := api.NewShell(ep) if sh.IsUp() { stump.VLog(" - using local ipfs daemon for transfer") rc, err := sh.Cat(ipfspath) if err != nil { return nil, err } return newLimitReadCloser(rc, fetchSizeLimit), nil } } return httpFetch(GlobalGatewayUrl + ipfspath) }
func main() { sh := api.NewShell("localhost:5001") r := &Registry{ pkgs: make(map[string]*Package), sh: sh, } err := r.tryLoad(regname) if err != nil { Fatal(err) } pkgT := hb.Trigger{ func(b *hb.Bot, mes *hb.Message) bool { return strings.HasPrefix(mes.Content, "!gx ") }, func(c *hb.Bot, mes *hb.Message) bool { parts := strings.Split(mes.Content, " ") if len(parts) == 1 { return true } switch parts[1] { case "pub": if len(parts) != 4 { return true } name := parts[2] hash := parts[3] Log("add package request from %s [%s %s]", mes.From, name, hash) err := r.CheckAndAddPackage(name, &Package{ Hash: hash, Author: mes.From, }) if err != nil { c.Msg(mes.To, err.Error()) } else { c.Msg(mes.To, "success!") } case "get": if len(parts) != 3 { return true } h, err := r.GetPackage(parts[2]) if err != nil { c.Msg(mes.To, err.Error()) } else { c.Msg(mes.To, h) } default: Error("unrecognized command: ", parts[1]) } return true }, } bot, err := hb.NewBot("irc.freenode.com:6667", "gexbot", hb.ReconOpt()) if err != nil { Fatal(err) } bot.Channels = []string{"#whydev"} bot.AddTrigger(pkgT) bot.Run() for range bot.Incoming { } }
func HasDaemonRunning() bool { shell := api.NewShell(LocalApiUrl) return shell.IsUp() }