@@ -10,10 +10,14 @@ import (
1010 "strings"
1111
1212 "github.com/cli/cli/context"
13+ "github.com/cli/cli/pkg/cmdutil"
14+ "github.com/cli/cli/pkg/iostreams"
1315 "github.com/spf13/cobra"
1416)
1517
1618type ApiOptions struct {
19+ IO * iostreams.IOStreams
20+
1721 RequestMethod string
1822 RequestMethodPassed bool
1923 RequestPath string
@@ -54,9 +58,12 @@ on the format of the value:
5458 opts .RequestPath = args [0 ]
5559 opts .RequestMethodPassed = c .Flags ().Changed ("method" )
5660
61+ // TODO: pass in via caller
62+ opts .IO = iostreams .System ()
63+
5764 opts .HttpClient = func () (* http.Client , error ) {
5865 ctx := context .New ()
59- token , err := ctx .AuthLogin ()
66+ token , err := ctx .AuthToken ()
6067 if err != nil {
6168 return nil , err
6269 }
@@ -108,12 +115,16 @@ func apiRun(opts *ApiOptions) error {
108115 }
109116 defer resp .Body .Close ()
110117
111- // TODO: make stdout configurable for tests
112- _ , err = io .Copy (os .Stdout , resp .Body )
118+ _ , err = io .Copy (opts .IO .Out , resp .Body )
113119 if err != nil {
114120 return err
115121 }
116122
123+ // TODO: detect GraphQL errors
124+ if resp .StatusCode > 299 {
125+ return cmdutil .SilentError
126+ }
127+
117128 return nil
118129}
119130
@@ -131,7 +142,7 @@ func parseFields(opts *ApiOptions) (map[string]interface{}, error) {
131142 if err != nil {
132143 return params , err
133144 }
134- value , err := magicFieldValue (strValue )
145+ value , err := magicFieldValue (strValue , opts . IO . In )
135146 if err != nil {
136147 return params , fmt .Errorf ("error parsing %q value: %w" , key , err )
137148 }
@@ -148,12 +159,12 @@ func parseField(f string) (string, string, error) {
148159 return f [0 :idx ], f [idx + 1 :], nil
149160}
150161
151- func magicFieldValue (v string ) (interface {}, error ) {
162+ func magicFieldValue (v string , stdin io. ReadCloser ) (interface {}, error ) {
152163 if strings .HasPrefix (v , "@" ) {
153- return readUserFile (v [1 :])
164+ return readUserFile (v [1 :], stdin )
154165 }
155166
156- if n , err := strconv .Atoi (v ); err ! = nil {
167+ if n , err := strconv .Atoi (v ); err = = nil {
157168 return n , nil
158169 }
159170
@@ -169,18 +180,17 @@ func magicFieldValue(v string) (interface{}, error) {
169180 }
170181}
171182
172- func readUserFile (fn string ) ([]byte , error ) {
183+ func readUserFile (fn string , stdin io. ReadCloser ) ([]byte , error ) {
173184 var r io.ReadCloser
174185 if fn == "-" {
175- // TODO: make stdin configurable for tests
176- r = os .Stdin
186+ r = stdin
177187 } else {
178188 var err error
179189 r , err = os .Open (fn )
180190 if err != nil {
181191 return nil , err
182192 }
183- defer r .Close ()
184193 }
194+ defer r .Close ()
185195 return ioutil .ReadAll (r )
186196}
0 commit comments