Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jul 20, 2024
1 parent fa2393a commit 5e6c07a
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 120 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@

# Go workspace file
go.work
runny
121 changes: 2 additions & 119 deletions main.go
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()
}
46 changes: 46 additions & 0 deletions runny/cmd.go
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)
}
}
6 changes: 6 additions & 0 deletions runny/constants.go
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)
69 changes: 69 additions & 0 deletions runny/model.go
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
}
33 changes: 33 additions & 0 deletions runny/utils.go
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
}

0 comments on commit 5e6c07a

Please sign in to comment.