Skip to content

Commit

Permalink
Move build update to update build-agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Aug 6, 2018
1 parent ed68ed1 commit daed42c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
51 changes: 0 additions & 51 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"path"
"regexp"
"syscall"

"github.com/CircleCI-Public/circleci-cli/settings"
Expand All @@ -16,27 +15,16 @@ import (
)

func newBuildCommand() *cobra.Command {

updateCommand := &cobra.Command{
Use: "update",
Short: "Update the build agent to the latest version",
RunE: updateBuildAgentToLatest,
}

buildCommand := &cobra.Command{
Use: "build",
Short: "Run a build",
RunE: runBuild,
DisableFlagParsing: true,
}

buildCommand.AddCommand(updateCommand)

return buildCommand
}

var picardRepo = "circleci/picard"

func circleCiDir() string {
return path.Join(settings.UserHomeDir(), ".circleci")
}
Expand Down Expand Up @@ -69,45 +57,6 @@ func storeBuildAgentSha(sha256 string) error {
return errors.Wrap(err, "Failed to write build agent settings file")
}

func updateBuildAgentToLatest(cmd *cobra.Command, args []string) error {

latestSha256, err := findLatestPicardSha()

if err != nil {
return err
}

Logger.Infof("Latest build agent is version %s", latestSha256)

return nil
}

func findLatestPicardSha() (string, error) {

outputBytes, err := exec.Command("docker", "pull", picardRepo).CombinedOutput() // #nosec

if err != nil {
return "", errors.Wrap(err, "failed to pull latest docker image")
}

output := string(outputBytes)
sha256 := regexp.MustCompile("(?m)sha256.*$")
latest := sha256.FindString(output)

if latest == "" {
return "", fmt.Errorf("failed to parse sha256 from docker pull output")
}

err = storeBuildAgentSha(latest)

if err != nil {
return "", err
}

return latest, nil

}

func loadCurrentBuildAgentSha() string {
if _, err := os.Stat(buildAgentSettingsPath()); os.IsNotExist(err) {
return ""
Expand Down
50 changes: 50 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cmd

import (
"fmt"
"os/exec"
"regexp"

"github.com/CircleCI-Public/circleci-cli/version"
"github.com/pkg/errors"
"github.com/rhysd/go-github-selfupdate/selfupdate"
Expand All @@ -27,9 +31,55 @@ func newUpdateCommand() *cobra.Command {
RunE: installUpdate,
})

update.AddCommand(&cobra.Command{
Use: "build-agent",
Short: "Update the build agent to the latest version",
RunE: updateBuildAgent,
})

return update
}

var picardRepo = "circleci/picard"

func updateBuildAgent(cmd *cobra.Command, args []string) error {
latestSha256, err := findLatestPicardSha()

if err != nil {
return err
}

Logger.Infof("Latest build agent is version %s", latestSha256)

return nil
}

// Still depends on a function in cmd/build.go
func findLatestPicardSha() (string, error) {
outputBytes, err := exec.Command("docker", "pull", picardRepo).CombinedOutput() // #nosec

if err != nil {
return "", errors.Wrap(err, "failed to pull latest docker image")
}

output := string(outputBytes)
sha256 := regexp.MustCompile("(?m)sha256.*$")
latest := sha256.FindString(output)

if latest == "" {
return "", fmt.Errorf("failed to parse sha256 from docker pull output")
}

// This function still lives in cmd/build.go
err = storeBuildAgentSha(latest)

if err != nil {
return "", err
}

return latest, nil
}

func checkForUpdates(cmd *cobra.Command, args []string) error {
return update(true)

Expand Down

0 comments on commit daed42c

Please sign in to comment.