@@ -32,64 +32,81 @@ type TextReport struct {
3232}
3333
3434func exportLogic () func (cmd * cobra.Command , args []string ) {
35+ granularityOptions := []string {"lines" , "functions" }
3536 return func (cmd * cobra.Command , args []string ) {
3637 if len (args ) != 1 {
3738 log .Fatalf ("Exactly one profile file is required" )
3839 }
39- for _ , granularity := range []string {"lines" , "functions" } {
40- err , report := generateTextReports (granularity , args [0 ])
41- if err == nil {
42- var w io.Writer
43- // open output file
44- if local {
45- fo , err := os .Create (localFilename )
46- if err != nil {
47- panic (err )
48- }
49- // close fo on exit and check for its returned error
50- defer func () {
51- if err := fo .Close (); err != nil {
52- panic (err )
53- }
54- }()
55- // make a write buffer
56- w = bufio .NewWriter (fo )
57- enc := json .NewEncoder (w )
58- err = enc .Encode (report )
59- if err != nil {
60- log .Fatalf ("Unable to export the profile to local json. Error: %v" , err )
61- } else {
62- log .Printf ("Succesfully exported profile to local file %s" , localFilename )
63- }
40+ if local {
41+ for _ , granularity := range granularityOptions {
42+ err , report := generateTextReports (granularity , args [0 ])
43+ if err == nil {
44+ var w io.Writer
45+ // open output file
46+ fmt .Println ()
47+ localExportLogic (w , report )
6448 } else {
65- postBody , err := json .Marshal (report )
66- if err != nil {
67- log .Fatalf ("An Error Occured %v" , err )
68- }
69- responseBody := bytes .NewBuffer (postBody )
70- endPoint := fmt .Sprintf ("%s/v1/gh/%s/%s/commit/%s/bench/%s/cpu/%s" , codeperfUrl , gitOrg , gitRepo , gitCommit , bench , granularity )
71- resp , err := http .Post (endPoint , "application/json" , responseBody )
72- //Handle Error
73- if err != nil {
74- log .Fatalf ("An Error Occured %v" , err )
75- }
76- defer resp .Body .Close ()
77- //Read the response body
78- body , err := ioutil .ReadAll (resp .Body )
79- if err != nil {
80- log .Fatalln (err )
81- }
82- sb := string (body )
83- log .Printf (sb )
49+ log .Fatal (err )
8450 }
85- } else {
86- log .Fatal (err )
8751 }
52+ } else {
53+ for _ , granularity := range granularityOptions {
54+ err , report := generateTextReports (granularity , args [0 ])
55+ if err == nil {
56+ fmt .Println ()
57+ remoteExportLogic (report , granularity )
58+ } else {
59+ log .Fatal (err )
60+ }
61+ }
62+ log .Printf ("Successfully published profile data. Check it at: %s/#/gh/%s/%s/commit/%s/bench/%s/cpu" , codeperfUrl , gitOrg , gitRepo , gitCommit , bench )
8863 }
8964
9065 }
9166}
9267
68+ func remoteExportLogic (report TextReport , granularity string ) {
69+ postBody , err := json .Marshal (report )
70+ if err != nil {
71+ log .Fatalf ("An Error Occured %v" , err )
72+ }
73+ responseBody := bytes .NewBuffer (postBody )
74+ endPoint := fmt .Sprintf ("%s/v1/gh/%s/%s/commit/%s/bench/%s/cpu/%s" , codeperfUrl , gitOrg , gitRepo , gitCommit , bench , granularity )
75+ resp , err := http .Post (endPoint , "application/json" , responseBody )
76+ //Handle Error
77+ if err != nil {
78+ log .Fatalf ("An Error Occured %v" , err )
79+ }
80+ defer resp .Body .Close ()
81+ //Read the response body
82+ _ , err = ioutil .ReadAll (resp .Body )
83+ if err != nil {
84+ log .Fatalln (err )
85+ }
86+ }
87+
88+ func localExportLogic (w io.Writer , report TextReport ) {
89+ fo , err := os .Create (localFilename )
90+ if err != nil {
91+ panic (err )
92+ }
93+ // close fo on exit and check for its returned error
94+ defer func () {
95+ if err := fo .Close (); err != nil {
96+ panic (err )
97+ }
98+ }()
99+ // make a write buffer
100+ w = bufio .NewWriter (fo )
101+ enc := json .NewEncoder (w )
102+ err = enc .Encode (report )
103+ if err != nil {
104+ log .Fatalf ("Unable to export the profile to local json. Error: %v" , err )
105+ } else {
106+ log .Printf ("Succesfully exported profile to local file %s" , localFilename )
107+ }
108+ }
109+
93110func generateTextReports (granularity string , input string ) (err error , report TextReport ) {
94111 f := baseFlags ()
95112
0 commit comments