-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91f7e55
commit 2b58c18
Showing
12 changed files
with
170 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cmder | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type CommandOption func(*CommandRunner, *exec.Cmd) | ||
type ErrorParser func(output string, err error) error | ||
|
||
type CommandRunner struct { | ||
Exe string | ||
Args []string | ||
ErrorParser ErrorParser | ||
outputBuffer bytes.Buffer | ||
} | ||
|
||
func WithStdin(stdin io.Reader) CommandOption { | ||
return func(cr *CommandRunner, cmd *exec.Cmd) { | ||
cmd.Stdin = stdin | ||
} | ||
} | ||
|
||
func WithOutput(w io.Writer) CommandOption { | ||
return func(cr *CommandRunner, cmd *exec.Cmd) { | ||
cmd.Stdout = io.MultiWriter(w, &cr.outputBuffer) | ||
cmd.Stderr = io.MultiWriter(w, &cr.outputBuffer) | ||
} | ||
} | ||
|
||
func WithErrorParser(parser ErrorParser) CommandOption { | ||
return func(cr *CommandRunner, cmd *exec.Cmd) { | ||
cr.ErrorParser = parser | ||
} | ||
} | ||
|
||
func WithArgs(args ...string) CommandOption { | ||
return func(cr *CommandRunner, cmd *exec.Cmd) { | ||
cr.Args = args | ||
} | ||
} | ||
|
||
// RunCommand executes a command with the given options | ||
func (r *CommandRunner) RunCommand(opts ...CommandOption) error { | ||
r.outputBuffer.Reset() | ||
|
||
cmd := exec.Command(r.Exe) | ||
|
||
for _, opt := range opts { | ||
opt(r, cmd) | ||
} | ||
|
||
cmd.Args = append(cmd.Args, r.Args...) | ||
|
||
log.Debug().Msgf("Running command: %s %s", r.Exe, strings.Join(r.Args, " ")) | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
log.Error().Msgf("Command failed: %s", err) | ||
if r.ErrorParser != nil { | ||
return r.ErrorParser(r.outputBuffer.String(), err) | ||
} | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sdm | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type SDMErrorCode int | ||
|
||
const ( | ||
Unauthorized SDMErrorCode = iota | ||
InvalidCredentials | ||
Unknown | ||
ResourceNotFound | ||
) | ||
|
||
type SDMError struct { | ||
Code SDMErrorCode | ||
Msg string | ||
} | ||
|
||
func (e SDMError) Error() string { | ||
return e.Msg | ||
} | ||
|
||
// parseSdmError parses the output and error to return an SDMError with the appropriate code. | ||
func parseSdmError(output string, err error) error { | ||
log.Debug().Msgf("Parsing error: %s", output) | ||
if err == nil { | ||
return nil | ||
} | ||
switch { | ||
case strings.Contains(output, "You are not authenticated"): | ||
return SDMError{Code: Unauthorized, Msg: output} | ||
case strings.Contains(output, "Cannot find datasource named"): | ||
return SDMError{Code: ResourceNotFound, Msg: output} | ||
case strings.Contains(output, "access denied"): | ||
return SDMError{Code: InvalidCredentials, Msg: output} | ||
case strings.Contains(output, "Invalid credentials"): | ||
return SDMError{Code: InvalidCredentials, Msg: output} | ||
default: | ||
return SDMError{Code: Unknown, Msg: output} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.