github.com/amplia-iiot/yutil@v1.0.1-0.20231229120411-5d96a4c5a136/cmd/format.go (about) 1 /* 2 Copyright (c) 2021 amplia-iiot 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in all 12 copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 SOFTWARE. 21 */ 22 23 package cmd 24 25 import ( 26 "errors" 27 "fmt" 28 29 "github.com/amplia-iiot/yutil/internal/io" 30 "github.com/amplia-iiot/yutil/pkg/format" 31 "github.com/spf13/cobra" 32 ) 33 34 type formatOptions struct { 35 outputFile string 36 inPlace bool 37 suffix string 38 } 39 40 var fOptions formatOptions 41 42 // formatCmd represents the format command 43 var formatCmd = &cobra.Command{ 44 Use: "format [FILE...]", 45 Short: "Format a yaml file", 46 Long: `Format a yaml file ordering its keys alphabetically and 47 cleaning it. 48 49 For example: 50 51 yutil format file.yml 52 yutil format file.yml -o file.formatted.yml 53 cat file.yml | yutil format > file.formatted.yml 54 echo "this is not a yaml" | yutil --no-input format file.yml > file.formatted.yml 55 `, 56 Args: func(cmd *cobra.Command, args []string) error { 57 if inPlaceEnabled(cmd) { 58 if canAccessStdin() { 59 return errors.New("stdin not compatible with in place format") 60 } 61 if fOptions.outputFile != "" { 62 return errors.New("output option not compatible with in place format") 63 } 64 } else { 65 if canAccessStdin() && len(args) != 0 { 66 return errors.New("only one yaml can be formatted to output, stdin is active") 67 } else if !canAccessStdin() && len(args) == 0 { 68 if stdinBlocked() { 69 return errors.New("requires one file to be formatted, stdin is blocked") 70 } else { 71 return errors.New("requires one file to be formatted") 72 } 73 } else if !canAccessStdin() && len(args) != 1 { 74 return errors.New("only one file can be formatted to output") 75 } 76 } 77 for _, file := range args { 78 if !io.Exists(file) { 79 return fmt.Errorf("file %s does not exist", file) 80 } 81 } 82 return nil 83 }, 84 Run: func(cmd *cobra.Command, args []string) { 85 var err error 86 if inPlaceEnabled(cmd) { 87 if fOptions.suffix == "" { 88 err = format.FormatFilesInPlace(args) 89 } else { 90 err = format.FormatFilesInPlaceB(args, fOptions.suffix) 91 } 92 } else { 93 var formatted string 94 if canAccessStdin() { 95 formatted, err = format.FormatStdin() 96 } else { 97 formatted, err = format.FormatFile(args[0]) 98 } 99 if err != nil { 100 panic(err) 101 } 102 if len(fOptions.outputFile) > 0 { 103 err = io.WriteToFile(fOptions.outputFile, formatted) 104 } else { 105 err = io.WriteToStdout(formatted) 106 } 107 } 108 if err != nil { 109 panic(err) 110 } 111 }, 112 } 113 114 func init() { 115 rootCmd.AddCommand(formatCmd) 116 117 formatCmd.Flags().StringVarP(&fOptions.outputFile, "output", "o", "", "format yaml to output file instead of stdout (not compatible in place format)") 118 formatCmd.Flags().BoolVarP(&fOptions.inPlace, "in-place", "i", false, "format yaml files in place (makes backup if suffix is supplied)") 119 formatCmd.Flags().StringVarP(&fOptions.suffix, "suffix", "s", "", "format yaml files in place making a backup with the given suffix (-i is not necessary if suffix is passed)") 120 } 121 122 // Whether in place format is enabled 123 func inPlaceEnabled(cmd *cobra.Command) bool { 124 return fOptions.inPlace || cmd.Flags().Changed("suffix") 125 }