Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default log mode to Warn+ and streamline error handling and cleanup #243

Merged
merged 6 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ jobs:
name: 'Test (${{ matrix.target }})'
runs-on: ${{ matrix.platform }}
steps:
- name: Install macos deps
if: ${{ startsWith(matrix.platform, 'macos') }}
run: |
brew install \
ninja

- name: Install linux deps
if: ${{ startsWith(matrix.platform, 'ubuntu') }}
run: |
sudo apt-get update
sudo apt-get install \
ninja-build

- name: Install windows deps
if: ${{ startsWith(matrix.platform, 'windows') }}
run: choco install -y ninja

brondani marked this conversation as resolved.
Show resolved Hide resolved
- name: Check out repository code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

Expand Down Expand Up @@ -166,6 +183,12 @@ jobs:
name: 'Coverage check'
runs-on: ubuntu-latest
steps:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install \
ninja-build

- name: Check out repository code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

Expand Down
9 changes: 5 additions & 4 deletions cmd/cbuild/commands/build/buildcprj.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
package build

import (
"errors"
"path/filepath"

"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder/cproject"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/errutils"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/utils"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +21,7 @@ func BuildCPRJ(cmd *cobra.Command, args []string) error {
inputFile = args[0]
} else {
_ = cmd.Help()
return errors.New("invalid arguments")
return errutils.New(errutils.ErrInvalidCmdLineArg)
}

intDir, _ := cmd.Flags().GetString("intdir")
Expand Down Expand Up @@ -73,11 +73,12 @@ func BuildCPRJ(cmd *cobra.Command, args []string) error {
}

fileExtension := filepath.Ext(inputFile)
expectedExtension := ".cprj"
var b builder.IBuilderInterface
if fileExtension == ".cprj" {
if fileExtension == expectedExtension {
b = cproject.CprjBuilder{BuilderParams: params}
} else {
return errors.New("invalid file argument")
return errutils.New(errutils.ErrInvalidFileExtension, fileExtension, expectedExtension)
}

return b.Build()
Expand Down
2 changes: 1 addition & 1 deletion cmd/cbuild/commands/build/buildcprj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestPreLogConfiguration(t *testing.T) {
cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"buildcprj", cprjFile, "-C"})
_ = cmd.Execute()
assert.Equal(log.InfoLevel, log.GetLevel())
assert.Equal(log.WarnLevel, log.GetLevel())
})

