mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-20 06:51:39 -04:00
* [COMPLIANCE] Add/Update Copyright Headers * make generate update * make generate --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: sanya <[email protected]> Co-authored-by: Sanya <[email protected]>
38 lines
641 B
Go
38 lines
641 B
Go
// Copyright IBM Corp. 2024, 2026
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package kvflag
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// FlagJSON is a flag.Value implementation for parsing user variables
|
|
// from the command-line using JSON files.
|
|
type FlagJSON map[string]string
|
|
|
|
func (v *FlagJSON) String() string {
|
|
return ""
|
|
}
|
|
|
|
func (v *FlagJSON) Set(raw string) error {
|
|
f, err := os.Open(raw)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
if *v == nil {
|
|
*v = make(map[string]string)
|
|
}
|
|
|
|
if err := json.NewDecoder(f).Decode(v); err != nil {
|
|
return fmt.Errorf(
|
|
"Error reading variables in '%s': %s", raw, err)
|
|
}
|
|
|
|
return nil
|
|
}
|