diff --git a/pkg/flags/setfile.go b/pkg/flags/setfile.go new file mode 100644 index 0000000..071be56 --- /dev/null +++ b/pkg/flags/setfile.go @@ -0,0 +1,50 @@ +// 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 ( + "fmt" + "github.com/go-errors/errors" + "github.com/micovery/apigee-yaml-toolkit/pkg/values" + "os" + "strings" +) + +type SetFile struct { + Data *values.Map +} + +func NewSetFile(data *values.Map) SetString { + return SetString{Data: data} +} + +func (v *SetFile) String() string { + return fmt.Sprintf("%v", v.Data) +} + +func (v *SetFile) Set(entry string) error { + key, filePath, found := strings.Cut(entry, "=") + if !found { + return errors.Errorf("missing file path in set-file for key=%s", key) + } + + fileText, err := os.ReadFile(filePath) + if err != nil { + return errors.New(err) + } + + v.Data.Set(key, fileText) + return nil +}