go-hep.org/x/hep@v0.38.1/fwk/cmd/fwk-app/cmd_run.go (about) 1 // Copyright ©2017 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "flag" 9 "fmt" 10 "os" 11 "os/exec" 12 "path/filepath" 13 14 "codeberg.org/gonuts/commander" 15 "go-hep.org/x/hep/fwk/utils/builder" 16 ) 17 18 func fwk_make_cmd_run() *commander.Command { 19 cmd := &commander.Command{ 20 Run: fwk_run_cmd_run, 21 UsageLine: "run [options] <config.go> [<config2.go> [...]]", 22 Short: "run a fwk job", 23 Long: ` 24 run runs a fwk-based job. 25 26 ex: 27 $ fwk-app run config.go 28 $ fwk-app run config1.go config2.go 29 $ fwk-app run ./some-dir 30 $ fwk-app run -l=INFO -nprocs=4 -evtmax=-1 config.go 31 `, 32 Flag: *flag.NewFlagSet("fwk-app-run", flag.ExitOnError), 33 } 34 35 cmd.Flag.String("o", "", "name of the resulting binary (default=name of parent directory)") 36 cmd.Flag.Bool("k", false, "whether to keep the resulting binary after a successful run") 37 38 // flags passed to sub-process 39 cmd.Flag.String("l", "INFO", "log level (DEBUG|INFO|WARN|ERROR)") 40 cmd.Flag.Int("evtmax", -1, "number of events to process") 41 cmd.Flag.Int("nprocs", 0, "number of concurrent events to process") 42 cmd.Flag.Bool("cpu-prof", false, "enable CPU profiling") 43 return cmd 44 } 45 46 func fwk_run_cmd_run(cmd *commander.Command, args []string) error { 47 var err error 48 49 n := "fwk-app-" + cmd.Name() 50 51 subargs := make([]string, 0, len(args)) 52 for _, nn := range []string{"l", "evtmax", "nprocs", "cpu-prof"} { 53 val := cmd.Flag.Lookup(nn) 54 if val == nil { 55 continue 56 } 57 subargs = append( 58 subargs, 59 fmt.Sprintf("-"+nn+"=%v", val.Value.(flag.Getter).Get()), 60 ) 61 } 62 63 fnames := make([]string, 0, len(args)) 64 for _, arg := range args { 65 if arg == "" || arg == "--" { 66 continue 67 } 68 69 if arg[0] == '-' { 70 subargs = append(subargs, arg) 71 continue 72 } 73 74 fnames = append(fnames, arg) 75 } 76 77 if len(fnames) <= 0 { 78 return fmt.Errorf("%s: you need to give a list of files or a directory", n) 79 } 80 81 bldr, err := builder.NewBuilder(fnames...) 82 if err != nil { 83 return err 84 } 85 86 if o := cmd.Lookup("o").(string); o != "" { 87 bldr.Name = o 88 } 89 90 err = bldr.Build() 91 if err != nil { 92 return err 93 } 94 95 bin := bldr.Name 96 if !filepath.IsAbs(bin) { 97 pwd, err := os.Getwd() 98 if err != nil { 99 return err 100 } 101 bin = filepath.Join(pwd, bin) 102 } 103 104 if !cmd.Lookup("k").(bool) { 105 defer os.Remove(bin) 106 } 107 108 sub := exec.Command(bin, subargs...) 109 sub.Stdout = os.Stdout 110 sub.Stderr = os.Stderr 111 sub.Stdin = os.Stdin 112 113 err = sub.Run() 114 115 return err 116 }