Skip to content

Commit

Permalink
feat: add support for --set-json
Browse files Browse the repository at this point in the history
This flag allows you to set any arbitrary JSON content within Values
  • Loading branch information
micovery committed Apr 15, 2024
1 parent 282d8fb commit ccb0bf4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/flags/setjson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flags

import (
"encoding/json"
"fmt"
"github.com/go-errors/errors"
"github.com/micovery/apigee-yaml-toolkit/pkg/values"
"strings"
)

type SetJSON struct {
Data *values.Map
}

func NewSetJSON(data *values.Map) SetJSON {
return SetJSON{Data: data}
}

func (v *SetJSON) String() string {
return fmt.Sprintf("%v", v.Data)
}

func (v *SetJSON) Set(entry string) error {
key, jsonText, found := strings.Cut(entry, "=")
if !found {
return errors.Errorf("missing JSON text in set-json for key=%s", key)
}

wrappedJSONText := fmt.Sprintf(`{"JSON":%s}`, jsonText)
type Wrapper struct {
JSON any `yaml:"Data"`
}

wrapper := Wrapper{JSON: nil}
err := json.Unmarshal([]byte(wrappedJSONText), &wrapper)
if err != nil {
return errors.New(err)
}

v.Data.Set(key, wrapper.JSON)
return nil
}
2 changes: 2 additions & 0 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func GetRenderFlags(printVersion func(), printUsage func()) (*Flags, error) {
setOAS := flags.NewSetOAS(rFlags.Values)
setGraphQL := flags.NewSetGraphQL(rFlags.Values)
setGRPC := flags.NewSetGRPC(rFlags.Values)
setJSON := flags.NewSetJSON(rFlags.Values)

flag.BoolVar(&rFlags.Version, "version", false, "(optional) prints version text")
flag.BoolVar(&rFlags.Help, "help", false, `(optional) prints additional help text`)
Expand All @@ -377,6 +378,7 @@ func GetRenderFlags(printVersion func(), printUsage func()) (*Flags, error) {
flag.Var(&setOAS, "set-oas", "(optional,0..*) sets context key/value where value is an OpenAPI spec of a file e.g. \"my_oas=petstore.yaml\"")
flag.Var(&setGRPC, "set-grpc", "(optional,0..*) sets context key/value where value is a gRPC proto of a file e.g. \"my_proto=greeter.proto\"")
flag.Var(&setGraphQL, "set-graphql", "(optional,0..*) sets context key/value where value is a GraphQL schema of a file e.g. \"my_schema=resorts.graphql\"")
flag.Var(&setJSON, "set-json", "(optional,0..*) sets context key/value where value is JSON e.g. 'servers=[\"server1\",\"server2\"]'")

flag.Parse()

Expand Down

0 comments on commit ccb0bf4

Please # to comment.