Skip to content

Commit

Permalink
Reorganize Konkfile code
Browse files Browse the repository at this point in the history
  • Loading branch information
jclem committed Mar 29, 2024
1 parent 8705fde commit 200355b
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 169 deletions.
15 changes: 12 additions & 3 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

var konkfilePath string

const konkfileName = "konkfile"

var execCommand = cobra.Command{
Use: "exec <command>",
Aliases: []string{"e"},
Expand All @@ -30,7 +32,12 @@ var execCommand = cobra.Command{
}
}

kfsearch := []string{"konkfile", "konkfile.json", "konkfile.toml", "konkfile.yaml", "konkfile.yml"}
kfsearch := []string{
konkfileName,
konkfileName + ".json",
konkfileName + ".toml",
konkfileName + ".yaml", konkfileName + ".yml",
}
if konkfilePath != "" {
kfsearch = []string{konkfilePath}
}
Expand Down Expand Up @@ -71,13 +78,15 @@ var execCommand = cobra.Command{
if err := toml.Unmarshal(kf, &file); err != nil {
return fmt.Errorf("unmarshalling konkfile: %w", err)
}
} else {
} else if ext == ".json" {
if err := json.Unmarshal(kf, &file); err != nil {
return fmt.Errorf("unmarshalling konkfile: %w", err)
}
} else {
return fmt.Errorf("unrecognized file extension: %s", ext)
}

if err := konkfile.Execute(cmd.Context(), file, args[0], konkfile.ExecuteConfig{
if err := file.Execute(cmd.Context(), args[0], konkfile.ExecuteConfig{
AggregateOutput: aggregateOutput,
ContinueOnError: continueOnError,
NoColor: noColor,
Expand Down
24 changes: 12 additions & 12 deletions konk/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
)

type Command struct {
c *exec.Cmd
cmd *exec.Cmd
out strings.Builder
prefix string
}

type RunCommandConfig struct {
AggregateOutput bool
KillOnCancel bool
StopOnCancel bool
}

type ShellCommandConfig struct {
Expand All @@ -40,7 +40,7 @@ func NewShellCommand(conf ShellCommandConfig) *Command {
prefix := getPrefix(conf.Label, conf.NoColor)

return &Command{
c: c,
cmd: c,
prefix: prefix,
}
}
Expand All @@ -63,30 +63,30 @@ func setEnv(c *exec.Cmd, env []string, omitEnv bool) {
}

func NewCommand(conf CommandConfig) *Command {
c := exec.Command(conf.Name, conf.Args...) //nolint:gosec // Intentional user-defined sub-process.
setEnv(c, conf.Env, conf.OmitEnv)
cmd := exec.Command(conf.Name, conf.Args...) //nolint:gosec // Intentional user-defined sub-process.
setEnv(cmd, conf.Env, conf.OmitEnv)
prefix := getPrefix(conf.Label, conf.NoColor)

return &Command{
c: c,
cmd: cmd,
prefix: prefix,
}
}

func (c *Command) Run(ctx context.Context, cancel context.CancelFunc, conf RunCommandConfig) error {
stdout, err := c.c.StdoutPipe()
stdout, err := c.cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("getting stdout pipe: %w", err)
}
c.c.Stderr = c.c.Stdout
c.cmd.Stderr = c.cmd.Stdout

out := make(chan string)
scanner := bufio.NewScanner(stdout)
scannerDone := make(chan bool)
scannerErr := make(chan error)
allDone := make(chan error)

if err := c.c.Start(); err != nil {
if err := c.cmd.Start(); err != nil {
return fmt.Errorf("starting command: %w", err)
}

Expand Down Expand Up @@ -119,8 +119,8 @@ func (c *Command) Run(ctx context.Context, cancel context.CancelFunc, conf RunCo
fmt.Fprint(os.Stdout, line)
}
case <-ctx.Done():
if conf.KillOnCancel {
_ = c.c.Process.Signal(syscall.SIGTERM)
if conf.StopOnCancel {
_ = c.cmd.Process.Signal(syscall.SIGTERM)
allDone <- nil
return
}
Expand All @@ -146,7 +146,7 @@ func (c *Command) Run(ctx context.Context, cancel context.CancelFunc, conf RunCo
fmt.Fprint(os.Stdout, c.ReadOut())
}

if err := c.c.Wait(); err != nil {
if err := c.cmd.Wait(); err != nil {
cancel()

var xerr *exec.ExitError
Expand Down
134 changes: 0 additions & 134 deletions konk/konkfile/execute.go

This file was deleted.

Loading

0 comments on commit 200355b

Please sign in to comment.