Skip to content

Commit

Permalink
Log error msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 committed Jun 20, 2024
1 parent 1aba21b commit 2a79270
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 83 deletions.
24 changes: 20 additions & 4 deletions cmd/cbuild/commands/build/buildcprj.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ import (
"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"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func BuildCPRJ(cmd *cobra.Command, args []string) error {
var inputFile string
if len(args) == 1 {
argCnt := len(args)
if argCnt == 0 {
err := errutils.New(errutils.ErrRequireArg, "cbuild buildcprj --help")
log.Error(err)
return err
} else if argCnt == 1 {
inputFile = args[0]
} else {
_ = cmd.Help()
return errutils.New(errutils.ErrInvalidCmdLineArg)
err := errutils.New(errutils.ErrMultipleCmdLineArg)
log.Error(err)
return err
}

intDir, _ := cmd.Flags().GetString("intdir")
Expand Down Expand Up @@ -62,6 +70,7 @@ func BuildCPRJ(cmd *cobra.Command, args []string) error {

configs, err := utils.GetInstallConfigs()
if err != nil {
log.Error(err)
return err
}

Expand All @@ -78,7 +87,15 @@ func BuildCPRJ(cmd *cobra.Command, args []string) error {
if fileExtension == expectedExtension {
b = cproject.CprjBuilder{BuilderParams: params}
} else {
return errutils.New(errutils.ErrInvalidFileExtension, fileExtension, expectedExtension)
err := errutils.New(errutils.ErrInvalidFileExtension, fileExtension, expectedExtension)
log.Error(err)
return err
}

_, err = utils.FileExists(inputFile)
if err != nil {
log.Error(err)
return err
}

return b.Build()
Expand All @@ -87,7 +104,6 @@ func BuildCPRJ(cmd *cobra.Command, args []string) error {
var BuildCPRJCmd = &cobra.Command{
Use: "buildcprj <name>.cprj [options]",
Short: "Use a *.CPRJ file as build input",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return BuildCPRJ(cmd, args)
},
Expand Down
70 changes: 52 additions & 18 deletions cmd/cbuild/commands/list/list_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,70 @@
package list

import (
"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"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func listContexts(cmd *cobra.Command, args []string) error {
var inputFile string
argCnt := len(args)
if argCnt == 0 {
return errutils.New(errutils.ErrRequireArg, "cbuild list contexts --help")
} else if argCnt == 1 {
inputFile = args[0]
} else {
_ = cmd.Help()
return errutils.New(errutils.ErrMultipleCmdLineArg)
}

fileName := filepath.Base(inputFile)
expectedExtension := ".csolution.yml"
if !strings.HasSuffix(fileName, expectedExtension) {
return errutils.New(errutils.ErrInvalidFileExtension, fileName, expectedExtension)
}

_, err := utils.FileExists(inputFile)
if err != nil {
return err
}

configs, err := utils.GetInstallConfigs()
if err != nil {
return err
}

schemaCheck, _ := cmd.Flags().GetBool("schema")
filter, _ := cmd.Flags().GetString("filter")
p := csolution.CSolutionBuilder{
BuilderParams: builder.BuilderParams{
Runner: utils.Runner{},
Options: builder.Options{
Schema: schemaCheck,
Filter: filter,
},
InputFile: args[0],
InstallConfigs: configs,
},
}
return p.ListContexts()
}

var ListContextsCmd = &cobra.Command{
Use: "contexts <name>.csolution.yml [options]",
Short: "Print list of contexts in a csolution.yml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
configs, err := utils.GetInstallConfigs()
err := listContexts(cmd, args)
if err != nil {
return err
}

schemaCheck, _ := cmd.Flags().GetBool("schema")
filter, _ := cmd.Flags().GetString("filter")
p := csolution.CSolutionBuilder{
BuilderParams: builder.BuilderParams{
Runner: utils.Runner{},
Options: builder.Options{
Schema: schemaCheck,
Filter: filter,
},
InputFile: args[0],
InstallConfigs: configs,
},
log.Error(err)
}
return p.ListContexts()
return err
},
}

Expand Down
35 changes: 24 additions & 11 deletions cmd/cbuild/commands/list/list_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,40 @@ package list
import (
"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"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func listEnvironment(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errutils.New(errutils.ErrAcceptNoArgs, "cbuild list environment --help")
}

configs, err := utils.GetInstallConfigs()
if err != nil {
return err
}

p := csolution.CSolutionBuilder{
BuilderParams: builder.BuilderParams{
Runner: utils.Runner{},
InstallConfigs: configs,
},
}
return p.ListEnvironment()
}

var ListEnvironmentCmd = &cobra.Command{
Use: "environment",
Short: "Print list of environment configurations",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
configs, err := utils.GetInstallConfigs()
err := listEnvironment(cmd, args)
if err != nil {
return err
}

p := csolution.CSolutionBuilder{
BuilderParams: builder.BuilderParams{
Runner: utils.Runner{},
InstallConfigs: configs,
},
log.Error(err)
}
return p.ListEnvironment()
return err
},
}

Expand Down
65 changes: 46 additions & 19 deletions cmd/cbuild/commands/list/list_toolchains.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,67 @@
package list

import (
"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"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var ListToolchainsCmd = &cobra.Command{
Use: "toolchains [<name>.csolution.yml] [options]",
Short: "Print list of installed toolchains",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var inputFile string
if len(args) == 1 {
inputFile = args[0]
func listToolchains(cmd *cobra.Command, args []string) error {
var inputFile string
argCnt := len(args)
if argCnt == 1 {
inputFile = args[0]

fileName := filepath.Base(inputFile)
expectedExtension := ".csolution.yml"
if !strings.HasSuffix(fileName, expectedExtension) {
return errutils.New(errutils.ErrInvalidFileExtension, fileName, expectedExtension)
}

configs, err := utils.GetInstallConfigs()
_, err := utils.FileExists(inputFile)
if err != nil {
return err
}
} else if argCnt > 1 {
_ = cmd.Help()
return errutils.New(errutils.ErrMultipleCmdLineArg)
}

configs, err := utils.GetInstallConfigs()
if err != nil {
return err
}

verbose, _ := cmd.Flags().GetBool("verbose")
verbose, _ := cmd.Flags().GetBool("verbose")

p := csolution.CSolutionBuilder{
BuilderParams: builder.BuilderParams{
Runner: utils.Runner{},
Options: builder.Options{
Verbose: verbose,
},
InputFile: inputFile,
InstallConfigs: configs,
p := csolution.CSolutionBuilder{
BuilderParams: builder.BuilderParams{
Runner: utils.Runner{},
Options: builder.Options{
Verbose: verbose,
},
InputFile: inputFile,
InstallConfigs: configs,
},
}
return p.ListToolchains()
}

var ListToolchainsCmd = &cobra.Command{
Use: "toolchains [<name>.csolution.yml] [options]",
Short: "Print list of installed toolchains",
RunE: func(cmd *cobra.Command, args []string) error {
err := listToolchains(cmd, args)
if err != nil {
log.Error(err)
}
return p.ListToolchains()
return err
},
}

Expand Down
11 changes: 9 additions & 2 deletions cmd/cbuild/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ func preConfiguration(cmd *cobra.Command, args []string) error {
parentLogDir := filepath.Dir(logFile)
if _, err := os.Stat(parentLogDir); os.IsNotExist(err) {
if err := os.MkdirAll(parentLogDir, 0755); err != nil {
log.Error(err)
return err
}
}
file, err := os.Create(logFile)
if err != nil {
log.Error(err)
return err
}
multiWriter := io.MultiWriter(os.Stdout, file)
Expand Down Expand Up @@ -109,7 +111,9 @@ func NewRootCmd() *cobra.Command {
inputFile = args[0]
} else {
_ = cmd.Help()
return errutils.New(errutils.ErrInvalidCmdLineArg)
err := errutils.New(errutils.ErrMultipleCmdLineArg)
log.Error(err)
return err
}
intDir, _ := cmd.Flags().GetString("intdir")
outDir, _ := cmd.Flags().GetString("outdir")
Expand Down Expand Up @@ -161,6 +165,7 @@ func NewRootCmd() *cobra.Command {

configs, err := utils.GetInstallConfigs()
if err != nil {
log.Error(err)
return err
}

Expand All @@ -174,12 +179,14 @@ func NewRootCmd() *cobra.Command {
// get builder for supported input file
b, err := getBuilder(inputFile, params)
if err != nil {
log.Error(err)
return err
}

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

Expand Down Expand Up @@ -247,6 +254,6 @@ func getBuilder(inputFile string, params builder.BuilderParams) (builder.IBuilde
case strings.HasSuffix(fileName, ".cprj"):
return cproject.CprjBuilder{BuilderParams: params}, nil
default:
return nil, errutils.New(errutils.ErrInvalidFileExtension, fileName, ".csolution.yml & .cprj")
return nil, errutils.New(errutils.ErrInvalidFileExtension, fileName, ".csolution.yml or .cprj")
}
}
Loading

0 comments on commit 2a79270

Please sign in to comment.