forked from turnage/graw
/
loader.go
32 lines (27 loc) · 836 Bytes
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package reddit
import (
"bytes"
"io/ioutil"
"github.com/turnage/redditproto"
"github.com/golang/protobuf/proto"
)
// load loads the user agent and App config from an AgentFile (legacy graw 0.3.0
// file format).
func load(filename string) (string, App, error) {
agentPB, err := loadAgentFile(filename)
return agentPB.GetUserAgent(), App{
ID: agentPB.GetClientId(),
Secret: agentPB.GetClientSecret(),
Username: agentPB.GetUsername(),
Password: agentPB.GetPassword(),
}, err
}
// loadAgentFile reads a user agent from a protobuffer file and returns it.
func loadAgentFile(filename string) (*redditproto.UserAgent, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
agent := &redditproto.UserAgent{}
return agent, proto.UnmarshalText(bytes.NewBuffer(buf).String(), agent)
}