Skip to content

Commit 11ec8c7

Browse files
authored
fix: determine stdin input from arguments (#46)
1 parent 99fbab2 commit 11ec8c7

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

cmd/push.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func runPushForCategory(cmd *cobra.Command, args []string, resolver *files.PathR
3939
hubClient, err := hub.NewClient()
4040
errutil.Check(err)
4141

42-
localSource, err := getSrc(cmd, args)
42+
localSource, err := getSrc(args)
4343
errutil.Check(err)
4444

4545
destinationOverride, err := cmd.Flags().GetString("destination")
@@ -181,22 +181,22 @@ func init() {
181181
pushCmd.AddCommand(NewPushProjectCmd())
182182
}
183183

184-
func getSrc(cmd *cobra.Command, args []string) (string, error) {
185-
if shouldUseStdin() {
186-
log.Debug("Detected stdin, saving it to a temporary file...")
184+
func getSrc(args []string) (string, error) {
185+
input := args[0]
186+
if shouldUseStdin(input) {
187+
log.Debug("Detected stdin, saving it to a temporary file...\n")
187188
return saveStdinToTempFile()
188189
}
189190

190-
return args[0], nil
191+
return input, nil
191192
}
192193

193-
func shouldUseStdin() bool {
194-
stat, err := os.Stdin.Stat()
195-
if err != nil {
196-
return false
194+
func shouldUseStdin(input string) bool {
195+
if input == "-" || input == "/dev/stdin" {
196+
return true
197197
}
198198

199-
return (stat.Mode() & os.ModeCharDevice) == 0
199+
return false
200200
}
201201

202202
func saveStdinToTempFile() (string, error) {

pkg/api/signed_url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (u *SignedURL) put(client *http.Client, artifact *Artifact) error {
7676
// If the file has no bytes, we need to use http.NoBody
7777
// See https://cs.opensource.google/go/go/+/refs/tags/go1.18.2:src/net/http/request.go;l=920
7878
if fileInfo.Size() == 0 {
79-
log.Debugf("'%s' is empty.", artifact.LocalPath)
79+
log.Debugf("'%s' is empty.\n", artifact.LocalPath)
8080
contentBody = http.NoBody
8181
}
8282

test/integration/integration_test.go

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ func Test__Push(t *testing.T) {
208208
os.RemoveAll(tmpDir)
209209
})
210210

211-
t.Run("push using input from a pipe", func(t *testing.T) {
211+
t.Run("push using input from a pipe using -", func(t *testing.T) {
212212
if runtime.GOOS == "windows" {
213213
t.Skip()
214214
}
215215

216-
command := fmt.Sprintf("echo -n \"hello from pipe\" | %s push job - -d from-pipe.txt -v", getBinaryPath(rootFolder))
216+
command := fmt.Sprintf("echo -n \"hello from dash\" | %s push job - -d from-dash.txt -v", getBinaryPath(rootFolder))
217217
tmpScript, err := createTempScript(command)
218218
if !assert.Nil(t, err) {
219219
return
@@ -224,14 +224,71 @@ func Test__Push(t *testing.T) {
224224
assert.Contains(t, output, "Detected stdin, saving it to a temporary file...")
225225
assert.Contains(t, output, "Successfully pushed artifact for current job")
226226

227-
output, err = executeCommand("pull", rootFolder, []string{"from-pipe.txt"})
227+
output, err = executeCommand("pull", rootFolder, []string{"from-dash.txt"})
228228
assert.Nil(t, err)
229229
assert.Contains(t, output, "Successfully pulled artifact for current job")
230230

231-
fileContents, _ := ioutil.ReadFile("from-pipe.txt")
232-
assert.Equal(t, "hello from pipe", string(fileContents))
231+
fileContents, _ := ioutil.ReadFile("from-dash.txt")
232+
assert.Equal(t, "hello from dash", string(fileContents))
233233

234-
os.Remove("from-pipe.txt")
234+
os.Remove("from-dash.txt")
235+
os.Remove(tmpScript)
236+
})
237+
238+
t.Run("push using input from a pipe using /dev/stdin", func(t *testing.T) {
239+
if runtime.GOOS == "windows" {
240+
t.Skip()
241+
}
242+
243+
command := fmt.Sprintf("echo -n \"hello from /dev/stdin\" | %s push job /dev/stdin -d from-dev-stdin.txt -v", getBinaryPath(rootFolder))
244+
tmpScript, err := createTempScript(command)
245+
if !assert.Nil(t, err) {
246+
return
247+
}
248+
249+
output, err := executeTempScript(tmpScript)
250+
assert.Nil(t, err)
251+
assert.Contains(t, output, "Detected stdin, saving it to a temporary file...")
252+
assert.Contains(t, output, "Successfully pushed artifact for current job")
253+
254+
output, err = executeCommand("pull", rootFolder, []string{"from-dev-stdin.txt"})
255+
assert.Nil(t, err)
256+
assert.Contains(t, output, "Successfully pulled artifact for current job")
257+
258+
fileContents, _ := ioutil.ReadFile("from-dev-stdin.txt")
259+
assert.Equal(t, "hello from /dev/stdin", string(fileContents))
260+
261+
os.Remove("from-dev-stdin.txt")
262+
os.Remove(tmpScript)
263+
})
264+
265+
t.Run("input coming from pipe but file is used", func(t *testing.T) {
266+
if runtime.GOOS == "windows" {
267+
t.Skip()
268+
}
269+
270+
tmpFile, _ := ioutil.TempFile("", "*")
271+
tmpFile.Write([]byte("hello from file"))
272+
273+
command := fmt.Sprintf("echo -n \"hello from pipe\" | %s push job %s -d not-from-pipe.txt -v", getBinaryPath(rootFolder), tmpFile.Name())
274+
tmpScript, err := createTempScript(command)
275+
if !assert.Nil(t, err) {
276+
return
277+
}
278+
279+
output, err := executeTempScript(tmpScript)
280+
assert.Nil(t, err)
281+
assert.NotContains(t, output, "Detected stdin, saving it to a temporary file...")
282+
assert.Contains(t, output, "Successfully pushed artifact for current job")
283+
284+
output, err = executeCommand("pull", rootFolder, []string{"not-from-pipe.txt"})
285+
assert.Nil(t, err)
286+
assert.Contains(t, output, "Successfully pulled artifact for current job")
287+
288+
fileContents, _ := ioutil.ReadFile("not-from-pipe.txt")
289+
assert.Equal(t, "hello from file", string(fileContents))
290+
291+
os.Remove("not-from-pipe.txt")
235292
os.Remove(tmpScript)
236293
})
237294

0 commit comments

Comments
 (0)