Implement golint suggestions

This commit is contained in:
Marcin Kulik
2014-11-15 13:33:20 +01:00
parent 5ff2567755
commit 41d1d71ab6
7 changed files with 34 additions and 34 deletions

View File

@@ -17,29 +17,29 @@ type Frame struct {
Data []byte Data []byte
} }
type Api interface { type API interface {
CreateAsciicast([]Frame, time.Duration, int, int, string, string) (string, error) CreateAsciicast([]Frame, time.Duration, int, int, string, string) (string, error)
} }
type AsciinemaApi struct { type AsciinemaAPI struct {
url string url string
token string token string
version string version string
http HTTP http HTTP
} }
func New(url, token, version string) *AsciinemaApi { func New(url, token, version string) *AsciinemaAPI {
return &AsciinemaApi{ return &AsciinemaAPI{
url: url, url: url,
token: token, token: token,
version: version, version: version,
http: &HttpClient{}, http: &HTTPClient{},
} }
} }
func (a *AsciinemaApi) CreateAsciicast(frames []Frame, duration time.Duration, cols, rows int, command, title string) (string, error) { func (a *AsciinemaAPI) CreateAsciicast(frames []Frame, duration time.Duration, cols, rows int, command, title string) (string, error) {
response, err := a.http.PostForm( response, err := a.http.PostForm(
a.createUrl(), a.createURL(),
a.username(), a.username(),
a.token, a.token,
a.createHeaders(), a.createHeaders(),
@@ -47,7 +47,7 @@ func (a *AsciinemaApi) CreateAsciicast(frames []Frame, duration time.Duration, c
) )
if err != nil { if err != nil {
return "", errors.New(fmt.Sprintf("Connection failed (%v)", err.Error())) return "", fmt.Errorf("Connection failed (%v)", err.Error())
} }
defer response.Body.Close() defer response.Body.Close()
@@ -71,21 +71,21 @@ func (a *AsciinemaApi) CreateAsciicast(frames []Frame, duration time.Duration, c
return body.String(), nil return body.String(), nil
} }
func (a *AsciinemaApi) createUrl() string { func (a *AsciinemaAPI) createURL() string {
return a.url + "/api/asciicasts" return a.url + "/api/asciicasts"
} }
func (a *AsciinemaApi) username() string { func (a *AsciinemaAPI) username() string {
return os.Getenv("USER") return os.Getenv("USER")
} }
func (a *AsciinemaApi) createHeaders() map[string]string { func (a *AsciinemaAPI) createHeaders() map[string]string {
return map[string]string{ return map[string]string{
"User-Agent": fmt.Sprintf("asciinema/%s %s/%s %s-%s", a.version, runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH), "User-Agent": fmt.Sprintf("asciinema/%s %s/%s %s-%s", a.version, runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH),
} }
} }
func (a *AsciinemaApi) createFiles(frames []Frame, duration time.Duration, cols, rows int, command, title string) map[string]io.Reader { func (a *AsciinemaAPI) createFiles(frames []Frame, duration time.Duration, cols, rows int, command, title string) map[string]io.Reader {
return map[string]io.Reader{ return map[string]io.Reader{
"asciicast[stdout]:stdout": gzippedDataReader(frames), "asciicast[stdout]:stdout": gzippedDataReader(frames),
"asciicast[stdout_timing]:stdout.time": gzippedTimingReader(frames), "asciicast[stdout_timing]:stdout.time": gzippedTimingReader(frames),

View File

@@ -12,9 +12,9 @@ type HTTP interface {
PostForm(string, string, string, map[string]string, map[string]io.Reader) (*http.Response, error) PostForm(string, string, string, map[string]string, map[string]io.Reader) (*http.Response, error)
} }
type HttpClient struct{} type HTTPClient struct{}
func (c *HttpClient) PostForm(url, username, password string, headers map[string]string, files map[string]io.Reader) (*http.Response, error) { func (c *HTTPClient) PostForm(url, username, password string, headers map[string]string, files map[string]io.Reader) (*http.Response, error) {
req, err := createPostRequest(url, username, password, headers, files) req, err := createPostRequest(url, username, password, headers, files)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -9,14 +9,14 @@ import (
) )
type AuthCommand struct { type AuthCommand struct {
apiUrl string apiURL string
apiToken string apiToken string
} }
func NewAuthCommand(cfg *util.Config) cli.Command { func NewAuthCommand(cfg *util.Config) cli.Command {
return &AuthCommand{ return &AuthCommand{
apiUrl: cfg.Api.Url, apiURL: cfg.API.URL,
apiToken: cfg.Api.Token, apiToken: cfg.API.Token,
} }
} }
@@ -25,7 +25,7 @@ func (c *AuthCommand) RegisterFlags(flags *flag.FlagSet) {
func (c *AuthCommand) Execute(args []string) error { func (c *AuthCommand) Execute(args []string) error {
fmt.Println("Open the following URL in your browser to register your API token and assign any recorded asciicasts to your profile:") fmt.Println("Open the following URL in your browser to register your API token and assign any recorded asciicasts to your profile:")
fmt.Printf("%v/connect/%v\n", c.apiUrl, c.apiToken) fmt.Printf("%v/connect/%v\n", c.apiURL, c.apiToken)
return nil return nil
} }

View File

@@ -14,16 +14,16 @@ import (
type RecordCommand struct { type RecordCommand struct {
Cfg *util.Config Cfg *util.Config
Api api.Api API api.API
Terminal terminal.Terminal Terminal terminal.Terminal
Command string Command string
Title string Title string
NoConfirm bool NoConfirm bool
} }
func NewRecordCommand(api api.Api, cfg *util.Config) cli.Command { func NewRecordCommand(api api.API, cfg *util.Config) cli.Command {
return &RecordCommand{ return &RecordCommand{
Api: api, API: api,
Cfg: cfg, Cfg: cfg,
Terminal: terminal.New(), Terminal: terminal.New(),
} }
@@ -82,7 +82,7 @@ func (c *RecordCommand) Execute(args []string) error {
rows, cols, _ = c.Terminal.Size() rows, cols, _ = c.Terminal.Size()
url, err := c.Api.CreateAsciicast(stdout.Frames, stdout.Duration(), cols, rows, c.Command, c.Title) url, err := c.API.CreateAsciicast(stdout.Frames, stdout.Duration(), cols, rows, c.Command, c.Title)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -29,12 +29,12 @@ func (t *testTerminal) Record(command string, stdoutCopy io.Writer) error {
return nil return nil
} }
type testApi struct { type testAPI struct {
err error err error
t *testing.T t *testing.T
} }
func (a *testApi) CreateAsciicast(frames []api.Frame, duration time.Duration, cols, rows int, command, title string) (string, error) { func (a *testAPI) CreateAsciicast(frames []api.Frame, duration time.Duration, cols, rows int, command, title string) (string, error) {
if command != "ls" { if command != "ls" {
a.t.Errorf("expected command to be set on asciicast") a.t.Errorf("expected command to be set on asciicast")
} }
@@ -84,13 +84,13 @@ func TestRecordCommand_Execute(t *testing.T) {
for _, test := range tests { for _, test := range tests {
terminal := &testTerminal{err: test.recordError} terminal := &testTerminal{err: test.recordError}
api := &testApi{err: test.apiError, t: t} api := &testAPI{err: test.apiError, t: t}
command := &commands.RecordCommand{ command := &commands.RecordCommand{
Command: "ls", Command: "ls",
Title: "listing", Title: "listing",
Terminal: terminal, Terminal: terminal,
Api: api, API: api,
} }
err := command.Execute(nil) err := command.Execute(nil)

View File

@@ -17,7 +17,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
api := api.New(cfg.Api.Url, cfg.Api.Token, Version) api := api.New(cfg.API.URL, cfg.API.Token, Version)
cli := &cli.CLI{ cli := &cli.CLI{
Commands: map[string]cli.Command{ Commands: map[string]cli.Command{

View File

@@ -12,13 +12,13 @@ import (
) )
const ( const (
DEFAULT_API_URL = "https://asciinema.org" DefaultAPIURL = "https://asciinema.org"
) )
type Config struct { type Config struct {
Api struct { API struct {
Token string Token string
Url string URL string
} }
Record struct { Record struct {
Command string Command string
@@ -38,12 +38,12 @@ func LoadConfig() (*Config, error) {
return nil, err return nil, err
} }
if cfg.Api.Url == "" { if cfg.API.URL == "" {
cfg.Api.Url = DEFAULT_API_URL cfg.API.URL = DefaultAPIURL
} }
if envApiUrl := os.Getenv("ASCIINEMA_API_URL"); envApiUrl != "" { if envAPIURL := os.Getenv("ASCIINEMA_API_URL"); envAPIURL != "" {
cfg.Api.Url = envApiUrl cfg.API.URL = envAPIURL
} }
return cfg, nil return cfg, nil