Skip to content

Commit

Permalink
fix: fix all lint errors shown in golangci-lint (#75)
Browse files Browse the repository at this point in the history
* fix: fix all lint errors shown in golangci-lint

Signed-off-by: hanshal101 <[email protected]>

* fix: fix all lint errors shown in golangci-lint

Signed-off-by: hanshal101 <[email protected]>

* fix: fix all lint errors shown in golangci-lint

Signed-off-by: hanshal101 <[email protected]>

* fix: fix all lint errors shown in golangci-lint

Signed-off-by: hanshal101 <[email protected]>

---------

Signed-off-by: hanshal101 <[email protected]>
  • Loading branch information
hanshal101 authored Jul 12, 2024
1 parent 26b2946 commit 5a6adca
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 118 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ jobs:

- name: Run GolangCI-Lint
run: task lint
continue-on-error: true
3 changes: 3 additions & 0 deletions cmd/direnv/direnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func fetchGitignore() error {
if !strings.Contains(string(read), ".envrc") {

file, err := os.OpenFile(".gitignore", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
_, err = file.WriteString("\n.envrc")
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/init/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var (
textStyle = styles.TextStyle.Render
sucessStyle = styles.SucessStyle.Render
spinnerStyle = styles.SpinnerStyle
helpStyle = styles.HelpStyle.Render
errorStyle = styles.ErrorStyle.Render
stages = 4
// helpStyle = styles.HelpStyle.Render
errorStyle = styles.ErrorStyle.Render
stages = 4
)

type model struct {
Expand Down
6 changes: 3 additions & 3 deletions cmd/nixgenerate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var (
textStyle = styles.TextStyle.Render
sucessStyle = styles.SucessStyle.Render
spinnerStyle = styles.SpinnerStyle
helpStyle = styles.HelpStyle.Render
errorStyle = styles.ErrorStyle.Render
stages = 2
// helpStyle = styles.HelpStyle.Render
errorStyle = styles.ErrorStyle.Render
stages = 2
)

type model struct {
Expand Down
6 changes: 1 addition & 5 deletions cmd/precheck/precheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ var PreCheckCmd = &cobra.Command{
func checkVersionGreater(currentVer string, nixversion string) bool {
val := semver.Compare(currentVer, nixversion)

if val < 0 {
return false
}

return true
return val >= 0
}

// ValidateNixVersion checks if the current nix version is it compatible with bsf
Expand Down
8 changes: 4 additions & 4 deletions cmd/search/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (m *versionConstraintsModel) updateVersionConstraint() (tea.Model, tea.Cmd)

data, err := os.ReadFile("bsf.hcl")
if err != nil {
m.errorMsg = fmt.Sprintf(errorStyle.Render("Error reading bsf.hcl: %s", err.Error()))
m.errorMsg = errorStyle.Render(fmt.Sprintf("Error reading bsf.hcl: %s", err.Error()))
return m, tea.Quit
}

Expand All @@ -133,19 +133,19 @@ func (m *versionConstraintsModel) updateVersionConstraint() (tea.Model, tea.Cmd)
// changing file handler to allow writes
fh.ModFile, err = os.Create("bsf.hcl")
if err != nil {
m.errorMsg = fmt.Sprintf(errorStyle.Render("Error creating bsf.hcl: %s", err.Error()))
m.errorMsg = errorStyle.Render(fmt.Sprintf("Error creating bsf.hcl: %s", err.Error()))
return m, tea.Quit
}

err = hcl2nix.AddPackages(data, newConfFromSelectedPackages(m.name, m.version, m.selectedConstraints, m.env), fh.ModFile)
if err != nil {
m.errorMsg = fmt.Sprintf(errorStyle.Render("Error updating bsf.hcl: %s", err.Error()))
m.errorMsg = errorStyle.Render(fmt.Sprintf("Error updating bsf.hcl: %s", err.Error()))
return m, tea.Quit
}

err = generate.Generate(fh, sc)
if err != nil {
m.errorMsg = fmt.Sprintf(errorStyle.Render("Error regenerating nix files: %s", err.Error()))
m.errorMsg = errorStyle.Render(fmt.Sprintf("Error regenerating bsf.hcl: %s", err.Error()))
return m, tea.Quit
}

Expand Down
4 changes: 0 additions & 4 deletions cmd/search/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ func convLPR2Items(packages *buildsafev1.ListPackagesResponse) []list.Item {
return items
}

type (
errMsg struct{ error }
)

type mode int

const (
Expand Down
2 changes: 1 addition & 1 deletion cmd/search/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m packageOptionModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if semver.IsValid("v" + m.version) {
v := initVersionConstraints(m.name, m.version, m.selected, m)
p := tea.NewProgram(v, tea.WithAltScreen())
if err := p.Start(); err != nil {
if _, err := p.Run(); err != nil {
m.errorMsg = fmt.Sprintf("Error starting version constraints model: %s", err.Error())
}
} else {
Expand Down
100 changes: 35 additions & 65 deletions pkg/builddocker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (
"fmt"
"html/template"
"io"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/buildsafedev/bsf/pkg/hcl2nix"
)
Expand Down Expand Up @@ -75,48 +72,48 @@ func readDockerFile(file *os.File) ([]string, error) {
return lines, nil
}

func editDockerfile(lines []string, dev bool, tag string) ([]string, error) {
var searchTag string
if dev {
searchTag = "bsfimage:dev"
} else {
searchTag = "bsfimage:runtime"
}

var selectedFrom string
var selectedIndex int
for i, line := range lines {
if strings.Contains(line, searchTag) {
selectedFrom = line
selectedIndex = i
break
}
}
func editDockerfile(lines []string, dev bool, tag string) ([]string, error) {
var searchTag string
if dev {
searchTag = "bsfimage:dev"
} else {
searchTag = "bsfimage:runtime"
}

if selectedFrom == "" {
return nil, fmt.Errorf("no FROM command found with tag %s", searchTag)
var selectedFrom string
var selectedIndex int
for i, line := range lines {
if strings.Contains(line, searchTag) {
selectedFrom = line
selectedIndex = i
break
}
}

fromParts := strings.Fields(selectedFrom)
if len(fromParts) < 2 {
return nil, fmt.Errorf("invalid FROM command format")
}
if selectedFrom == "" {
return nil, fmt.Errorf("no FROM command found with tag %s", searchTag)
}

var newFrom string
if strings.Contains(fromParts[1], ":") {
imageParts := strings.Split(fromParts[1], ":")
newFrom = fmt.Sprintf("FROM %s:%s", imageParts[0], tag)
} else {
newFrom = fmt.Sprintf("FROM %s:%s", fromParts[1], tag)
}
for _, part := range fromParts[2:] {
newFrom = fmt.Sprintf("%s %s", newFrom, part)
}
fromParts := strings.Fields(selectedFrom)
if len(fromParts) < 2 {
return nil, fmt.Errorf("invalid FROM command format")
}

lines[selectedIndex] = newFrom
return lines, nil
var newFrom string
if strings.Contains(fromParts[1], ":") {
imageParts := strings.Split(fromParts[1], ":")
newFrom = fmt.Sprintf("FROM %s:%s", imageParts[0], tag)
} else {
newFrom = fmt.Sprintf("FROM %s:%s", fromParts[1], tag)
}
for _, part := range fromParts[2:] {
newFrom = fmt.Sprintf("%s %s", newFrom, part)
}

lines[selectedIndex] = newFrom
return lines, nil
}

func convertExportCfgToDockerfileCfg(env hcl2nix.OCIArtifact, platform string) dockerfileCfg {
switch platform {
case "linux/amd64":
Expand All @@ -134,33 +131,6 @@ func convertExportCfgToDockerfileCfg(env hcl2nix.OCIArtifact, platform string) d
}
}

// generateRandomFilename generates a random filename
func generateRandomFilename() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
letterRunes := []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, 10)
for i := range b {
b[i] = letterRunes[r.Intn(len(letterRunes))]
}
return string(b)
}

func createTempDir() (string, error) {
tmpDir := os.TempDir()
bsfDir := filepath.Join(tmpDir, "bsf")

if _, err := os.Stat(bsfDir); os.IsNotExist(err) {
err := os.Mkdir(bsfDir, 0755)
if err != nil {
return "", err
}
} else if err != nil {
return "", err
}

return bsfDir, nil
}

func convertEnvsToMap(envs []string) map[string]string {
envMap := make(map[string]string)

Expand Down
7 changes: 2 additions & 5 deletions pkg/clients/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewClientWithAddr(addr string, tlsSkip bool) (buildsafev1.SearchServiceClie
} else {
creds = insecure.NewCredentials()
}
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(creds))
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
Expand Down Expand Up @@ -51,10 +51,7 @@ func SortPackagesWithVersion(packageVersions []*buildsafev1.Package) []*buildsaf
packageVersions[i].Version = "v" + packageVersions[i].Version
}
sort.Slice(packageVersions, func(i, j int) bool {
if semver.Compare(packageVersions[i].Version, packageVersions[j].Version) > 0 {
return true
}
return false
return semver.Compare(packageVersions[i].Version, packageVersions[j].Version) > 0
})
for i := range packageVersions {
packageVersions[i].Version = packageVersions[i].Version[1:]
Expand Down
8 changes: 6 additions & 2 deletions pkg/hcl2nix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func ReadConfig(src []byte, dstErr io.Writer) (*Config, error) {
78,
true,
)
wr.WriteDiagnostics(diags)
if err := wr.WriteDiagnostics(diags); err != nil {
return nil, fmt.Errorf("error writing diagnostics: %w", err)
}
return nil, diags
}

Expand All @@ -119,7 +121,9 @@ func ReadConfig(src []byte, dstErr io.Writer) (*Config, error) {
78,
true,
)
wr.WriteDiagnostics(diags)
if err := wr.WriteDiagnostics(diags); err != nil {
return nil, fmt.Errorf("error writing diagnostics: %w", err)
}
return nil, diags
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/langdetect/langdetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ProjectDetails struct {
Name string
}

var supportedLanguages = []string{string(GoModule), string(PythonPoetry), string(RustCargo)}
// var supportedLanguages = []string{string(GoModule), string(PythonPoetry), string(RustCargo)}

// FindProjectType detects the programming language/package manager of the current project.
func FindProjectType() (ProjectType, *ProjectDetails, error) {
Expand Down
1 change: 0 additions & 1 deletion pkg/nix/cmd/closure.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func addNarHashToGraph(graph *gographviz.Graph) {
}

wg.Wait()
return
}

// GetNarHashFromPath returns the sha256 hash of the nar
Expand Down
1 change: 1 addition & 0 deletions pkg/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (s *Statement) FromDerivationClosure(drvPath string, drv *derivation.Deriva
},
}

//nolint
s.Predicate = prov

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/release/githubRelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (gh *GHRelease) GHReleaseCreate(params GHParams) error {
return err
}
if ghRelease == nil {
ghRelease, err = gh.createRelease(context.Background())
_, err = gh.createRelease(context.Background())
if err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/release/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ func createFileDescriptors(fs *file.Store, dir string, files []string) ([]ociv1.
return fileDescriptors, nil
}

func ociCreateFlake(fds []ociv1.Descriptor, params OCIParams) error {
// temp file
ff, err := os.CreateTemp("/tmp", "flake.nix")
if err != nil {
return err
}
// func ociCreateFlake(fds []ociv1.Descriptor, params OCIParams) error {
// // temp file
// ff, err := os.CreateTemp("/tmp", "flake.nix")
// if err != nil {
// return err
// }

defer ff.Close()
// defer ff.Close()

// TODO: write flake to temp file
// // TODO: write flake to temp file

return nil
}
// return nil
// }
36 changes: 28 additions & 8 deletions pkg/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,30 @@ func parseLockfileToSBOMNodes(document *sbom.Document, appNode *sbom.Node, lf *h

document.NodeList.AddNode(&snode)
if pkg.Runtime {
document.NodeList.RelateNodeAtID(&snode, appNode.Id, sbom.Edge_runtimeDependency)
if err := document.NodeList.RelateNodeAtID(
&snode,
appNode.Id,
sbom.Edge_runtimeDependency,
); err != nil {
continue
}
} else {
document.NodeList.RelateNodeAtID(&snode, appNode.Id, sbom.Edge_devDependency)
document.NodeList.RelateNodeAtID(&snode, appNode.Id, sbom.Edge_devTool)
if err := document.NodeList.RelateNodeAtID(
&snode,
appNode.Id,
sbom.Edge_devDependency,
); err != nil {
continue
}
if err := document.NodeList.RelateNodeAtID(
&snode,
appNode.Id,
sbom.Edge_devTool,
); err != nil {
continue
}
}
}

return
}

func parseDotGraph(document *sbom.Document, appNode *sbom.Node, graph *gographviz.Graph) {
Expand All @@ -154,10 +170,14 @@ func parseDotGraph(document *sbom.Document, appNode *sbom.Node, graph *gographvi
},
}
document.NodeList.AddNode(&snode)
document.NodeList.RelateNodeAtID(&snode, appNode.Id, sbom.Edge_contains)
if err := document.NodeList.RelateNodeAtID(
&snode,
appNode.Id,
sbom.Edge_contains,
); err != nil {
continue
}
}

return
}

// GeneratePurl returns a package url for the given name and version
Expand Down
Loading

0 comments on commit 5a6adca

Please sign in to comment.