Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variants support #21

Merged
merged 5 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/src",
"args": ["run"],
"args": ["run", "prod", "go"],
"cwd": "${workspaceFolder}"
}
]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,9 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
23 changes: 8 additions & 15 deletions src/cmd/get/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,27 @@ func New() *cobra.Command {
return &cobra.Command{
Use: "context",
Short: "Displays resolved values of given context",
Args: cobra.RangeArgs(0, 1),
Args: cobra.ArbitraryArgs,
RunE: func(_ *cobra.Command, args []string) error {
name := ""
if len(args) == 1 {
name = args[0]
}
return run(name)
return run(args)
},
}
}

func run(name string) error {
func run(names []string) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
}

if name == "" {
var err error
name, err = cmd.PromptContext(contexts)
if err != nil {
return fmt.Errorf("failed to prompt for desired context: %w", err)
}
names, err = cmd.GetOrPromptContextNames(contexts, names)
if err != nil {
return err
}

context, err := contexts.GetContext(name)
context, err := contexts.GetContext(names)
if err != nil {
return fmt.Errorf("failed to get context with name %q: %w", name, err)
return fmt.Errorf("failed to get context: %w", err)
}

fmt.Println(context.String())
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/get/contexts/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func run() error {
return err
}

fmt.Println(strings.Join(contexts.GetNames(), "\n"))
names := contexts.GetNamesInAllLayers()
for i, layerNames := range names {
fmt.Printf("%s: %s\n", contexts.Layers[i].Name, strings.Join(layerNames, ", "))
}
return nil
}
36 changes: 15 additions & 21 deletions src/cmd/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,22 @@ import (
yey "github.com/silphid/yey/src/internal"
)

func PromptContext(contexts yey.Contexts) (string, error) {
// Get context names
names := contexts.GetNames()
if len(names) == 0 {
return "", fmt.Errorf("no context defined")
}

// Only one context defined, no need to prompt
if len(names) == 1 {
return names[0], nil
}

// Show selection prompt
prompt := &survey.Select{
Message: "Select context:",
Options: names,
}
// Parses given value into context name and variant and, as needed, prompt user for those values
func GetOrPromptContextNames(contexts yey.Contexts, names []string) ([]string, error) {
availableNames := contexts.GetNamesInAllLayers()

selectedIndex := 0
if err := survey.AskOne(prompt, &selectedIndex); err != nil {
return "", err
// Prompt unspecified names
for i := len(names); i < len(contexts.Layers); i++ {
prompt := &survey.Select{
Message: fmt.Sprintf("Select %s:", contexts.Layers[i].Name),
Options: availableNames[i],
}
selectedIndex := 0
if err := survey.AskOne(prompt, &selectedIndex); err != nil {
return nil, err
}
names = append(names, availableNames[i][selectedIndex])
}

return names[selectedIndex], nil
return names, nil
}
23 changes: 8 additions & 15 deletions src/cmd/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ func New() *cobra.Command {
Use: "remove",
Aliases: []string{"rm"},
Short: "Removes context container",
Args: cobra.RangeArgs(0, 1),
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
name := ""
if len(args) == 1 {
name = args[0]
}
return run(cmd.Context(), name, options)
return run(cmd.Context(), args, options)
},
}

Expand All @@ -38,23 +34,20 @@ type options struct {
force bool
}

func run(ctx context.Context, name string, options options) error {
func run(ctx context.Context, names []string, options options) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
}

if name == "" {
var err error
name, err = cmd.PromptContext(contexts)
if err != nil {
return fmt.Errorf("failed to prompt for desired context: %w", err)
}
names, err = cmd.GetOrPromptContextNames(contexts, names)
if err != nil {
return fmt.Errorf("failed to prompt for context: %w", err)
}

context, err := contexts.GetContext(name)
context, err := contexts.GetContext(names)
if err != nil {
return fmt.Errorf("failed to get context with name %q: %w", name, err)
return fmt.Errorf("failed to get context: %w", err)
}

container := yey.ContainerName(contexts.Path, context)
Expand Down
23 changes: 8 additions & 15 deletions src/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ func New() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Runs container using given context",
Args: cobra.RangeArgs(0, 1),
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
name := ""
if len(args) == 1 {
name = args[0]
}
if !cmd.Flag("rm").Changed {
options.Remove = nil
}
return run(cmd.Context(), name, options)
return run(cmd.Context(), args, options)
},
}

