Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jarosch <[email protected]>
  • Loading branch information
lukasjarosch committed May 10, 2024
1 parent 4871380 commit b85f7ba
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 45 deletions.
21 changes: 0 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +0,0 @@
The MIT License (MIT)

Copyright (c) 2022 Lukas Jarosch

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ This allows you for example to have way more control over how you want to proces
Skipper is not meant to be a _one-size-fits-all_ solution. The goal of Skipper is to enable
you to create the own - custom built - template and inventory engine, without having to do the heavy lifing.

![](./docs/docs/assets/current_arch_state.svg)

# TODO

- [ ] Core Features
Expand Down
117 changes: 117 additions & 0 deletions cmd/repl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"

"github.com/fatih/color"
"github.com/manifoldco/promptui"

"github.com/lukasjarosch/skipper/v1/expression"
)

var (
errorColor = color.New(color.Bold, color.FgRed)
printfError = errorColor.PrintfFunc()
printlnError = errorColor.PrintlnFunc()

previousInput string
)

// run is the function which is being called on every iteration of the app-loop.
func run() error {
templates := &promptui.PromptTemplates{
Prompt: "{{ . }}",
Valid: ">>> ",
Invalid: "xxx ",
Success: "",
}

p := promptui.Prompt{
Label: "",
Templates: templates,
HideEntered: true,
}

input, err := p.Run()
if err != nil {
return err
}
if len(input) == 0 {
return nil
}

fmt.Printf(">>> %s\n", input)

previousInput = input
input = strings.TrimSpace(input)
inputWords := strings.Split(input, " ")

switch inputWords[0] {
case "expr":
expr(strings.Join(inputWords[1:], " "))
}

return nil
}

func expr(input string) {
input = strings.TrimSpace(input)

// expression.Parse may panic, so be prepared
defer func() {
e := recover()
if e != nil {
printlnError(e)
}
}()

if !strings.HasPrefix(input, "${") {
input = "${ " + input
}
if !strings.HasSuffix(input, "}") {
input = input + " }"
}

expr := expression.Parse(input)
if len(expr) == 0 {
printlnError("no expression present ")
return
}

fmt.Println(color.GreenString("✔ expression:"), expr[0].Text())
}

func main() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
done := make(chan bool, 1)

go func() {
// start app-loop
for {
err := run()
if err != nil {
if !errors.Is(err, promptui.ErrInterrupt) {
fmt.Println("ERR:", err)
}
done <- true
return
}

}
}()

// Wait for a signal or error
select {
case <-sig:
done <- true
os.Exit(0)
case <-done:
return
}
}
26 changes: 26 additions & 0 deletions cmd/skipper/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"log"
"os"

"github.com/urfave/cli/v2"
)

