Skip to content

Commit

Permalink
Fix lint errors in ui and remove deprecated ioutil in api
Browse files Browse the repository at this point in the history
Signed-off-by: Shiv Verma <[email protected]>
  • Loading branch information
pratap0007 committed Feb 27, 2023
1 parent ec94a1e commit 246185d
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 87 deletions.
8 changes: 4 additions & 4 deletions api/pkg/app/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package app

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
)

Expand Down Expand Up @@ -75,13 +76,12 @@ func httpRead(url string) ([]byte, error) {
return nil, fmt.Errorf(resp.Status)
}

return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

// readLocalFile reads data from a local file using file path
func readLocalFile(path string) ([]byte, error) {

data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions api/pkg/cli/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package hub

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os/user"
Expand Down Expand Up @@ -184,7 +184,7 @@ func httpGet(url string) ([]byte, int, error) {
}
defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}
Expand Down
14 changes: 9 additions & 5 deletions api/pkg/parser/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -107,7 +106,7 @@ func (c CatalogParser) findResourcesByKind(kind string) ([]Resource, Result) {
// search for resources under catalog/<contextPath>/<kind>
kindPath := filepath.Join(c.repo.Path(), c.contextPath, strings.ToLower(kind))

resourceDirs, err := ioutil.ReadDir(kindPath)
resourceDirs, err := os.ReadDir(kindPath)
if err != nil && ignoreNotExists(err) != nil {
log.Warnf("failed to find %s: %s", kind, err)
// NOTE: returns empty task list; upto caller to check for error
Expand All @@ -121,7 +120,12 @@ func (c CatalogParser) findResourcesByKind(kind string) ([]Resource, Result) {
continue
}

res, r := c.parseResource(kind, kindPath, res)
resDirInfo, err := res.Info()
if err != nil {
log.Infof("Failing to read dir info for %s", res.Name())
continue
}
res, r := c.parseResource(kind, kindPath, resDirInfo)
result.Combine(r)
if r.Errors != nil {
log.Warn(r.Error())
Expand All @@ -141,7 +145,7 @@ func (c CatalogParser) findResourcesByKind(kind string) ([]Resource, Result) {

func dirCount(path string) int {
count := 0
dirs, _ := ioutil.ReadDir(path)
dirs, _ := os.ReadDir(path)
for _, d := range dirs {
if d.IsDir() {
count++
Expand Down Expand Up @@ -332,7 +336,7 @@ func decodeResource(reader io.Reader, kind string) (*TektonResource, error) {
// to read from readers
var dup bytes.Buffer
r := io.TeeReader(reader, &dup)
contents, err := ioutil.ReadAll(r)
contents, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions api/pkg/service/admin/admin_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"os"
"testing"

"github.com/golang-jwt/jwt"
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestUpdateAgent_Http_NewAgent(t *testing.T) {
WithHeader("Authorization", accessToken).WithBody(data).
Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -97,7 +98,7 @@ func TestUpdateAgent_Http_NormalUserExistWithName(t *testing.T) {
WithHeader("Authorization", accessToken).WithBody(data).
Check().
HasStatus(400).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -127,7 +128,7 @@ func TestUpdateAgent_Http_InvalidScopeCase(t *testing.T) {
WithHeader("Authorization", accessToken).WithBody(data).
Check().
HasStatus(400).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -159,7 +160,7 @@ func TestUpdateAgent_Http_UpdateCase(t *testing.T) {
WithHeader("Authorization", accessToken).WithBody(data).
Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -205,7 +206,7 @@ func TestRefreshConfig_Http(t *testing.T) {
RefreshConfigChecker(tc).Test(t, http.MethodPost, "/system/config/refresh").
WithHeader("Authorization", token).Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -230,7 +231,7 @@ func TestRefreshConfig_Http(t *testing.T) {
}

func computeChecksum() (string, error) {
data, err := ioutil.ReadFile("../../../test/config/config.yaml")
data, err := os.ReadFile("../../../test/config/config.yaml")
if err != nil {
return "", err
}
Expand Down
10 changes: 5 additions & 5 deletions api/pkg/service/catalog/catalog_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package catalog
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -51,7 +51,7 @@ func TestRefresh_Http(t *testing.T) {
RefreshChecker(tc).Test(t, http.MethodPost, "/catalog/catalog-official/refresh").
WithHeader("Authorization", token).Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -93,7 +93,7 @@ func TestRefreshAll_Http(t *testing.T) {
RefreshAllChecker(tc).Test(t, http.MethodPost, "/catalog/refresh").
WithHeader("Authorization", token).Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -117,7 +117,7 @@ func TestCatalogError_Http(t *testing.T) {
CatalogErrorChecker(tc).Test(t, http.MethodGet, "/catalog/catalog-official/error").
WithHeader("Authorization", token).Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -141,7 +141,7 @@ func TestCatalogError_HttpHavingNoError(t *testing.T) {
CatalogErrorChecker(tc).Test(t, http.MethodGet, "/catalog/catalog-community/error").
WithHeader("Authorization", token).Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down
6 changes: 3 additions & 3 deletions api/pkg/service/category/category_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package category

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -40,7 +40,7 @@ func TestCategories_List_Http(t *testing.T) {

checker.Test(t, http.MethodGet, "/categories").Check().
HasStatus(http.StatusOK).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -63,7 +63,7 @@ func TestCategories_List_Http_V1(t *testing.T) {

checker.Test(t, http.MethodGet, "/v1/categories").Check().
HasStatus(http.StatusOK).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down
16 changes: 8 additions & 8 deletions api/pkg/service/rating/rating_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package rating

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -47,7 +47,7 @@ func TestGet_Http_InvalidToken(t *testing.T) {
GetChecker(tc).Test(t, http.MethodGet, "/resource/1/rating").
WithHeader("Authorization", "invalidToken").Check().
HasStatus(401).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -75,7 +75,7 @@ func TestGet_Http_ExpiredToken(t *testing.T) {
GetChecker(tc).Test(t, http.MethodGet, "/resource/1/rating").
WithHeader("Authorization", accessToken).Check().
HasStatus(401).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -102,7 +102,7 @@ func TestGet_Http_InvalidScopes(t *testing.T) {
GetChecker(tc).Test(t, http.MethodGet, "/resource/1/rating").
WithHeader("Authorization", accessToken).Check().
HasStatus(403).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -129,7 +129,7 @@ func TestGet_Http(t *testing.T) {
GetChecker(tc).Test(t, http.MethodGet, "/resource/1/rating").
WithHeader("Authorization", accessToken).Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -156,7 +156,7 @@ func TestGet_Http_RatingNotFound(t *testing.T) {
GetChecker(tc).Test(t, http.MethodGet, "/resource/3/rating").
WithHeader("Authorization", accessToken).Check().
HasStatus(200).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand All @@ -183,7 +183,7 @@ func TestGet_Http_ResourceNotFound(t *testing.T) {
GetChecker(tc).Test(t, http.MethodGet, "/resource/99/rating").
WithHeader("Authorization", accessToken).Check().
HasStatus(404).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -273,7 +273,7 @@ func TestUpdate_Http_ResourceNotFound(t *testing.T) {
WithHeader("Authorization", accessToken).
WithBody(data).Check().
HasStatus(404).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down
6 changes: 3 additions & 3 deletions api/pkg/service/status/status_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package status
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"testing"
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestOk_http(t *testing.T) {

checker.Test(t, http.MethodGet, "/").Check().
HasStatus(http.StatusOK).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down Expand Up @@ -120,7 +120,7 @@ func TestDB_NotOK(t *testing.T) {

checker.Test(t, http.MethodGet, "/").Check().
HasStatus(http.StatusOK).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down
4 changes: 2 additions & 2 deletions api/v1/service/catalog/catalog_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package catalog

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -40,7 +40,7 @@ func TestCatalog_List_Http(t *testing.T) {

checker.Test(t, http.MethodGet, "/v1/catalogs").Check().
HasStatus(http.StatusOK).Cb(func(r *http.Response) {
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
assert.NoError(t, readErr)
defer r.Body.Close()

Expand Down
10 changes: 5 additions & 5 deletions api/v1/service/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -185,7 +185,7 @@ func (s *service) ByCatalogKindNameVersionReadme(ctx context.Context,

readmePath := fmt.Sprintf("%s/%s/%s/%s/%s/README.md", s.CatalogClonePath(), strings.ToLower(p.Catalog), strings.ToLower(p.Kind), p.Name, p.Version)
s.Logger(ctx).Info(fmt.Sprintf("Fetching README for resource %s", p.Name))
content, err := ioutil.ReadFile(readmePath)
content, err := os.ReadFile(readmePath)
if err != nil {
return nil, resource.MakeNotFound(fmt.Errorf("resource not found"))
}
Expand All @@ -207,7 +207,7 @@ func (s *service) ByCatalogKindNameVersionYaml(ctx context.Context,
yamlPath := fmt.Sprintf("%s/%s/%s/%s/%s", s.CatalogClonePath(), strings.ToLower(p.Catalog), strings.ToLower(p.Kind), p.Name, p.Version)
yamlPath = fmt.Sprintf("%s/%s.yaml", yamlPath, p.Name)
s.Logger(ctx).Info(fmt.Sprintf("Fetching YAML for resource %s", p.Name))
content, err := ioutil.ReadFile(yamlPath)
content, err := os.ReadFile(yamlPath)
if err != nil {
return nil, resource.MakeNotFound(fmt.Errorf("resource not found"))
}
Expand Down Expand Up @@ -511,10 +511,10 @@ func (s *service) GetRawYamlByCatalogKindNameVersion(ctx context.Context, p *res
yamlPath := fmt.Sprintf("%s/%s/%s/%s/%s", s.CatalogClonePath(), strings.ToLower(p.Catalog), strings.ToLower(p.Kind), p.Name, p.Version)
yamlPath = fmt.Sprintf("%s/%s.yaml", yamlPath, p.Name)

content, err := ioutil.ReadFile(yamlPath)
content, err := os.ReadFile(yamlPath)
if err != nil {
return nil, resource.MakeNotFound(fmt.Errorf("resource not found"))
}

return ioutil.NopCloser(bytes.NewBuffer(content)), nil
return io.NopCloser(bytes.NewBuffer(content)), nil
}
Loading

0 comments on commit 246185d

Please sign in to comment.