Skip to content

Commit

Permalink
Merge pull request #6 from silphid/refactor-structure
Browse files Browse the repository at this point in the history
fixed cmd dependency tree and made start readable
  • Loading branch information
silphid authored May 24, 2021
2 parents 2611ba9 + b9cec77 commit e727f49
Show file tree
Hide file tree
Showing 18 changed files with 850 additions and 249 deletions.
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ module github.com/silphid/yey
go 1.14

require (
github.com/AlecAivazis/survey/v2 v2.2.12 // indirect
github.com/AlecAivazis/survey/v2 v2.2.12
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/containerd/containerd v1.4.4 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.6+incompatible // indirect
github.com/containerd/containerd v1.5.2 // indirect
github.com/docker/docker v20.10.6+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/go-test/deep v1.0.7
github.com/mitchellh/go-homedir v1.1.0
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v2 v2.4.0
)
689 changes: 674 additions & 15 deletions go.sum

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions src/cmd/get/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package context
import (
"fmt"

"github.com/silphid/yey/src/internal/core"
"github.com/silphid/yey/src/cmd"
yey "github.com/silphid/yey/src/internal"
"github.com/spf13/cobra"
)

Expand All @@ -24,18 +25,24 @@ func New() *cobra.Command {
}

func run(name string) error {
c, err := core.New()
contexts, err := yey.LoadContexts()
if err != nil {
return err
}
name, err = c.GetOrPromptContextName(name)
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)
}
}
context, err := c.GetContext(name)

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

fmt.Println(context.String())
return nil
}
12 changes: 5 additions & 7 deletions src/cmd/get/contexts/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"fmt"
"strings"

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

"github.com/spf13/cobra"
)

Expand All @@ -21,14 +22,11 @@ func New() *cobra.Command {
}

func run() error {
c, err := core.New()
contexts, err := yey.LoadContexts()
if err != nil {
return err
}
names, err := c.GetContextNames()
if err != nil {
return err
}
fmt.Println(strings.Join(names, "\n"))

fmt.Println(strings.Join(contexts.GetNames(), "\n"))
return nil
}
9 changes: 1 addition & 8 deletions src/cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package get

import (
"github.com/silphid/yey/src/cmd/get/containers"
"github.com/silphid/yey/src/cmd/get/context"
"github.com/silphid/yey/src/cmd/get/contexts"
"github.com/spf13/cobra"
)

// New creates a cobra command
func New() *cobra.Command {
c := &cobra.Command{
return &cobra.Command{
Use: "get",
Short: "Displays value(s) of entity or variable",
}
c.AddCommand(containers.New())
c.AddCommand(context.New())
c.AddCommand(contexts.New())
return c
}
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
}
9 changes: 2 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,6 @@ 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))

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.LoadContexts()
if err != nil {
return err
}
return c.Start(name)

if name == "" {
var err error
name, err = cmd.PromptContext(contexts)
if err != nil {
return fmt.Errorf("failed to prompt for desired context: %w", 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 fmt.Errorf("failed to get short image name for context.image: %w", 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)
}
41 changes: 0 additions & 41 deletions src/internal/contain/contain.go

This file was deleted.

29 changes: 16 additions & 13 deletions src/internal/contextFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func readContextFileFromFilePath(path string) ([]byte, error) {
return os.ReadFile(path)
}

// readContextFileFromNetwork reads the contextfile from the network over http.
// readContextFileFromNetwork reads the contextfile from the network over http
func readContextFileFromNetwork(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
Expand Down Expand Up @@ -89,7 +89,7 @@ func parseContextFile(data []byte) (Contexts, error) {
}

if ctxFile.Parent != "" {
parent, err := readAndParseContextFile(ctxFile.Parent)
parent, err := readAndParseContextFileFromURI(ctxFile.Parent)
if err != nil {
return Contexts{}, fmt.Errorf("failed to resolve parent context %q: %w", ctxFile.Parent, err)
}
Expand All @@ -99,29 +99,32 @@ func parseContextFile(data []byte) (Contexts, error) {
return contexts, nil
}

// readAndParseContextFile reads and parses the context file from a path. If empty will work from current working directory
// looking for default .yeyrc.yaml file, if starts with https: will download from network. Otherwise searches path in filesystem
func readAndParseContextFile(path string) (Contexts, error) {
// readAndParseContextFileFromURI reads and parses the context file from an URI, which can either
// be an URL or local path
func readAndParseContextFileFromURI(path string) (Contexts, error) {
var bytes []byte
var err error

if path == "" {
bytes, err = readContextFileFromWorkingDirectory()
} else if strings.HasPrefix(path, "https:") {
if strings.HasPrefix(path, "https:") || strings.HasPrefix(path, "http:") {
bytes, err = readContextFileFromNetwork(path)
} else {
bytes, err = readContextFileFromFilePath(path)
}

if err != nil {
return Contexts{}, fmt.Errorf("failed to read contextfile: %w", err)
return Contexts{}, fmt.Errorf("failed to read context file: %w", err)
}

return parseContextFile(bytes)
}

// ReadAndParseContextFile reads the context file and returns the contexts. It starts by reading from current working directory
// and resolves all parent context files
func ReadAndParseContextFile() (Contexts, error) {
return readAndParseContextFile("")
// LoadContexts reads the context file and returns the contexts. It starts by reading from current
// working directory and resolves all parent context files.
func LoadContexts() (Contexts, error) {
bytes, err := readContextFileFromWorkingDirectory()
if err != nil {
return Contexts{}, fmt.Errorf("failed to read context file: %w", err)
}

return parseContextFile(bytes)
}
2 changes: 1 addition & 1 deletion src/internal/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func loadContext(file string) Context {
path := filepath.Join("testdata", file+".yaml")
contexts, err := readAndParseContextFile(path)
contexts, err := readAndParseContextFileFromURI(path)
if err != nil {
panic(fmt.Errorf("loading context from %q: %w", path, err))
}
Expand Down
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
Loading

0 comments on commit e727f49

Please sign in to comment.