func main() {
app := &cli.App{
Name: "skipper",
Usage: "",
Commands: []*cli.Command{
{
Name: "shell",
Usage: "Starts an interactive skipper shell",
Action: shellAction,
},
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
23 changes: 23 additions & 0 deletions cmd/skipper/shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/c-bata/go-prompt"
"github.com/ryboe/q"
"github.com/urfave/cli/v2"
)

func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "project", Description: "Define the path to the skipper project to load"},
{Text: "expr", Description: "Execute an expression"},
{Text: "help", Description: "Show help for the skipper shell"},
{Text: "exit", Description: "Quit the shell"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}

func shellAction(ctx *cli.Context) error {
input := prompt.Input(">>> ", completer)
q.Q(input)
return nil
}
52 changes: 49 additions & 3 deletions compiled.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,65 @@ import (
"bytes"
"crypto/sha256"
"encoding/gob"
"errors"
"fmt"
"os"
"time"

"github.com/google/uuid"
)

var ErrCompiledInventoryIntegityCheckFailed = fmt.Errorf("CompiledInventory failed to pass the integrity check and cannot be loaded")

// CompiledInventory is the result of calling [Inventory.Compile].
// It is essentially a read-only copy of the source Inventory
// but with all expressions executed and other dependencies (e.g. target handling, secrets, ...) taken care of.
// If one then makes further modifications to the source inventory, a new CompiledInventory artifact must be created.
// The CompiledInventory can then be passed on to make good use of the heap of data :)
// TODO: should this have metadata like buildTimestamp and who build it?
// TODO: should it be possible to actually write this artifact to a file? Skipper could then also work with pre-compiled inventories.
// TODO: This should have capabilities() func like (can-write, can-reveal) to tell the consumer what it can do with it (and to allow more flexibility)
type CompiledInventory struct {
// BuildID is an UUIDv4
BuildID string
// BuildTimestamp is the timestamp of creation of the instance, in UTF.
BuildTimestamp time.Time
// capabilities determine what can be done with this inventory when loaded by a consumer
capability InventoryCapability
scopes map[Scope]*Registry
}

func (c CompiledInventory) HasCapability(cap InventoryCapability) bool {
return c.capability&cap != 0
}

// NewCompiledInventory returns a new CompiledInventory
// with read capability, buildId and buildTimestamp.
func NewCompiledInventory() CompiledInventory {
return CompiledInventory{
BuildID: uuid.New().String(),
BuildTimestamp: time.Now().UTC(),
capability: InventoryRead,
}
}

// InventoryCapability is a binary value.
// Capabilities define what a consumer can do with an Inventory.
type InventoryCapability uint8

const (
// Read (default capability) allows a consumer to read all values within the inventory.
InventoryRead InventoryCapability = 1 << iota
// Write allows the user to modify the inventory
InventoryWrite
// Reveal allows, given the config is provided, that the consumer can reveal secrets
InventoryReveal
)

func (c *InventoryCapability) AddCapability(cap InventoryCapability) {
*c |= cap
}

func (c *InventoryCapability) RemoveCapability(cap InventoryCapability) {
*c &= ^cap
}

func init() {
Expand Down Expand Up @@ -76,7 +122,7 @@ func LoadCompiledInventoryFile(filePath string) (CompiledInventory, error) {
// calculate hash of the actual data and compare
actualHash := sha256.Sum256(fileData)
if !bytes.Equal(hash, actualHash[:]) {
return zero, errors.New("file integrity check failed")
return zero, ErrCompiledInventoryIntegityCheckFailed
}

// decode the remaining fileData
Expand Down
18 changes: 0 additions & 18 deletions compiled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@ import (
"github.com/lukasjarosch/skipper/v1"
)

// func TestWriteCompiledInventoryFile(t *testing.T) {
// err := skipper.WriteCompiledInventoryFile(skipper.CompiledInventory{
// BuildID: "hello-there",
// }, "/tmp/compiled.skipper")
// if err != nil {
// panic(err)
// }
// }
//
// func TestLoadCompiledInventoryFile(t *testing.T) {
// inventory, err := skipper.LoadCompiledInventoryFile("/tmp/compiled.skipper")
// if err != nil {
// panic(err)
// }
//
// q.Q(inventory)
// }

func TestCompiledInventory_WriteAndLoadCompiledInventoryFile(t *testing.T) {
// Create a test inventory
testInventory := skipper.CompiledInventory{
Expand Down
5 changes: 5 additions & 0 deletions docs/docs/assets/current_arch_state.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"regexp"
"strings"

"github.com/ryboe/q"

"github.com/lukasjarosch/skipper/v1/data"
"github.com/lukasjarosch/skipper/v1/expression"
"github.com/lukasjarosch/skipper/v1/graph"
Expand Down Expand Up @@ -256,8 +258,17 @@ func (m *ExpressionManager) ExecuteInput(input string) (data.Value, error) {
}

// ExecuteRegistry will execute all known expressions in the order determined by the DependencyGraph
func (m *ExpressionManager) ExecuteRegistry() error {
return nil
// TODO: Instead of allowing the manager to overwrite the inventory, we could embed the manager into it
// and let it just return a map of path -> newValue and let the inventory handle the overwriting.
func (m *ExpressionManager) ExecuteRegistry() (map[string]data.Value, error) {
pathOrder, err := m.dependencies.TopologicalSort()
if err != nil {
return nil, err
}

q.Q(pathOrder)

return nil, nil
}

// resetDependencyGraph resets the DependencyGraph with the current registry state.
Expand Down
44 changes: 44 additions & 0 deletions expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,47 @@ func TestExpressionManager_ExecuteInput(t *testing.T) {
})
}
}

func TestExpressionManager_ExecuteRegistry(t *testing.T) {
tests := []struct {
name string
pathValues map[string]data.Value
variables map[string]any
expected map[string]data.Value
errExpected error
}{
{
name: "Simple valid path",
pathValues: map[string]data.Value{
"project.name": data.NewValue("skipper"),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
source := mocks.NewMockPathValueSource(t)
source.EXPECT().Values().Return(tt.pathValues)

varSource := mocks.NewMockVariableSource(t)
varSource.EXPECT().GetAll().Return(tt.variables)

for path := range tt.pathValues {
source.EXPECT().GetPath(data.NewPath(path)).Return(tt.pathValues[path], nil)
}

exprManager, err := skipper.NewExpressionManager(source, varSource)
assert.NoError(t, err)

ret, err := exprManager.ExecuteRegistry()

if tt.errExpected != nil {
assert.ErrorContains(t, err, tt.errExpected.Error())
return
}

assert.NoError(t, err)
assert.Equal(t, tt.expected, ret)
})
}
}
Loading

0 comments on commit b85f7ba

Please sign in to comment.