github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/commands/resources/logs/query.go (about) 1 package logs 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "os" 8 "path" 9 10 "github.com/taubyte/tau-cli/cli/common" 11 patrickClient "github.com/taubyte/tau-cli/singletons/patrick_client" 12 "github.com/urfave/cli/v2" 13 ) 14 15 func (link) Query() common.Command { 16 return common.Create( 17 &cli.Command{ 18 Flags: []cli.Flag{ 19 &cli.StringFlag{ 20 Name: "jid", 21 Usage: "(required) job id of log to query", 22 }, 23 &cli.StringFlag{ 24 Name: "output", 25 Aliases: []string{"o"}, 26 Usage: "set output dir of log files (defaults to terminal)", 27 }, 28 }, 29 Action: query, 30 }, 31 ) 32 } 33 34 func query(ctx *cli.Context) error { 35 patrickC, err := patrickClient.Load() 36 if err != nil { 37 return err 38 } 39 40 jobId := ctx.String("jid") 41 if len(jobId) < 1 { 42 return errors.New("job id not set") 43 } 44 45 job, err := patrickC.Job(jobId) 46 if err != nil { 47 return err 48 } 49 50 outputDir := ctx.String("output") 51 var writeFile bool 52 if len(outputDir) > 0 { 53 if err = os.MkdirAll(outputDir, 0751); err != nil { 54 return err 55 } 56 57 writeFile = true 58 } 59 60 for resourceId, cid := range job.Logs { 61 log, err := patrickC.LogFile(jobId, cid) 62 if err != nil { 63 return err 64 } 65 66 data, err := io.ReadAll(log) 67 if err != nil { 68 return err 69 } 70 71 if writeFile { 72 if err = os.WriteFile(path.Join(outputDir, resourceId), data, 0666); err != nil { 73 return err 74 } 75 } else { 76 fmt.Printf("-----------------------------------------------------------------------------\nResource: %s\n\n%s\n\n", resourceId, string(data)) 77 } 78 } 79 80 return nil 81 }