@@ -32,64 +32,81 @@ type TextReport struct {
32
32
}
33
33
34
34
func exportLogic () func (cmd * cobra.Command , args []string ) {
35
+ granularityOptions := []string {"lines" , "functions" }
35
36
return func (cmd * cobra.Command , args []string ) {
36
37
if len (args ) != 1 {
37
38
log .Fatalf ("Exactly one profile file is required" )
38
39
}
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 )
64
48
} 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 )
84
50
}
85
- } else {
86
- log .Fatal (err )
87
51
}
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 )
88
63
}
89
64
90
65
}
91
66
}
92
67
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
+
93
110
func generateTextReports (granularity string , input string ) (err error , report TextReport ) {
94
111
f := baseFlags ()
95
112
0 commit comments