Skip to content

Commit

Permalink
fixed cmd dependency tree and made start readable
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed May 23, 2021
1 parent 71d4a75 commit e26777b
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 17 deletions.
34 changes: 34 additions & 0 deletions src/cmd/prompts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
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,
}

selectedIndex := 0
if err := survey.AskOne(prompt, &selectedIndex); err != nil {
return "", err
}

return names[selectedIndex], nil
}
11 changes: 4 additions & 7 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package cmd

import (
"github.com/silphid/yey/src/cmd/get"
"github.com/silphid/yey/src/cmd/start"
"github.com/silphid/yey/src/cmd/versioning"
"github.com/silphid/yey/src/internal/logging"
"github.com/spf13/cobra"
)

// NewRoot creates the root cobra command
func NewRoot(version string) *cobra.Command {
func NewRoot() *cobra.Command {
c := &cobra.Command{
Use: "yey",
Short: "A DevOps & CI/CD & Kubernetes-oriented general purpose Docker container with CLI launcher",
Expand All @@ -19,8 +16,8 @@ func NewRoot(version string) *cobra.Command {

// var options internal.Options
c.PersistentFlags().BoolVarP(&logging.Verbose, "verbose", "v", false, "display verbose messages")
c.AddCommand(get.New())
c.AddCommand(start.New())
c.AddCommand(versioning.New(version))
// c.AddCommand(get.New())
// c.AddCommand(start.New())
// c.AddCommand(versioning.New(version))
return c
}
45 changes: 39 additions & 6 deletions src/cmd/start/start.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package start

import (
"github.com/silphid/yey/src/internal/core"
"context"
"fmt"

yey "github.com/silphid/yey/src/internal"
"github.com/silphid/yey/src/internal/docker"

"github.com/silphid/yey/src/cmd"

"github.com/spf13/cobra"
)

Expand All @@ -11,20 +18,46 @@ func New() *cobra.Command {
Use: "start",
Short: "Starts container",
Args: cobra.RangeArgs(0, 1),
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
name := ""
if len(args) == 1 {
name = args[0]
}
return run(name)
return run(cmd.Context(), name)
},
}
}

func run(name string) error {
c, err := core.New()
func run(ctx context.Context, name string) error {
contexts, err := yey.ReadAndParseContextFile()
if err != nil {
return err
}
return c.Start(name)

if name == "" {
var err error
name, err = cmd.PromptContext(contexts)
if err != nil {
return err
}
}

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

shortImageName, err := docker.GetShortImageName(yeyContext.Image)
if err != nil {
return err
}

containerName := fmt.Sprintf("yey-%s-%s", shortImageName, yeyContext.Name)

api, err := docker.NewAPI()
if err != nil {
return fmt.Errorf("failed to connect to docker client: %w", err)
}

return api.Start(ctx, yeyContext, containerName)
}
4 changes: 2 additions & 2 deletions src/internal/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c Contexts) Merge(source Contexts) Contexts {

// GetNames returns the list of all context names user can choose from,
// including the special "base" contexts.
func (c Contexts) GetNames() ([]string, error) {
func (c Contexts) GetNames() []string {
// Extract unique names
namesMap := make(map[string]bool)
for name := range c.Named {
Expand All @@ -56,7 +56,7 @@ func (c Contexts) GetNames() ([]string, error) {
names = append(names, BaseContextName)
names = append(names, sortedNames...)

return names, nil
return names
}

// GetContext returns context with given name, or base context
Expand Down
2 changes: 1 addition & 1 deletion src/internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func New() (Core, error) {
// GetContextNames returns the list of all context names user can
// choose from, including the special "base" context.
func (c Core) GetContextNames() ([]string, error) {
return c.contexts.GetNames()
return c.contexts.GetNames(), nil
}

// GetContext finds shared/user base/named contexts and returns their merged result.
Expand Down
18 changes: 18 additions & 0 deletions src/internal/docker/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package docker

import (
"fmt"
"regexp"
)

var imageNameRegex = regexp.MustCompile(`(.*/)?(.+?)(:.*)?$`)

// GetShortImageName returns short image name without any registry
// prefix or tag suffix, to be used as part of container name.
func GetShortImageName(imageName string) (string, error) {
submatches := imageNameRegex.FindStringSubmatch(imageName)
if len(submatches) < 4 {
return "", fmt.Errorf("malformed image name %q", imageName)
}
return submatches[2], nil
}
9 changes: 8 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import (
"os"

"github.com/silphid/yey/src/cmd"
"github.com/silphid/yey/src/cmd/get"
"github.com/silphid/yey/src/cmd/start"
"github.com/silphid/yey/src/cmd/versioning"
)

var version string

func main() {
rootCmd := cmd.NewRoot(version)
rootCmd := cmd.NewRoot()
rootCmd.AddCommand(start.New())
rootCmd.AddCommand(get.New())
rootCmd.AddCommand(versioning.New(version))

if err := rootCmd.Execute(); err != nil {
os.Exit(-1)
}
Expand Down

0 comments on commit e26777b

Please sign in to comment.