-
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
fa2393a
commit 5e6c07a
Showing
6 changed files
with
156 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,3 @@ | |
|
||
# Go workspace file | ||
go.work | ||
runny |
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 |
---|---|---|
@@ -1,124 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type CommandName string | ||
|
||
var secondaryColor = color.New(color.FgHiBlack) | ||
var defaultShell = "/bin/bash" | ||
|
||
func singleLine(command string) string { | ||
command = strings.TrimSpace(command) | ||
lines := strings.Split(command, "\n") | ||
trimmedLines := []string{} | ||
for _, line := range lines { | ||
trimmedLines = append(trimmedLines, strings.TrimSpace(line)) | ||
} | ||
return strings.Join(trimmedLines, "; ") | ||
} | ||
|
||
type CommandDef struct { | ||
Command string `yaml:"command"` | ||
Pre []CommandName `yaml:"pre"` | ||
Post []CommandName `yaml:"post"` | ||
} | ||
|
||
type Config struct { | ||
Commands map[CommandName]CommandDef `yaml:"commands"` | ||
shell string `yaml:"shell"` | ||
} | ||
|
||
func (c *Config) GetShell() string { | ||
if len(c.shell) > 0 { | ||
return c.shell | ||
} | ||
return defaultShell | ||
} | ||
|
||
func (c *CommandDef) Execute(conf Config) error { | ||
// Handle pre-commands | ||
for _, name := range c.Pre { | ||
// TODO: handle invalid names | ||
command := conf.Commands[name] | ||
err := command.Execute(conf) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Handle the command | ||
command := strings.TrimSpace(c.Command) | ||
if len(command) > 0 { | ||
// FIXME: -c is bash-specific, won't work with every shell | ||
args := []string{"-c", command} | ||
|
||
cmd := exec.Command(conf.GetShell(), args...) | ||
cmd.Stdout = os.Stdout | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
fmt.Printf("%s %s\n", color.RedString(string(command)), secondaryColor.Sprint(err)) | ||
return err | ||
} | ||
} | ||
|
||
// Handle post-commands | ||
for _, name := range c.Post { | ||
// TODO: handle invalid names | ||
command := conf.Commands[name] | ||
err := command.Execute(conf) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
import "github.com/simonwhitaker/runny/runny" | ||
|
||
func main() { | ||
var conf Config | ||
|
||
// Read .runny.yaml from the current directory | ||
yamlFile, err := os.ReadFile(".runny.yaml") | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = yaml.Unmarshal(yamlFile, &conf) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// read command line args | ||
if len(os.Args) > 1 { | ||
name := CommandName(os.Args[1]) | ||
if command, ok := conf.Commands[name]; ok { | ||
command.Execute(conf) | ||
} else { | ||
color.Red("Command not found") | ||
} | ||
} else { | ||
commands := conf.Commands | ||
names := make([]CommandName, len(commands)) | ||
i := 0 | ||
for key := range commands { | ||
names[i] = key | ||
i += 1 | ||
} | ||
|
||
slices.Sort(names) | ||
|
||
for _, name := range names { | ||
nameColor := color.New(color.Bold) | ||
fmt.Printf("%s %s\n", nameColor.Sprint(name), secondaryColor.Sprint(singleLine(commands[name].Command))) | ||
} | ||
} | ||
runny.Run() | ||
} |
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,46 @@ | ||
package runny | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"slices" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
func showHelp(conf Config) { | ||
commands := conf.Commands | ||
names := make([]CommandName, len(commands)) | ||
i := 0 | ||
for key := range commands { | ||
names[i] = key | ||
i += 1 | ||
} | ||
|
||
slices.Sort(names) | ||
|
||
for _, name := range names { | ||
nameColor := color.New(color.Bold) | ||
fmt.Printf("%s %s\n", nameColor.Sprint(name), secondaryColor.Sprint(commandStringToSingleLine(commands[name].Command))) | ||
} | ||
|
||
} | ||
|
||
func Run() { | ||
conf, err := readConfig() | ||
if err != nil { | ||
color.Red("Problem reading config: %v", err) | ||
} | ||
|
||
// read command line args | ||
if len(os.Args) > 1 { | ||
name := CommandName(os.Args[1]) | ||
if command, ok := conf.Commands[name]; ok { | ||
command.Execute(conf) | ||
} else { | ||
color.Red("Command not found") | ||
} | ||
} else { | ||
showHelp(conf) | ||
} | ||
} |
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,6 @@ | ||
package runny | ||
|
||
import "github.com/fatih/color" | ||
|
||
var defaultShell = "/bin/bash" | ||
var secondaryColor = color.New(color.FgHiBlack) |
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,69 @@ | ||
package runny | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
type CommandName string | ||
type CommandDef struct { | ||
Command string `yaml:"command"` | ||
Pre []CommandName `yaml:"pre"` | ||
Post []CommandName `yaml:"post"` | ||
} | ||
|
||
type Config struct { | ||
Commands map[CommandName]CommandDef `yaml:"commands"` | ||
shell string `yaml:"shell"` | ||
} | ||
|
||
func (c *Config) GetShell() string { | ||
if len(c.shell) > 0 { | ||
return c.shell | ||
} | ||
return defaultShell | ||
} | ||
|
||
func (c *CommandDef) Execute(conf Config) error { | ||
// Handle pre-commands | ||
for _, name := range c.Pre { | ||
// TODO: handle invalid names | ||
command := conf.Commands[name] | ||
err := command.Execute(conf) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Handle the command | ||
command := strings.TrimSpace(c.Command) | ||
if len(command) > 0 { | ||
// FIXME: -c is bash-specific, won't work with every shell | ||
args := []string{"-c", command} | ||
|
||
cmd := exec.Command(conf.GetShell(), args...) | ||
cmd.Stdout = os.Stdout | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
fmt.Printf("%s %s\n", color.RedString(string(command)), secondaryColor.Sprint(err)) | ||
return err | ||
} | ||
} | ||
|
||
// Handle post-commands | ||
for _, name := range c.Post { | ||
// TODO: handle invalid names | ||
command := conf.Commands[name] | ||
err := command.Execute(conf) | ||
if err != nil { | ||
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,33 @@ | ||
package runny | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func commandStringToSingleLine(command string) string { | ||
command = strings.TrimSpace(command) | ||
lines := strings.Split(command, "\n") | ||
trimmedLines := []string{} | ||
for _, line := range lines { | ||
trimmedLines = append(trimmedLines, strings.TrimSpace(line)) | ||
} | ||
return strings.Join(trimmedLines, "; ") | ||
} | ||
|
||
func readConfig() (Config, error) { | ||
// Read .runny.yaml from the current directory | ||
var conf Config | ||
yamlFile, err := os.ReadFile(".runny.yaml") | ||
if err != nil { | ||
return conf, err | ||
} | ||
|
||
err = yaml.Unmarshal(yamlFile, &conf) | ||
if err != nil { | ||
return conf, err | ||
} | ||
return conf, nil | ||
} |