Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Cmd = &cobra.Command{ Use: "json-to-yaml <json-file>", Short: "Convert JSON file to YAML in place", Long: `Convert a JSON file to YAML format. Creates a new .yaml file and removes the original .json file. Example: slu scripts json-to-yaml example.json Creates: example.yaml Removes: example.json`, Args: cobra.ExactArgs(1), Run: func(c *cobra.Command, args []string) { jsonFilePath := args[0] jsonData, err := os.ReadFile(jsonFilePath) error_utils.HandleError(err, "Failed to read JSON file:") // Unmarshal JSON to generic interface var data interface{} err = json.Unmarshal(jsonData, &data) error_utils.HandleError(err, "Failed to parse JSON file:") yamlData, err := yaml.Marshal(data) error_utils.HandleError(err, "Failed to convert to YAML:") ext := filepath.Ext(jsonFilePath) var yamlFilePath string if ext == ".json" { yamlFilePath = strings.TrimSuffix(jsonFilePath, ext) + ".yaml" } else { yamlFilePath = jsonFilePath + ".yaml" } err = os.WriteFile(yamlFilePath, yamlData, 0644) error_utils.HandleError(err, "Failed to write YAML file:") err = os.Remove(jsonFilePath) error_utils.HandleError(err, "Failed to remove JSON file:") }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.