Skip to content

Commit

Permalink
fix(1163): Reduce api call (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichi10 authored and catto committed Jul 6, 2018
1 parent a47692a commit afccabc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
5 changes: 2 additions & 3 deletions screwdriver.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
workflow:
- deploy

shared:
image: golang
environment:
GOPATH: /sd/workspace

jobs:
main:
requires: [~commit, ~pr]
steps:
- get: go get -t ./...
- vet: go vet ./...
Expand All @@ -16,6 +14,7 @@ jobs:
- build: go build -a -o sd-step

deploy:
requires: main
steps:
- setup-ci: git clone https://github.com/screwdriver-cd/toolbox.git ci
- get: go get -t ./...
Expand Down
46 changes: 33 additions & 13 deletions sd-step.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -77,16 +78,22 @@ func execHab(pkgName string, pkgVersion string, habChannel string, command []str
return verErr
}

installCmd := []string{habPath, "pkg", "install", pkg, "-c", habChannel, ">/dev/null"}
if u, userErr := user.Current(); userErr != nil || u.Uid != "0" {
// execute sudo command if not root user
installCmd = append([]string{"sudo"}, installCmd...)
}
// Check if the desired version of the package exists
checkCmd := habPath + " pkg path " + pkg + " >/dev/null 2>&1"
checkCmdResult := runCommand(checkCmd, output)

if checkCmdResult != nil {
installCmd := []string{habPath, "pkg", "install", pkg, "-c", habChannel, ">/dev/null"}
if u, userErr := user.Current(); userErr != nil || u.Uid != "0" {
// execute sudo command if not root user
installCmd = append([]string{"sudo"}, installCmd...)
}

unwrappedInstallCommand := strings.Join(installCmd, " ")
installErr := runCommand(unwrappedInstallCommand, output)
if installErr != nil {
return installErr
unwrappedInstallCommand := strings.Join(installCmd, " ")
installErr := runCommand(unwrappedInstallCommand, output)
if installErr != nil {
return installErr
}
}

execCmd := []string{habPath, "pkg", "exec", pkg}
Expand All @@ -109,7 +116,15 @@ func getPackageVersion(depot hab.Depot, pkgName, pkgVerExp string, habChannel st

foundVersions, err := depot.PackageVersionsFromName(pkgName, habChannel)
if err != nil {
return "", fmt.Errorf("Failed to fetch package versions: %v", err)
fmt.Fprintf(os.Stderr, "ERROR: Unable to access to Habitat depot API. %v\n"+
"Trying to fetch versions from installed packages...\n", err)
dirs, err := ioutil.ReadDir("/hab/pkgs/" + pkgName)
if err != nil {
return "", errors.New("The specified version not found")
}
for _, dir := range dirs {
foundVersions = append(foundVersions, dir.Name())
}
}

var versions []*semver.Version
Expand Down Expand Up @@ -143,6 +158,8 @@ func main() {

var pkgVerExp string
var habChannel string
var pkgVersion string
var err error

app := cli.NewApp()
app.Name = "sd-step"
Expand Down Expand Up @@ -182,10 +199,13 @@ func main() {
pkgName := c.Args().Get(0)

depot := hab.New(habDepotURL)
pkgVersion, err := getPackageVersion(depot, pkgName, pkgVerExp, habChannel)

if err != nil {
failureExit(fmt.Errorf("Failed to get package version: %v", err))
if pkgVerExp != "" {
pkgVersion, err = getPackageVersion(depot, pkgName, pkgVerExp, habChannel)

if err != nil {
failureExit(fmt.Errorf("Failed to get package version: %v", err))
}
}

err = execHab(pkgName, pkgVersion, habChannel, c.Args().Tail(), os.Stdout)
Expand Down
2 changes: 1 addition & 1 deletion sd-step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestGetPackageVersions(t *testing.T) {
foundVersions: []string{"0.0.1", "0.1.0", "1.1.9", "1.2.1", "1.2.2", "1.3.0", "2.0.0"},
expectedVersion: "",
depotError: errors.New("depot error"),
expectedError: errors.New("Failed to fetch package versions: depot error"),
expectedError: errors.New("The specified version not found"),
},
{
versionExpression: "~1.2.0",
Expand Down

0 comments on commit afccabc

Please sign in to comment.