Skip to content

Commit

Permalink
switch to aws-sdk-go-v2
Browse files Browse the repository at this point in the history
pass ctx for all
  • Loading branch information
fujiwara committed Jun 4, 2024
1 parent 02f2e0f commit 8b81ab9
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 82 deletions.
3 changes: 2 additions & 1 deletion cmd/stretcher/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -73,7 +74,7 @@ func main() {

log.Println("stretcher version:", version)
stretcher.Version = version
err := stretcher.Run(conf)
err := stretcher.Run(context.TODO(), conf)
if err != nil {
log.Println(err)
if os.Getenv("CONSUL_INDEX") != "" {
Expand Down
17 changes: 9 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stretcher

import (
"bytes"
"context"
"fmt"
"io"
"log"
Expand All @@ -20,20 +21,20 @@ type Commands struct {

type CommandLines []CommandLine

func (cs CommandLines) Invoke() error {
func (cs CommandLines) Invoke(ctx context.Context) error {
for _, c := range cs {
err := c.Invoke()
err := c.Invoke(ctx)
if err != nil {
return err
}
}
return nil
}

func (cs CommandLines) InvokePipe(src *bytes.Buffer) error {
func (cs CommandLines) InvokePipe(ctx context.Context, src *bytes.Buffer) error {
for _, c := range cs {
buf := bytes.NewBuffer(src.Bytes())
err := c.InvokePipe(buf)
err := c.InvokePipe(ctx, buf)
if err != nil {
return err
}
Expand All @@ -47,9 +48,9 @@ func (c CommandLine) String() string {
return string(c)
}

func (c CommandLine) Invoke() error {
func (c CommandLine) Invoke(ctx context.Context) error {
log.Println("invoking command:", c.String())
out, err := exec.Command("sh", "-c", c.String()).CombinedOutput()
out, err := exec.CommandContext(ctx, "sh", "-c", c.String()).CombinedOutput()
if len(out) > 0 {
log.Println(string(out))
}
Expand All @@ -59,9 +60,9 @@ func (c CommandLine) Invoke() error {
return nil
}

func (c CommandLine) InvokePipe(src io.Reader) error {
func (c CommandLine) InvokePipe(ctx context.Context, src io.Reader) error {
log.Println("invoking command:", c.String())
cmd := exec.Command("sh", "-c", c.String())
cmd := exec.CommandContext(ctx, "sh", "-c", c.String())
stdin, err := cmd.StdinPipe()
if err != nil {
return err
Expand Down
13 changes: 9 additions & 4 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stretcher_test

import (
"bytes"
"context"
"fmt"
"os"
"strings"
Expand All @@ -12,6 +13,7 @@ import (
)

func TestCommandLines(t *testing.T) {
ctx := context.Background()
stretcher.LogBuffer.Reset()
now := time.Now()
ymd := now.Format("20060102")
Expand All @@ -20,7 +22,7 @@ func TestCommandLines(t *testing.T) {
stretcher.CommandLine("date +%Y%m%d"),
stretcher.CommandLine("date +%H%M"),
}
cmdlines.Invoke()
cmdlines.Invoke(ctx)
output := stretcher.LogBuffer.String()
if strings.Index(output, ymd) == -1 {
t.Error("invalid output", output)
Expand All @@ -31,6 +33,7 @@ func TestCommandLines(t *testing.T) {
}

func TestCommandLinesPipe(t *testing.T) {
ctx := context.Background()
stretcher.LogBuffer.Reset()
var buf bytes.Buffer
for i := 0; i < 10; i++ {
Expand All @@ -45,7 +48,7 @@ func TestCommandLinesPipe(t *testing.T) {
}
defer os.Remove("test/tmp/cmdoutput")

err := cmdlines.InvokePipe(&buf)
err := cmdlines.InvokePipe(ctx, &buf)
if err != nil {
t.Error(err)
}
Expand All @@ -60,11 +63,12 @@ func TestCommandLinesPipe(t *testing.T) {
}

func TestCommandLinesFail(t *testing.T) {
ctx := context.Background()
stretcher.LogBuffer.Reset()
cmdlines := stretcher.CommandLines{
stretcher.CommandLine("echo 'FOO'; echo 'BAR' 2>&1; false"),
}
err := cmdlines.Invoke()
err := cmdlines.Invoke(ctx)
if err == nil {
t.Error("false command must fail")
}
Expand All @@ -81,6 +85,7 @@ func TestCommandLinesFail(t *testing.T) {
}

func TestCommandLinesPipeIgnoreEPIPE(t *testing.T) {
ctx := context.Background()
stretcher.LogBuffer.Reset()
var buf bytes.Buffer
for i := 0; i < 1025; i++ {
Expand All @@ -90,7 +95,7 @@ func TestCommandLinesPipeIgnoreEPIPE(t *testing.T) {
stretcher.CommandLine("echo ok"),
}

err := cmdlines.InvokePipe(&buf)
err := cmdlines.InvokePipe(ctx, &buf)
if err != nil {
t.Error(err)
}
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ go 1.12

require (
cloud.google.com/go/storage v1.29.0
github.com/aws/aws-sdk-go v1.44.285
github.com/aws/aws-sdk-go-v2 v1.27.1
github.com/aws/aws-sdk-go-v2/config v1.27.17
github.com/aws/aws-sdk-go-v2/service/s3 v1.54.4
github.com/dustin/go-humanize v1.0.0
github.com/fujiwara/shapeio v0.0.0-20170602072123-c073257dd745
github.com/fujiwara/shapeio v1.0.0
github.com/google/go-github v17.0.0+incompatible // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/go-version v1.0.0 // indirect
Expand Down
50 changes: 39 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,42 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0=
github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4xei5aX110hRiI=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
github.com/aws/aws-sdk-go v1.44.285 h1:rgoWYl+NdmKzRgoi/fZLEtGXOjCkcWIa5jPH02Uahdo=
github.com/aws/aws-sdk-go v1.44.285/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go-v2 v1.27.1 h1:xypCL2owhog46iFxBKKpBcw+bPTX/RJzwNj8uSilENw=
github.com/aws/aws-sdk-go-v2 v1.27.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg=
github.com/aws/aws-sdk-go-v2/config v1.27.17 h1:L0JZN7Gh7pT6u5CJReKsLhGKparqNKui+mcpxMXjDZc=
github.com/aws/aws-sdk-go-v2/config v1.27.17/go.mod h1:MzM3balLZeaafYcPz8IihAmam/aCz6niPQI0FdprxW0=
github.com/aws/aws-sdk-go-v2/credentials v1.17.17 h1:b3Dk9uxQByS9sc6r0sc2jmxsJKO75eOcb9nNEiaUBLM=
github.com/aws/aws-sdk-go-v2/credentials v1.17.17/go.mod h1:e4khg9iY08LnFK/HXQDWMf9GDaiMari7jWPnXvKAuBU=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.4 h1:0cSfTYYL9qiRcdi4Dvz+8s3JUgNR2qvbgZkXcwPEEEk=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.4/go.mod h1:Wjn5O9eS7uSi7vlPKt/v0MLTncANn9EMmoDvnzJli6o=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.8 h1:RnLB7p6aaFMRfyQkD6ckxR7myCC9SABIqSz4czYUUbU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.8/go.mod h1:XH7dQJd+56wEbP1I4e4Duo+QhSMxNArE8VP7NuUOTeM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.8 h1:jzApk2f58L9yW9q1GEab3BMMFWUkkiZhyrRUtbwUbKU=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.8/go.mod h1:WqO+FftfO3tGePUtQxPXM6iODVfqMwsVMgTbG/ZXIdQ=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.8 h1:jH33S0y5Bo5ZVML62JgZhjd/LrtU+vbR8W7XnIE3Srk=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.8/go.mod h1:hD5YwHLOy6k7d6kqcn3me1bFWHOtzhaXstMd6BpdB68=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 h1:Ji0DY1xUsUr3I8cHps0G+XM3WWU16lP6yG8qu1GAZAs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2/go.mod h1:5CsjAbs3NlGQyZNFACh+zztPDI7fU6eW9QsxjfnuBKg=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.10 h1:pkYC5zTOSPXEYJj56b2SOik9AL432i5MT1YVTQbKOK0=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.10/go.mod h1:/WNsBOlKWZCG3PMh2aSp8vkyyT/clpMZqOtrnIKqGfk=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.10 h1:7kZqP7akv0enu6ykJhb9OYlw16oOrSy+Epus8o/VqMY=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.10/go.mod h1:gYVF3nM1ApfTRDj9pvdhootBb8WbiIejuqn4w8ruMes=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.8 h1:iQNXVs1vtaq+y9M90M4ZIVNORje0qXTscqHLqoOnFS0=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.8/go.mod h1:yUQPRlWqGG0lfNsmjbRWKVwgilfBtZTOFSLEYALlAig=
github.com/aws/aws-sdk-go-v2/service/s3 v1.54.4 h1:4p9SCdZBO0PdEXLTF2fcQuxOEkEiqPQpK824cP2VKRo=
github.com/aws/aws-sdk-go-v2/service/s3 v1.54.4/go.mod h1:oSkRFuHVWmUY4Ssk16ErGzBqvYEbvORJFzFXzWhTB2s=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.10 h1:ItKVmFwbyb/ZnCWf+nu3XBVmUirpO9eGEQd7urnBA0s=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.10/go.mod h1:5XKooCTi9VB/xZmJDvh7uZ+v3uQ7QdX6diOyhvPA+/w=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.4 h1:QMSCYDg3Iyls0KZc/dk3JtS2c1lFfqbmYO10qBPPkJk=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.4/go.mod h1:MZ/PVYU/mRbmSF6WK3ybCYHjA2mig8utVokDEVLDgE0=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.11 h1:HYS0csS7UJxdYRoG+bGgUYrSwVnV3/ece/wHm90TApM=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.11/go.mod h1:QXnthRM35zI92048MMwfFChjFmoufTdhtHmouwNfhhU=
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -643,7 +677,6 @@ github.com/cncf/xds/go v0.0.0-20230105202645-06c439db220b/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
Expand All @@ -665,8 +698,8 @@ github.com/envoyproxy/protoc-gen-validate v0.9.1/go.mod h1:OKNgG7TCp5pF4d6XftA0+
github.com/envoyproxy/protoc-gen-validate v0.10.1/go.mod h1:DRjgyB0I43LtJapqN6NiRwroiAU2PaFuvk/vjgh61ss=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fujiwara/shapeio v0.0.0-20170602072123-c073257dd745 h1:+tPNWeI7Uk5JKgSj4IYytZc0mdch+e3Yf8g1sGOg4hQ=
github.com/fujiwara/shapeio v0.0.0-20170602072123-c073257dd745/go.mod h1:/WpqsrSkjgwEG2Es2qnZXbXwHDVbawpdlXJIjJMmnZs=
github.com/fujiwara/shapeio v1.0.0 h1:xG5D9oNqCSUUbryZ/jQV3cqe1v2suEjwPIcEg1gKM8M=
github.com/fujiwara/shapeio v1.0.0/go.mod h1:LmEmu6L/8jetyj1oewewFb7bZCNRwE7wLCUNzDLaLVA=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=
github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks=
Expand Down Expand Up @@ -799,9 +832,7 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
Expand Down Expand Up @@ -838,7 +869,6 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down Expand Up @@ -1008,7 +1038,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221012135044-0b7e1fb9d458/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
Expand Down Expand Up @@ -1137,7 +1166,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -1150,7 +1178,6 @@ golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
Expand Down Expand Up @@ -1181,6 +1208,7 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
Expand Down
31 changes: 17 additions & 14 deletions manifest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stretcher

import (
"context"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
Expand Down Expand Up @@ -47,19 +48,19 @@ func (m *Manifest) newHash() (hash.Hash, error) {
}
}

func (m *Manifest) runCommands() error {
if err := m.Commands.Pre.Invoke(); err != nil {
func (m *Manifest) runCommands(ctx context.Context) error {
if err := m.Commands.Pre.Invoke(ctx); err != nil {
return err
}
if err := m.Commands.Post.Invoke(); err != nil {
if err := m.Commands.Post.Invoke(ctx); err != nil {
return err
}
return nil
}

func (m *Manifest) Deploy(conf Config) error {
func (m *Manifest) Deploy(ctx context.Context, conf Config) error {
if m.Src == "" {
return m.runCommands()
return m.runCommands(ctx)
}

strategy, err := NewSyncStrategy(m)
Expand All @@ -76,10 +77,12 @@ func (m *Manifest) Deploy(conf Config) error {

if conf.Timeout != 0 {
log.Printf("Set timeout %s", conf.Timeout)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
timer := time.NewTimer(conf.Timeout)
done := make(chan error)
go func() {
done <- m.fetchSrc(conf, tmp)
done <- m.fetchSrc(ctx, conf, tmp)
}()
select {
case <-timer.C:
Expand All @@ -90,7 +93,7 @@ func (m *Manifest) Deploy(conf Config) error {
}
}
} else {
err := m.fetchSrc(conf, tmp)
err := m.fetchSrc(ctx, conf, tmp)
if err != nil {
return err
}
Expand All @@ -102,7 +105,7 @@ func (m *Manifest) Deploy(conf Config) error {
}
defer os.RemoveAll(dir)

err = m.Commands.Pre.Invoke()
err = m.Commands.Pre.Invoke(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -143,22 +146,22 @@ func (m *Manifest) Deploy(conf Config) error {
return err
}

err = m.Commands.Post.Invoke()
err = m.Commands.Post.Invoke(ctx)
if err != nil {
return err
}
return nil
}

func (m *Manifest) fetchSrc(conf Config, tmp *os.File) error {
func (m *Manifest) fetchSrc(ctx context.Context, conf Config, tmp *os.File) error {
begin := time.Now()
src, err := getURL(m.Src)
src, err := getURL(ctx, m.Src)
if err != nil {
for i := 0; i < conf.Retry; i++ {
log.Printf("Get src failed: %s", err)
log.Printf("Try again. Waiting: %s", conf.RetryWait)
time.Sleep(conf.RetryWait)
src, err = getURL(m.Src)
src, err = getURL(ctx, m.Src)
if err == nil {
break
}
Expand All @@ -175,7 +178,7 @@ func (m *Manifest) fetchSrc(conf Config, tmp *os.File) error {
lsrc.SetRateLimit(float64(conf.MaxBandWidth))
}

written, sum, err := m.copyAndCalcHash(tmp, lsrc)
written, sum, err := m.copyAndCalcHash(ctx, tmp, lsrc)
if err != nil {
return err
}
Expand All @@ -194,7 +197,7 @@ func (m *Manifest) fetchSrc(conf Config, tmp *os.File) error {
return nil
}

func (m *Manifest) copyAndCalcHash(dst io.Writer, src io.Reader) (int64, string, error) {
func (m *Manifest) copyAndCalcHash(ctx context.Context, dst io.Writer, src io.Reader) (int64, string, error) {
h, err := m.newHash()
if err != nil {
return 0, "", err
Expand Down
Loading

0 comments on commit 8b81ab9

Please sign in to comment.