Skip to content

Commit

Permalink
have readResourceInput read YAML apart from viper config
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbloss committed Dec 11, 2023
1 parent efcdee9 commit 2df34c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 25 additions & 6 deletions src/cmd/input.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cmd

import (
"bytes"
"os"

"github.com/rs/zerolog/log"

"github.com/creasty/defaults"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

var dataFile string
Expand All @@ -30,13 +31,31 @@ func readInputConfig() {
}

func readResourceInput[T any]() (*T, error) {
readInputConfig()
var evt T
viper.Unmarshal(&evt)
if err := defaults.Set(&evt); err != nil {
var err error
var resource T
var yamlData []byte

switch dataFile {
case ".":
yamlData, err = os.ReadFile("./data.yaml")
case "-":
if isStdInFromTerminal() {
log.Info().Msg("Reading input directly from command line... Press CTRL+D to stop typing")
}
buf := bytes.Buffer{}
_, err = buf.ReadFrom(os.Stdin)
yamlData = buf.Bytes()
default:
yamlData, err = os.ReadFile(dataFile)
}
if err != nil {
return nil, err
}

if err := yaml.Unmarshal(yamlData, &resource); err != nil {
return nil, err
}
return &evt, nil
return &resource, nil
}

func isStdInFromTerminal() bool {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var createTeamCmd = &cobra.Command{
Short: "Create a team",
Example: `opslevel create team my-team
cat << EOF | opslevel create team my-team" -f -
cat << EOF | opslevel create team my-team -f -
managerEmail: "[email protected]"
parentTeam:
alias: "parent-team"
Expand Down

0 comments on commit 2df34c1

Please sign in to comment.