Skip to content

Commit c91fd57

Browse files
krancourKent Rancourt
authored andcommitted
chore(deisctl): use a reusable download utility
Refactoring refresh-units to use a reusable download utility. This will be re-used for refreshing decorators too to avoid DRY exceptions.
1 parent 3a27d1a commit c91fd57

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

deisctl/cmd/cmd.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package cmd
22

33
import (
44
"bytes"
5-
"errors"
65
"fmt"
76
"io"
8-
"io/ioutil"
9-
"net/http"
107
"os"
118
"path/filepath"
129
"regexp"
@@ -18,6 +15,7 @@ import (
1815
"github.com/deis/deis/deisctl/config"
1916
"github.com/deis/deis/deisctl/units"
2017
"github.com/deis/deis/deisctl/utils"
18+
"github.com/deis/deis/deisctl/utils/net"
2119
)
2220

2321
const (
@@ -462,19 +460,7 @@ func RefreshUnits(dir, tag, url string) error {
462460
for _, unit := range units.Names {
463461
src := fmt.Sprintf(url, tag, unit)
464462
dest := filepath.Join(dir, unit+".service")
465-
res, err := http.Get(src)
466-
if err != nil {
467-
return err
468-
}
469-
if res.StatusCode != 200 {
470-
return errors.New(res.Status)
471-
}
472-
defer res.Body.Close()
473-
data, err := ioutil.ReadAll(res.Body)
474-
if err != nil {
475-
return err
476-
}
477-
if err = ioutil.WriteFile(dest, data, 0644); err != nil {
463+
if err := net.Download(src, dest); err != nil {
478464
return err
479465
}
480466
fmt.Printf("Refreshed %s from %s\n", unit, tag)

deisctl/utils/net/net.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Package net contains commonly useful network functions
2+
package net
3+
4+
import (
5+
"errors"
6+
"io/ioutil"
7+
"net/http"
8+
)
9+
10+
// Download downloads a resource from a specified
11+
// source (URL) to the specified destination
12+
func Download(src string, dest string) error {
13+
res, err := http.Get(src)
14+
if err != nil {
15+
return err
16+
}
17+
if res.StatusCode != 200 {
18+
return errors.New(res.Status)
19+
}
20+
defer res.Body.Close()
21+
data, err := ioutil.ReadAll(res.Body)
22+
if err != nil {
23+
return err
24+
}
25+
if err = ioutil.WriteFile(dest, data, 0644); err != nil {
26+
return err
27+
}
28+
return nil
29+
}

0 commit comments

Comments
 (0)