Skip to content

Commit

Permalink
Add initial implementation for the aliases command (#1)
Browse files Browse the repository at this point in the history
* Add initial implementation for the aliases command
  • Loading branch information
jfahrer committed Jul 10, 2019
1 parent 17c4b08 commit 683d079
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Donner
[![Go Report Card](https://goreportcard.com/badge/github.com/jfahrer/donner)](https://goreportcard.com/report/github.com/jfahrer/donner)
[![Go Report Card](https://goreportcard.com/badge/github.com/codetales/donner)](https://goreportcard.com/report/github.com/codetales/donner)

:boom: Donner is a generic command wrapper. It let's you define strategies to wrap commands in things like `docker-compose exec` or `docker container run`.
This is can come in very handy when developing applications in containers.
Expand Down
35 changes: 35 additions & 0 deletions aliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"strings"
)

func printAliases(cfg *Cfg, strictMode, fallbackMode bool) {
commands := cfg.ListCommands()
outputs := make([]string, len(commands))

flags := make([]string, 0, 2)
if strictMode {
flags = append(flags, "--strict")
}

if fallbackMode {
flags = append(flags, "--fallback")
}

for i, c := range commands {
output := append(flags, c)
outputs[i] = strings.Join(output, " ")
}

fmt.Println()
for i, c := range commands {
fmt.Printf("alias %s='donner run %s'\n", c, outputs[i])
}

aliasCommand := strings.Join(append([]string{"donner", "aliases"}, flags...), " ")

fmt.Printf("\n# copy and paste the output into your terminal or run\n")
fmt.Printf("# eval $(%s)\n", aliasCommand)
}
37 changes: 28 additions & 9 deletions donner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"errors"
"fmt"
"github.com/urfave/cli"
"io/ioutil"
"log"
"os"
"os/exec"

"github.com/urfave/cli"
)

// ErrUndefinedCommand is thrown if a command specified can't be found in the yaml definition
Expand All @@ -34,25 +35,28 @@ func main() {
Usage: "run a command",
SkipFlagParsing: true,
Action: func(c *cli.Context) error {
// TODO handle 'yaml' case
dat, err := ioutil.ReadFile(".donner.yml")
cfg, err := readConfig()
if err != nil {
return err
}

cfg, err := parseFile(dat)
if err != nil {
return err
}

return execCommand(cfg, c.Args())
},
},
{
Name: "aliases",
Aliases: []string{"a"},
Usage: "generate aliases",
Flags: []cli.Flag{
cli.BoolFlag{Name: "strict,s", Usage: "enable strict mode"},
cli.BoolFlag{Name: "fallback,f", Usage: "fallback to local commands"},
},
Action: func(c *cli.Context) error {
cfg, err := readConfig()
if err != nil {
return err
}
printAliases(cfg, c.Bool("strict"), c.Bool("fallback"))

return nil
},
},
Expand Down Expand Up @@ -112,3 +116,18 @@ func execCommand(cfg *Cfg, params []string) error {

return nil
}

func readConfig() (*Cfg, error) {
// TODO handle 'yaml' case
dat, err := ioutil.ReadFile(".donner.yml")
if err != nil {
return nil, err
}

cfg, err := parseFile(dat)
if err != nil {
return nil, err
}

return cfg, nil
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/jfahrer/donner
go 1.12

require (
github.com/stretchr/objx v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11 changes: 11 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"

"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -38,6 +39,7 @@ type Strategy struct {
Image string
}

// Validate checks whether the configuration specifies all mandatory properties
func (c *Cfg) Validate() error {
if len(c.Strategies) == 0 {
return ErrNoStrategiesSpecified
Expand All @@ -50,6 +52,15 @@ func (c *Cfg) Validate() error {
return nil
}

// ListCommands allows for retrieval of all defined commands in a config
func (c *Cfg) ListCommands() []string {
commands := make([]string, 0, len(c.Commands))
for cmd := range c.Commands {
commands = append(commands, cmd)
}
return commands
}

// Validate checks whether a strategy specifies only valid handlers
func (s *Strategy) Validate() error {
_, ok := availableHandlers[s.Handler]
Expand Down
6 changes: 6 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ func TestParseFile(t *testing.T) {
})
}
}

func TestListCommands(t *testing.T) {
cfg, err := parseFile([]byte(fullYaml))
assert.NoError(t, err)
assert.ElementsMatch(t, cfg.ListCommands(), []string{"ls", "bundle"})
}

0 comments on commit 683d079

Please sign in to comment.