Skip to content
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/goccy/go-yaml v1.17.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
46 changes: 46 additions & 0 deletions script/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package script

import (
"bytes"
"errors"
"fmt"
"io/fs"
Expand All @@ -19,6 +20,7 @@ import (
"time"

"github.com/cilium/hive/script/internal/diff"
yaml "github.com/goccy/go-yaml"
"github.com/spf13/pflag"
"golang.org/x/term"
)
Expand Down Expand Up @@ -53,6 +55,7 @@ func DefaultCmds() map[string]Cmd {
"symlink": Symlink(),
"wait": Wait(),
"break": Break(),
"yq": Yaml(),
}
}

Expand Down Expand Up @@ -131,6 +134,49 @@ func Cat() Cmd {
})
}

func Yaml() Cmd {
return Command(
CmdUsage{
Summary: "concatenate files and print to the script's stdout buffer",
Args: "files...",
},
func(s *State, args ...string) (WaitFunc, error) {
if len(args) == 0 {
return nil, ErrUsage
}

path := s.Path(args[0])
b, err := os.ReadFile(path)
var buf strings.Builder
buf.Write(b)
errc := make(chan error)
if err != nil {
errc <- err
} else {
close(errc)
}

wait := func(*State) (stdout, stderr string, err error) {
err = <-errc
if err != nil {
return "", "", err
}
path, err := yaml.PathString(args[1])
if err != nil {
return "", "", err
}
var out any
if err := path.Read(strings.NewReader(buf.String()), &out); err != nil {
return "", "", err
}
outBuf := &bytes.Buffer{}
yaml.NewEncoder(outBuf).Encode(out)
return outBuf.String(), "", err
}
return wait, nil
})
}

// Cd changes the current working directory.
func Cd() Cmd {
return Command(
Expand Down
17 changes: 17 additions & 0 deletions script/scripttest/testdata/yaml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
yq pets.yaml '$.info.labels'
cmp stdout labels.yaml
-- labels.yaml --
labels:
foo: bar
-- pets.yaml --
info:
name: "pets"
desc: "info about pets"
version: 7
labels:
foo: bar
dogs:
- "spot"
- "jeff"
cats:
- "mittens"
202 changes: 202 additions & 0 deletions vendor/github.com/cilium/stream/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/cilium/stream/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading