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

fix: Error handling #248

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 21 additions & 5 deletions cmd/cbuild/commands/build/buildcprj.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -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 {
soumeh01 marked this conversation as resolved.
Show resolved Hide resolved
err := errutils.New(errutils.ErrRequireArg, "cbuild buildcprj --help")
log.Error(err)
return err
} else if argCnt == 1 {
inputFile = args[0]
} else {
err := errutils.New(errutils.ErrInvalidCmdLineArg)
log.Error(err)
_ = cmd.Help()
return errutils.New(errutils.ErrInvalidCmdLineArg)
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
74 changes: 55 additions & 19 deletions cmd/cbuild/commands/list/list_contexts.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,77 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
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 {
err := errutils.New(errutils.ErrInvalidCmdLineArg)
log.Error(err)
_ = cmd.Help()
return err
}

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
37 changes: 25 additions & 12 deletions cmd/cbuild/commands/list/list_environment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -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
69 changes: 49 additions & 20 deletions cmd/cbuild/commands/list/list_toolchains.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,74 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
* Copyright (c) 2023-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
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 {
err := errutils.New(errutils.ErrInvalidCmdLineArg)
log.Error(err)
_ = cmd.Help()
return err
}

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 @@ -108,8 +110,10 @@ func NewRootCmd() *cobra.Command {
if len(args) == 1 {
inputFile = args[0]
} else {
err := errutils.New(errutils.ErrInvalidCmdLineArg)
log.Error(err)
_ = cmd.Help()
return errutils.New(errutils.ErrInvalidCmdLineArg)
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