Skip to content

Commit

Permalink
Merge pull request #26 from restechnica/fix/commander-error-handling
Browse files Browse the repository at this point in the history
fix/commander-error-handling
  • Loading branch information
shiouen committed Nov 12, 2021
2 parents 21a9fd9 + b151b29 commit 0190850
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
15 changes: 15 additions & 0 deletions internal/commands/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package commands

import (
"fmt"
)

type CommandError struct {
Arguments []string
Output string
Err error
}

func (e CommandError) Error() string {
return fmt.Sprintf("command %s exited with %s, output: \n%s", e.Arguments, e.Err.Error(), e.Output)
}
34 changes: 9 additions & 25 deletions internal/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"bytes"
"fmt"
"os/exec"
)

Expand All @@ -20,39 +19,24 @@ func NewExecCommander() *ExecCommander {
// Returns the output of the command or an error if it failed.
func (c ExecCommander) Output(name string, arg ...string) (string, error) {
var command = exec.Command(name, arg...)
var buffer bytes.Buffer

var stdout bytes.Buffer
var stderr bytes.Buffer

command.Stdout = &stdout
command.Stderr = &stderr
command.Stdout = &buffer
command.Stderr = &buffer

var err = command.Run()
var output = buffer.String()

if err != nil || !isEmpty(&stderr) {
return stdout.String(), fmt.Errorf("%s: %s", err, stderr.String())
if err != nil {
return "", CommandError{Arguments: command.Args, Err: err, Output: output}
}

return stdout.String(), err
return output, err
}

// Run runs a command.
// Returns an error if it failed.
func (c ExecCommander) Run(name string, arg ...string) error {
var command = exec.Command(name, arg...)

var stderr bytes.Buffer
command.Stderr = &stderr

var err = command.Run()

if err != nil || !isEmpty(&stderr) {
return fmt.Errorf("%s: %s", err, stderr.String())
}

return nil
}

func isEmpty(buffer *bytes.Buffer) bool {
return buffer.Len() == 0
var _, err = c.Output(name, arg...)
return err
}
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"github.com/restechnica/semverbot/cmd"
)
import "github.com/restechnica/semverbot/cmd"

// main bootstraps the `sbot` CLI app.
func main() {
Expand Down
23 changes: 23 additions & 0 deletions pkg/cli/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
package cli

const (
// DefaultConfig the default config.
DefaultConfig = `[git]
[git.config]
email = "semverbot@github.com"
name = "semverbot"
[git.tags]
prefix = "v"
[semver]
mode = "auto"
[semver.detection]
patch = ["fix/", "[fix]"]
minor = ["feature/", "[feature]"]
major = ["release/", "[release]"]
`

// DefaultConfigFilePath the default relative filepath to the config file.
DefaultConfigFilePath = ".semverbot.toml"

// DefaultVersion the default version when no other version can be found.
DefaultVersion = "0.0.0"
)
31 changes: 6 additions & 25 deletions pkg/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,9 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
)

// InitCommandSemverbotConfig the default semverbot config.
const InitCommandSemverbotConfig = `[git]
[git.config]
email = "semverbot@github.com"
name = "semverbot"
[git.tags]
prefix = "v"

[semver]
mode = "auto"
[semver.detection]
patch = ["fix/", "[fix]"]
minor = ["feature/", "[feature]"]
major = ["release/", "[release]"]
`
"github.com/restechnica/semverbot/pkg/cli"
)

// NewInitCommand creates a new init command.
// returns a new init spf13/cobra command.
Expand All @@ -44,11 +26,10 @@ func NewInitCommand() *cobra.Command {
// returns an error if the command failed.
func InitCommandRunE(cmd *cobra.Command, args []string) (err error) {
var file *os.File
var path = ".semverbot.toml"

if _, err = os.Stat(path); !os.IsNotExist(err) {
if _, err = os.Stat(cli.DefaultConfigFilePath); !os.IsNotExist(err) {
var prompt = &survey.Confirm{
Message: "Do you wish to overwrite your current semverbot config?",
Message: "Do you wish to overwrite your current config?",
}

var isOk = false
Expand All @@ -62,11 +43,11 @@ func InitCommandRunE(cmd *cobra.Command, args []string) (err error) {
}
}

if file, err = os.Create(path); err != nil {
if file, err = os.Create(cli.DefaultConfigFilePath); err != nil {
return err
}

if _, err = io.WriteString(file, InitCommandSemverbotConfig); err != nil {
if _, err = io.WriteString(file, cli.DefaultConfig); err != nil {
_ = file.Close()
return err
}
Expand Down

0 comments on commit 0190850

Please sign in to comment.