github.com/stillson/go-wf@v0.0.0-20240502203501-5781d3fae028/main.go (about) 1 /* 2 * Copyright (c) 2024. Christopher Stillson <stillson@gmail.com> 3 * 4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 * 6 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 9 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10 */ 11 12 package main 13 14 import ( 15 "bufio" 16 "flag" 17 "fmt" 18 "os" 19 "time" 20 21 "github.com/stillson/go-wf/termui" 22 23 "github.com/fatih/color" 24 "github.com/stillson/go-wf/executor" 25 "github.com/stillson/go-wf/rcfile" 26 "github.com/stillson/go-wf/rcparse" 27 ) 28 29 const ( 30 VERSION = "0.0.2" 31 ) 32 33 func vprint(verbose bool, printGreen bool, format string, inputs ...any) { 34 if !verbose { 35 return 36 } 37 if printGreen { 38 _, green := termui.GetColorPrints() 39 _, _ = green.Printf(format, inputs...) 40 return 41 } 42 fmt.Printf(format, inputs...) 43 } 44 45 func printRules(ourRcFile *rcparse.YRCfile) { 46 rules, err := ourRcFile.ListRules() 47 if err != nil { 48 _, _ = fmt.Fprintf(os.Stderr, "%v", err) 49 os.Exit(7) 50 } 51 52 for _, rule := range rules { 53 fmt.Printf("%s\n", rule) 54 } 55 } 56 57 func dumpRulesFile(f string, verb bool) { 58 red := color.New(color.FgHiRed) 59 60 var fp, err = os.Open(f) //nolint:gosec 61 if err != nil { 62 _, _ = red.Printf("Error reading rcfile:%v\n", err) 63 os.Exit(2) 64 } 65 defer func() { 66 _ = fp.Close() 67 }() 68 69 vprint(verb, false, "---\n") 70 71 scanner := bufio.NewScanner(fp) 72 for scanner.Scan() { 73 fmt.Println(scanner.Text()) 74 } 75 err = scanner.Err() 76 if err != nil { 77 _, _ = red.Printf("error dumping file %v\n", err) 78 } 79 } 80 81 func ParseArgs() (*bool, *bool, *bool, *string, *bool) { 82 // flags 83 versionQ := flag.Bool("v", false, "Version of this program") 84 verboseQ := flag.Bool("V", false, "Verbose output") 85 timeQ := flag.Bool("t", false, "Time the command") 86 dumpQ := flag.Bool("d", false, "Dump contents of workflow file") 87 wfFile := flag.String("f", ".workflow.yaml", "Name of workflow file") 88 ruleQ := flag.Bool("r", false, "Print available rules") 89 90 flag.Parse() 91 92 if *versionQ { 93 fmt.Printf("wf version %v\n", VERSION) 94 os.Exit(0) 95 } 96 return verboseQ, timeQ, dumpQ, wfFile, ruleQ 97 } 98 99 func main() { 100 verboseQ, timeQ, dumpQ, wfFile, ruleQ := ParseArgs() 101 102 vprint(*verboseQ, false, "Verbose is on\n") 103 vprint(*timeQ && *verboseQ, false, "Timing enabled\n") 104 vprint(*dumpQ && *verboseQ, false, "Dumping workflow file\n") 105 vprint(*verboseQ, false, "File to search for: %s\n", *wfFile) 106 107 // set up colors 108 red, green := termui.GetColorPrints() 109 110 // get filename of rcfile 111 f, err := rcfile.GetRCFile(*wfFile) 112 if err != nil { 113 _, _ = red.Printf("Error getting rcfile:%v\n", err) 114 os.Exit(1) 115 } 116 vprint(*verboseQ, false, "Actual file found: %s\n", f) 117 118 if *dumpQ { 119 dumpRulesFile(f, *verboseQ) 120 return 121 } 122 123 ourRcFile, err := rcparse.NewYRCFile(f) 124 if err != nil { 125 _, _ = red.Printf("Error parsing rcfile:%v\n", err) 126 os.Exit(2) 127 } 128 vprint(*verboseQ, false, "\tRC: %v\n", ourRcFile) 129 130 if *ruleQ { 131 printRules(ourRcFile) 132 return 133 } 134 135 rule := flag.Arg(0) 136 vprint(*verboseQ, false, "rule is: %s\n", rule) 137 138 var now int64 139 if *timeQ { 140 now = time.Now().UnixMicro() 141 vprint(*verboseQ, true, "Start time: %v\n", now) 142 } 143 144 localExec := executor.NewLocalExec("main") 145 rv, err := localExec.Run(rule, ourRcFile) 146 if err != nil { 147 _, _ = red.Printf("%v\n", err) 148 } 149 150 if *timeQ { 151 end := time.Now().UnixMicro() 152 vprint(*verboseQ, true, "End time: %v\n", end) 153 _, _ = green.Printf("Total Time in µsecs: %v\n", end-now) 154 } 155 156 if rv != 0 { 157 _, _ = red.Printf("\nProcess exited with %v\n", rv) 158 } 159 160 os.Exit(rv) 161 }