diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ffd785..025557a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## master -* Added ability to replay asciicast from stdin by passing "-" as filename +* Playing asciicast from stdin: `cat demo.json | asciinema play -` +* Playing asciicast from IPFS: `asciinema play ipfs://QmcdXYJp6e4zNuimuGeWPwNMHQdxuqWmKx7NhZofQ1nw2V` * `-q/--quiet` option added to `rec` command ## 1.1.1 (2015-06-21) diff --git a/asciicast/asciicast.go b/asciicast/asciicast.go index 1b23784..e5ceb6a 100644 --- a/asciicast/asciicast.go +++ b/asciicast/asciicast.go @@ -3,8 +3,11 @@ package asciicast import ( "encoding/json" "fmt" + "io" "io/ioutil" + "net/http" "os" + "strings" ) type Env struct { @@ -56,25 +59,50 @@ func Save(asciicast *Asciicast, path string) error { return nil } -func Load(path string) (*Asciicast, error) { - var file *os.File +// asciinema play file.json +// asciinema play https://asciinema.org/a/123.json +// asciinema play ipfs://QmbdpNCwqeZgnmAWBCQcs8u6Ts6P2ku97tfKAycE1XY88p +// asciinema play - + +func getSource(url string) (io.ReadCloser, error) { + if strings.HasPrefix(url, "ipfs://") { + hash := url[7:len(url)] + url = fmt.Sprintf("https://ipfs.io/ipfs/%v", hash) + } + + if url == "-" { + return os.Stdin, nil + } + + if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { + resp, err := http.Get(url) - if path == "-" { - file = os.Stdin - } else { - var err error - file, err = os.Open(path) if err != nil { return nil, err } - defer file.Close() + + if resp.StatusCode != 200 { + resp.Body.Close() + return nil, fmt.Errorf("got status %v when requesting %v", resp.StatusCode, url) + } + + return resp.Body, nil } - dec := json.NewDecoder(file) + return os.Open(url) +} + +func Load(url string) (*Asciicast, error) { + source, err := getSource(url) + if err != nil { + return nil, err + } + defer source.Close() + + dec := json.NewDecoder(source) asciicast := &Asciicast{} - err := dec.Decode(asciicast) - if err != nil { + if err = dec.Decode(asciicast); err != nil { return nil, err }