Add aggressive linting
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
2022-01-24 20:25:52 +01:00
parent 763d691b6c
commit e7b0c5fa33
18 changed files with 188 additions and 32 deletions

View File

@@ -18,9 +18,11 @@ import (
"github.com/kirsle/configdir"
)
const defaultTimeout = 10 * time.Second
type Client struct {
BaseURL string `json:"base_url"`
AuthToken string `json:"auth_token"`
BaseURL string `json:"baseUrl"`
AuthToken string `json:"authToken"`
httpClient http.Client
}
@@ -36,7 +38,9 @@ func (c *Client) WriteConfig() error {
if err != nil {
return err
}
path := filepath.Join(dir, "client.json")
f, err := os.Create(path)
if err != nil {
return err
@@ -49,6 +53,7 @@ func (c *Client) WriteConfig() error {
func (c *Client) LoadConfig() error {
dir := configdir.LocalCache("gpaste")
path := filepath.Join(dir, "client.json")
f, err := os.Open(path)
if err != nil {
return err
@@ -66,7 +71,7 @@ func (c *Client) LoadConfigFromReader(r io.Reader) error {
func (c *Client) Login(ctx context.Context, username, password string) error {
url := fmt.Sprintf("%s/api/login", c.BaseURL)
// TODO: Change timeout
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
body := new(bytes.Buffer)
@@ -74,10 +79,12 @@ func (c *Client) Login(ctx context.Context, username, password string) error {
Username: username,
Password: password,
}
encoder := json.NewEncoder(body)
if err := encoder.Encode(&requestData); err != nil {
return fmt.Errorf("error encoding response: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
@@ -99,6 +106,7 @@ func (c *Client) Login(ctx context.Context, username, password string) error {
if err := decoder.Decode(&responseData); err != nil {
return fmt.Errorf("unable to parse response: %s", err)
}
c.AuthToken = responseData.Token
return nil
@@ -112,6 +120,7 @@ func (c *Client) UserCreate(ctx context.Context, username, password string) erro
Username: username,
Password: password,
}
encoder := json.NewEncoder(body)
if err := encoder.Encode(requestData); err != nil {
return fmt.Errorf("error encoding response: %w", err)
@@ -164,9 +173,6 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) (*api.Respons
client := &http.Client{}
// TODO: Change timeout
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
// TODO: Improve buffering
buf := &bytes.Buffer{}
mw := multipart.NewWriter(buf)
@@ -176,16 +182,21 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) (*api.Respons
if err != nil {
return nil, err
}
if _, err := io.Copy(fw, file.Body); err != nil {
return nil, err
}
file.Body.Close()
}
mw.Close()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", mw.FormDataContentType())
resp, err := client.Do(req)
@@ -213,10 +224,12 @@ func (c *Client) Delete(ctx context.Context, id string) error {
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to perform request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("got non-ok response from server: %s", resp.Status)

View File

@@ -166,7 +166,7 @@ func TestClient(t *testing.T) {
})
t.Run("Save", func(t *testing.T) {
c := client.Client{BaseURL: "http://example.org/gpaste", AuthToken: "tokenpls"}
expectedConfig := "{\"base_url\":\"http://example.org/gpaste\",\"auth_token\":\"tokenpls\"}\n"
expectedConfig := "{\"baseUrl\":\"http://example.org/gpaste\",\"authToken\":\"tokenpls\"}\n"
buf := new(bytes.Buffer)
err := c.WriteConfigToWriter(buf)
if err != nil {
@@ -179,7 +179,7 @@ func TestClient(t *testing.T) {
})
t.Run("Load", func(t *testing.T) {
c := client.Client{}
config := "{\"base_url\":\"http://pasta.example.org\",\"auth_token\":\"tokenpls\"}\n"
config := "{\"baseUrl\":\"http://pasta.example.org\",\"authToken\":\"tokenpls\"}\n"
expectedClient := client.Client{BaseURL: "http://pasta.example.org", AuthToken: "tokenpls"}
sr := strings.NewReader(config)
if err := c.LoadConfigFromReader(sr); err != nil {