t.Run("test quiet verbosity level", func(t *testing.T) {
Expand Down
44 changes: 32 additions & 12 deletions cmd/cbuild/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package commands

import (
"errors"
"fmt"
"io"
"os"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder/cproject"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder/csolution"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/errutils"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,16 +60,18 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e

func preConfiguration(cmd *cobra.Command, args []string) error {
// configure log level
log.SetLevel(log.InfoLevel)
log.SetLevel(log.WarnLevel)
debug, _ := cmd.Flags().GetBool("debug")
quiet, _ := cmd.Flags().GetBool("quiet")
verbose, _ := cmd.Flags().GetBool("verbose")
logFile, _ := cmd.Flags().GetString("log")

if debug {
if debug || verbose {
log.SetLevel(log.DebugLevel)
} else if quiet {
log.SetLevel(log.ErrorLevel)
}

if logFile != "" {
parentLogDir := filepath.Dir(logFile)
if _, err := os.Stat(parentLogDir); os.IsNotExist(err) {
Expand Down Expand Up @@ -107,7 +109,7 @@ func NewRootCmd() *cobra.Command {
inputFile = args[0]
} else {
_ = cmd.Help()
return errors.New("invalid arguments")
return errutils.New(errutils.ErrInvalidCmdLineArg)
}
intDir, _ := cmd.Flags().GetString("intdir")
outDir, _ := cmd.Flags().GetString("outdir")
Expand Down Expand Up @@ -169,14 +171,16 @@ func NewRootCmd() *cobra.Command {
InstallConfigs: configs,
}

fileExtension := filepath.Ext(inputFile)
var b builder.IBuilderInterface
if fileExtension == ".cprj" {
b = cproject.CprjBuilder{BuilderParams: params}
} else if fileExtension == ".yml" || fileExtension == ".yaml" {
b = csolution.CSolutionBuilder{BuilderParams: params}
} else {
return errors.New("invalid file argument")
// get builder for supported input file
b, err := getBuilder(inputFile, params)
if err != nil {
return err
}

// check if input file exists
_, err = utils.FileExists(inputFile)
if err != nil {
return err
}

log.Info("Build Invocation " + Version + CopyrightNotice)
Expand Down Expand Up @@ -233,3 +237,19 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
}
return err
}

func getBuilder(inputFile string, params builder.BuilderParams) (builder.IBuilderInterface, error) {
fileExtension := filepath.Ext(inputFile)
var b builder.IBuilderInterface

switch fileExtension {
case ".cprj":
b = cproject.CprjBuilder{BuilderParams: params}
case ".yml":
b = csolution.CSolutionBuilder{BuilderParams: params}
default:
return nil, errutils.New(errutils.ErrInvalidFileExtension, fileExtension, ".cprj & .yml")
}

return b, nil
}
2 changes: 1 addition & 1 deletion cmd/cbuild/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestPreLogConfiguration(t *testing.T) {
cmd.SetArgs([]string{"--version"})
err := cmd.Execute()
assert.Nil(err)
assert.Equal(log.InfoLevel, log.GetLevel())
assert.Equal(log.WarnLevel, log.GetLevel())
})

t.Run("test quiet verbosity level", func(t *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions cmd/cbuild/commands/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package setup

import (
"errors"
"path/filepath"
"strings"

"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder/csolution"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/errutils"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/utils"
"github.com/spf13/cobra"
)
Expand All @@ -22,12 +22,14 @@ func SetUpProject(cmd *cobra.Command, args []string) error {
inputFile = args[0]
} else {
_ = cmd.Help()
return errors.New("invalid arguments")
return errutils.New(errutils.ErrInvalidCmdLineArg)
}

fileName := filepath.Base(inputFile)
if !strings.HasSuffix(fileName, ".csolution.yml") {
return errors.New("invalid file argument")
expectedExtension := ".csolution.yml"

if !strings.HasSuffix(fileName, expectedExtension) {
return errutils.New(errutils.ErrInvalidFile, fileName, "<project.>"+expectedExtension)
}

logFile, _ := cmd.Flags().GetString("log")
Expand Down
1 change: 0 additions & 1 deletion cmd/cbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func main() {
cmd := commands.NewRootCmd()
err := cmd.Execute()
if err != nil {
log.Error(err)
if exitError, ok := err.(*exec.ExitError); ok {
os.Exit(exitError.ExitCode())
} else {
Expand Down
24 changes: 15 additions & 9 deletions pkg/builder/cbuildidx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

builder "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/errutils"
utils "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/utils"
log "github.com/sirupsen/logrus"
)
Expand All @@ -24,12 +25,12 @@ type CbuildIdxBuilder struct {

func (b CbuildIdxBuilder) checkCbuildIdx() error {
fileName := filepath.Base(b.InputFile)
if !strings.HasSuffix(fileName, ".cbuild-idx.yml") {
err := errors.New(".cbuild-idx.yml file not found")
expectedExtension := ".cbuild-idx.yml"
if !strings.HasSuffix(fileName, expectedExtension) {
err := errutils.New(errutils.ErrInvalidFile, fileName, "<project>."+expectedExtension)
return err
} else {
if _, err := os.Stat(b.InputFile); os.IsNotExist(err) {
log.Error("cbuild-idx file " + b.InputFile + " does not exist")
return err
}
}
Expand Down Expand Up @@ -60,7 +61,6 @@ func (b CbuildIdxBuilder) clean(dirs builder.BuildDirs, vars builder.InternalVar

func (b CbuildIdxBuilder) getDirs(context string) (dirs builder.BuildDirs, err error) {
if _, err := os.Stat(b.InputFile); os.IsNotExist(err) {
log.Error("file " + b.InputFile + " does not exist")
return dirs, err
}

Expand Down Expand Up @@ -94,7 +94,6 @@ func (b CbuildIdxBuilder) getDirs(context string) (dirs builder.BuildDirs, err e
cbuildFile = filepath.Join(path, cbuildFile)
_, outDir, err := GetBuildDirs(cbuildFile)
if err != nil {
log.Error("error parsing file: " + cbuildFile)
return dirs, err
}

Expand All @@ -116,7 +115,7 @@ func (b CbuildIdxBuilder) getDirs(context string) (dirs builder.BuildDirs, err e
return dirs, err
}

func (b CbuildIdxBuilder) Build() error {
func (b CbuildIdxBuilder) build() error {
b.InputFile, _ = filepath.Abs(b.InputFile)
b.InputFile = utils.NormalizePath(b.InputFile)
err := b.checkCbuildIdx()
Expand All @@ -132,7 +131,7 @@ func (b CbuildIdxBuilder) Build() error {
_ = utils.UpdateEnvVars(vars.BinPath, vars.EtcPath)

if len(b.Options.Contexts) == 0 && b.BuildContext == "" {
err = errors.New("error no context(s) to process")
err = errutils.New(errutils.ErrNoContextFound)
return err
}

Expand Down Expand Up @@ -171,13 +170,13 @@ func (b CbuildIdxBuilder) Build() error {
}

if vars.CmakeBin == "" {
log.Error("cmake was not found")
err = errutils.New(errutils.ErrBinaryNotFound, "cmake", "")
return err
}
if b.Options.Generator == "" {
b.Options.Generator = "Ninja"
if vars.NinjaBin == "" {
log.Error("ninja was not found")
err = errutils.New(errutils.ErrBinaryNotFound, "ninja", "")
return err
}
}
Expand Down Expand Up @@ -226,3 +225,10 @@ func (b CbuildIdxBuilder) Build() error {
log.Info("build finished successfully!")
return nil
}

func (b CbuildIdxBuilder) Build() (err error) {
if err = b.build(); err != nil {
log.Error(err)
}
return err
}
4 changes: 2 additions & 2 deletions pkg/builder/cbuildidx/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
package cbuildidx

import (
"errors"
"os"
"path/filepath"
"strings"
"testing"

builder "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/builder"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/errutils"
"github.com/Open-CMSIS-Pack/cbuild/v2/pkg/inittest"
utils "github.com/Open-CMSIS-Pack/cbuild/v2/pkg/utils"
"github.com/stretchr/testify/assert"
Expand All @@ -39,7 +39,7 @@ func (r RunnerMock) ExecuteCommand(program string, quiet bool, args ...string) (
} else if strings.Contains(program, "ninja") {
} else if strings.Contains(program, "xmllint") {
} else {
return "", errors.New("invalid command")
return "", errutils.New(errutils.ErrInvalidCommand)
}
return "", nil
}
Expand Down
Loading