Expand All @@ -46,23 +42,20 @@ type Options struct {
Reset bool
}

func run(ctx context.Context, name string, options Options) error {
func run(ctx context.Context, names []string, options Options) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
}

if name == "" {
var err error
name, err = cmd.PromptContext(contexts)
if err != nil {
return fmt.Errorf("failed to prompt for desired context: %w", err)
}
names, err = cmd.GetOrPromptContextNames(contexts, names)
if err != nil {
return err
}

yeyContext, err := contexts.GetContext(name)
yeyContext, err := contexts.GetContext(names)
if err != nil {
return fmt.Errorf("failed to get context with name %q: %w", name, err)
return fmt.Errorf("failed to get context: %w", err)
}
if options.Remove != nil {
yeyContext.Remove = options.Remove
Expand Down
23 changes: 20 additions & 3 deletions src/cmd/tidy/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ func run(ctx context.Context, options options) error {
}

validNames := make(map[string]struct{})
for _, name := range contexts.GetNames() {
ctx, err := contexts.GetContext(name)
forEachPossibleNameCombination(contexts.GetNamesInAllLayers(), nil, func(names []string) error {
ctx, err := contexts.GetContext(names)
if err != nil {
return err
}
validNames[yey.ContainerName(contexts.Path, ctx)] = struct{}{}
}
return nil
})

prefix := yey.ContainerPathPrefix(contexts.Path)

Expand All @@ -67,3 +68,19 @@ func run(ctx context.Context, options options) error {

return docker.RemoveMany(ctx, unreferencedContainers, docker.WithForceRemove(options.force))
}

// forEachPossibleNameCombination calls given callback function with each possible combination of names from all layers
func forEachPossibleNameCombination(namesInLayers [][]string, baseCombo []string, fn func([]string) error) error {
currentDepth := len(baseCombo)
for _, name := range namesInLayers[currentDepth] {
currentCombo := append(baseCombo, name)
if currentDepth == len(namesInLayers)-1 {
fn(currentCombo)
} else {
if err := forEachPossibleNameCombination(namesInLayers, currentCombo, fn); err != nil {
return err
}
}
}
return nil
}
29 changes: 29 additions & 0 deletions src/cmd/tidy/tidy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tidy

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestForEachPossibleNameCombination(t *testing.T) {
allNames := [][]string{
{"dev", "stg", "prod"},
{"go", "node"},
}
expected := [][]string{
{"dev", "go"},
{"dev", "node"},
{"stg", "go"},
{"stg", "node"},
{"prod", "go"},
{"prod", "node"},
}
var actual [][]string
forEachPossibleNameCombination(allNames, nil, func(combo []string) error {
actual = append(actual, combo)
return nil
})

assert.Equal(t, expected, actual)
}
2 changes: 1 addition & 1 deletion src/internal/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package yey

import (
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

type DockerBuild struct {
Expand Down
22 changes: 13 additions & 9 deletions src/internal/contextFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

const (
Expand All @@ -19,9 +19,11 @@ const (

// ContextFile represents yey's current config persisted to disk
type ContextFile struct {
Version int
Parent string
Contexts `yaml:",inline"`
Version int
Parent string
Path string `yaml:"-"`
Context `yaml:",inline"`
Layers Layers `yaml:"layers"`
}

// readContextFileFromWorkingDirectory scans the current directory and searches for a .yeyrc.yaml file and returns
Expand Down Expand Up @@ -86,7 +88,7 @@ func parseContextFile(dir string, data []byte) (Contexts, error) {

contexts := Contexts{
Context: ctxFile.Context,
Named: ctxFile.Named,
Layers: ctxFile.Layers,
}

if dir != "" {
Expand Down Expand Up @@ -152,10 +154,12 @@ func resolveContextsPaths(dir string, contexts Contexts) (Contexts, error) {
if err != nil {
return Contexts{}, err
}
for name, context := range contexts.Named {
contexts.Named[name], err = resolveContextPaths(dir, context)
if err != nil {
return Contexts{}, err
for _, layer := range contexts.Layers {
for name, context := range layer.Contexts {
layer.Contexts[name], err = resolveContextPaths(dir, context)
if err != nil {
return Contexts{}, err
}
}
}
return contexts, nil
Expand Down
Loading