Skip to content

Commit

Permalink
Merge pull request #2 from silphid/yey-yey
Browse files Browse the repository at this point in the history
move ctx an ctxfile to package yey
  • Loading branch information
silphid authored May 18, 2021
2 parents efccd7e + 13f38fa commit 6379024
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 124 deletions.
42 changes: 18 additions & 24 deletions src/internal/ctx/ctx.go → src/internal/context.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
package ctx

import (
"fmt"
"io/ioutil"

"github.com/silphid/yey/cli/src/internal/helpers"
"gopkg.in/yaml.v2"
)
package yey

// Context represents an execution context for yey (env vars and volumes)
type Context struct {
Expand All @@ -22,24 +14,26 @@ var None = Context{
Mounts: make(map[string]string),
}

// Commented Load because we don't load a context from a file but will read a contextFile to get our Contexts

// Load loads the context from given file
func Load(file string) (Context, error) {
var context Context
if !helpers.PathExists(file) {
return context, nil
}
// func Load(file string) (Context, error) {
// var context Context
// if !helpers.PathExists(file) {
// return context, nil
// }

buf, err := ioutil.ReadFile(file)
if err != nil {
return context, fmt.Errorf("loading context file: %w", err)
}
err = yaml.Unmarshal(buf, &context)
if err != nil {
return context, fmt.Errorf("unmarshalling yaml of context file %q: %w", file, err)
}
// buf, err := ioutil.ReadFile(file)
// if err != nil {
// return context, fmt.Errorf("loading context file: %w", err)
// }
// err = yaml.Unmarshal(buf, &context)
// if err != nil {
// return context, fmt.Errorf("unmarshalling yaml of context file %q: %w", file, err)
// }

return context, nil
}
// return context, nil
// }

// Clone returns a deep-copy of this context
func (c Context) Clone() Context {
Expand Down
11 changes: 5 additions & 6 deletions src/internal/ctxfile/ctxfile.go → src/internal/contextFile.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ctxfile
package yey

import (
"fmt"
"io/ioutil"
"sort"

"github.com/silphid/yey/cli/src/internal/ctx"
"github.com/silphid/yey/cli/src/internal/helpers"
"gopkg.in/yaml.v2"
)
Expand All @@ -17,10 +16,10 @@ const (

// ContextFile represents yey's current config persisted to disk
type ContextFile struct {
ctx.Context
Context
Version int
Parent string
NamedContexts map[string]ctx.Context
NamedContexts map[string]Context
}

// Load loads ContextFile from given file
Expand Down Expand Up @@ -105,13 +104,13 @@ func (cf ContextFile) GetContextNames() ([]string, error) {

// GetContext returns context with given name, or base context
// if name is "base".
func (cf ContextFile) GetContext(name string) (ctx.Context, error) {
func (cf ContextFile) GetContext(name string) (Context, error) {
if name == BaseContextName {
return cf.Context, nil
}
context, ok := cf.NamedContexts[name]
if !ok {
return ctx.Context{}, fmt.Errorf("named context not found: %s", name)
return Context{}, fmt.Errorf("named context not found: %s", name)
}
return context, nil
}
Expand Down
80 changes: 80 additions & 0 deletions src/internal/contextFile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package yey

// // func loadContext(file string) ctx.Context {
// // path := filepath.Join("testdata", file+".yaml")
// // if !helpers.PathExists(path) {
// // panic(fmt.Errorf("context file not found: %q", path))
// // }
// // context, err := ctx.Load(path)
// // if err != nil {
// // panic(fmt.Errorf("loading context from %q: %w", path, err))
// // }
// // return context
// // }

// // func loadConfig(version, baseFile, ctx1Key, ctx1File, ctx2Key, ctx2File string) ContextFile {
// // return ContextFile{
// // Version: version,
// // Base: loadContext(baseFile),
// // NamedContexts: map[string]ctx.Context{
// // ctx1Key: loadContext(ctx1File),
// // ctx2Key: loadContext(ctx2File),
// // },
// // }
// // }

// func TestGetContext(t *testing.T) {
// assert := _assert.New(t)

// sharedConfig := loadConfig("version1", "base1", "ctx1", "ctx1", "ctx2", "ctx2")
// userConfig := loadConfig("version2", "base1b", "ctx1", "ctx1b", "ctx3", "ctx3")

// cases := []struct {
// name string
// expected string
// error string
// }{
// {
// name: "ctx1",
// expected: "base1_base1b_ctx1_ctx1b",
// },
// {
// name: "ctx2",
// expected: "base1_base1b_ctx2",
// },
// {
// name: "ctx3",
// expected: "base1_base1b_ctx3",
// },
// {
// name: "base",
// expected: "base1_base1b",
// },
// {
// name: "none",
// expected: "none",
// },
// {
// name: "unknown",
// error: `context not found "unknown"`,
// },
// }

// for _, c := range cases {
// t.Run(c.name, func(t *testing.T) {
// actual, err := getContext(sharedConfig, userConfig, c.name)

// if c.error != "" {
// assert.NotNil(err)
// assert.Equal(c.error, err.Error())
// return
// }

// expected := loadContext(c.expected)
// assert.NoError(err)
// if diff := deep.Equal(expected, actual); diff != nil {
// t.Error(diff)
// }
// })
// }
// }
5 changes: 2 additions & 3 deletions src/internal/ctx/ctx_test.go → src/internal/context_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctx
package yey

import (
"reflect"
Expand All @@ -12,8 +12,7 @@ func TestClone(t *testing.T) {
assert := _assert.New(t)

original := Context{
Registry: RegistryECR,
Image: "image",
Image: "image",
Env: map[string]string{
"ENV1": "value1",
"ENV2": "value2",
Expand Down
91 changes: 0 additions & 91 deletions src/internal/ctxfile/ctxfile_test.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 6379024

Please sign in to comment.