Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Jul 21, 2017
1 parent 6941297 commit 6ac6c28
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 121 deletions.
45 changes: 23 additions & 22 deletions cmd/capsulecd/capsulecd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"os"
"time"

"gopkg.in/urfave/cli.v2"
"capsulecd/pkg/config"
"path/filepath"
"capsulecd/pkg"
"capsulecd/pkg/config"
"capsulecd/pkg/version"
"gopkg.in/urfave/cli.v2"
"path/filepath"
)

func main() {
app := &cli.App{
Name: "capsulecd",
Usage: "Continuous Delivery scripts for automating package releases",
Version: version.VERSION,
Name: "capsulecd",
Usage: "Continuous Delivery scripts for automating package releases",
Version: version.VERSION,
Compiled: time.Now(),
Authors: []*cli.Author{
&cli.Author{
Expand All @@ -26,17 +27,17 @@ func main() {

Commands: []*cli.Command{
{
Name: "start",
Usage: "Start a new CapsuleCD package pipeline",
Action: func(c *cli.Context) error {
Name: "start",
Usage: "Start a new CapsuleCD package pipeline",
Action: func(c *cli.Context) error {

config, _ := config.Create()
config.Set("scm", c.String("scm"))
config.Set("package_type", c.String("package_type"))
config.Set("dry_run", c.String("dry_run"))

//load configuration file.
if(c.String("config_file") != ""){
if c.String("config_file") != "" {
if absConfigPath, aerr := filepath.Abs(c.String("config_file")); aerr != nil {
config.ReadConfig(absConfigPath)
} else {
Expand All @@ -56,42 +57,42 @@ func main() {
return nil
},

Flags: []cli.Flag {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "runner",
Name: "runner",
Value: "default", // can be :none, :circleci or :shippable (check the readme for why other hosted providers arn't supported.)
Usage: "The cloud CI runner that is running this PR. (Used to determine the Environmental Variables to parse)",
},

&cli.StringFlag{
Name: "scm",
Name: "scm",
Value: "default",
Usage: "The scm for the code, used to determine which git endpoint to clone from, and create releases on",
},

&cli.StringFlag{
Name: "package_type",
Name: "package_type",
Aliases: []string{"package-type"},
Value: "default",
Usage: "The type of package being built.",
Value: "default",
Usage: "The type of package being built.",
},

&cli.BoolFlag{
Name: "dry_run",
Name: "dry_run",
Aliases: []string{"dry-run"},
Value: false,
Usage: "Specifies that no changes should be pushed to source and no package will be released",
Value: false,
Usage: "Specifies that no changes should be pushed to source and no package will be released",
},

&cli.StringFlag{
Name: "config_file",
Name: "config_file",
Aliases: []string{"config-file"},
Usage: "Specifies the location of the config file",
Usage: "Specifies the location of the config file",
},
},
},
},
}

app.Run(os.Args)
}
}
7 changes: 3 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package config

import (
"capsulecd/pkg/errors"
"capsulecd/pkg/utils"
"encoding/base64"
"fmt"
"github.com/spf13/viper"
"log"
"os"
"capsulecd/pkg/errors"
"fmt"
)

// When initializing this class the following methods must be called:
Expand All @@ -26,9 +26,8 @@ type configuration struct {
// key/value store
// default


func (c *configuration) init() error {
c.Viper = viper.New();
c.Viper = viper.New()
//set defaults
c.SetDefault("package_type", "default")
c.SetDefault("scm", "default")
Expand Down
16 changes: 8 additions & 8 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package config_test
import (
"capsulecd/pkg/config"
"github.com/stretchr/testify/assert"
"testing"
"os"
"path"
"testing"
)

func TestConfiguration_init_ShouldCorrectlyInitializeConfiguration(t *testing.T) {
Expand Down Expand Up @@ -43,13 +43,13 @@ func TestConfiguration_init_EnvVariablesShouldLoadProperly(t *testing.T) {
os.Unsetenv("CAPSULE_ENGINE_VERSION_BUMP_TYPE")
}

func TestConfiguration_ReadConfig_InvalidFilePath(t *testing.T){
func TestConfiguration_ReadConfig_InvalidFilePath(t *testing.T) {
t.Parallel()
//setup
testConfig, _ := config.Create()

//assert
assert.Error(t, testConfig.ReadConfig(path.Join("does","not","exist.yml")), "should raise an error")
assert.Error(t, testConfig.ReadConfig(path.Join("does", "not", "exist.yml")), "should raise an error")
}

func TestConfiguration_ReadConfig_WithSampleConfigurationFile(t *testing.T) {
Expand All @@ -60,7 +60,7 @@ func TestConfiguration_ReadConfig_WithSampleConfigurationFile(t *testing.T) {

//test
testConfig.ReadConfig(path.Join("testdata", "sample_configuration.yml"))
str64, _ := testConfig.GetBase64Decoded("chef_supermarket_key");
str64, _ := testConfig.GetBase64Decoded("chef_supermarket_key")
//assert
assert.Equal(t, "sample_test_token", testConfig.GetString("scm_github_access_token"), "should populate scm_github_access_token")
assert.Equal(t, "sample_auth_token", testConfig.GetString("npm_auth_token"), "should populate engine_npm_auth_token")
Expand All @@ -86,7 +86,7 @@ func TestConfiguration_ReadConfig_WithMultipleConfigurationFiles(t *testing.T) {
assert.Equal(t, "-----BEGIN RSA PRIVATE KEY-----\nsample_supermarket_key\n-----END RSA PRIVATE KEY-----\n", str64, "should correctly base64 decode chef supermarket key")
}

func TestConfiguration_GetBool(t *testing.T){
func TestConfiguration_GetBool(t *testing.T) {

//setup
os.Setenv("CAPSULE_BOOL_TEST_1", "true")
Expand All @@ -105,13 +105,13 @@ func TestConfiguration_GetBool(t *testing.T){
os.Unsetenv("CAPSULE_BOOL_TEST_2")
}

func TestConfiguration_GetBase64Decoded_WithInvalidData(t *testing.T){
func TestConfiguration_GetBase64Decoded_WithInvalidData(t *testing.T) {
t.Parallel()

//setup
testConfig, _ := config.Create()
testConfig.Set("test_key", "invalidBase64_encoding")
testConfig.Set("test_key_2", "ZW5jb2RlIHRoaXMgc3RyaW5nLiA=") //"encode this string. "
testConfig.Set("test_key_2", "ZW5jb2RlIHRoaXMgc3RyaW5nLiA=") //"encode this string. "
testConfig.Set("test_key_3", "dGhpcw0KaXMNCmEgbXVsdGlsaW5lDQpzdHJpbmc=") //multiline string "this\nis\na multiline\nstring"

//test
Expand All @@ -129,4 +129,4 @@ func TestConfiguration_GetBase64Decoded_WithInvalidData(t *testing.T){
assert.Equal(t, "this\r\nis\r\na multiline\r\nstring", testKey3, "should correctly decode base64 into multiline string")
assert.Nil(t, terr3)

}
}
1 change: 0 additions & 1 deletion pkg/config/factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package config


func Create() (Interface, error) {
config := new(configuration)
config.init()
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/factory_impl_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package config

import (
"testing"
"github.com/stretchr/testify/assert"
"testing"
)


func TestConfiguration(t *testing.T) {

//test
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine/engine_base_impl_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package engine

import (
"testing"
"capsulecd/pkg/config"
"github.com/stretchr/testify/assert"
"testing"
)

func TestEngineBase_BumpVersion(t *testing.T) {
Expand All @@ -15,9 +15,9 @@ func TestEngineBase_BumpVersion(t *testing.T) {
}

//test
ver, err := eng.BumpVersion("1.2.2");
ver, err := eng.BumpVersion("1.2.2")
assert.Nil(t, err)

//assert
assert.Equal(t, ver, "1.2.3", "should correctly do a patch bump")
assert.Equal(t, ver, "1.2.3", "should correctly do a patch bump")
}
6 changes: 3 additions & 3 deletions pkg/engine/engine_chef.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (g *engineChef) ValidateTools() error {
return errors.EngineValidateToolError("bundler binary is missing")
}

if _, berr := exec.LookPath("foodcritic"); berr != nil && !g.Config.GetBool("engine_disable_lint"){
if _, berr := exec.LookPath("foodcritic"); berr != nil && !g.Config.GetBool("engine_disable_lint") {
return errors.EngineValidateToolError("foodcritic binary is missing")
}

Expand Down Expand Up @@ -221,8 +221,8 @@ func (g *engineChef) DistStep() error {
return kerr
}

chefKey, berr := g.Config.GetBase64Decoded("chef_supermarket_key");
if berr != nil{
chefKey, berr := g.Config.GetBase64Decoded("chef_supermarket_key")
if berr != nil {
return berr
}
_, perr := pemFile.Write([]byte(chefKey))
Expand Down
25 changes: 12 additions & 13 deletions pkg/engine/engine_golang.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package engine

import (
"bytes"
"capsulecd/pkg/config"
"capsulecd/pkg/errors"
"capsulecd/pkg/pipeline"
"capsulecd/pkg/scm"
"capsulecd/pkg/utils"
"fmt"
"io/ioutil"
"os/exec"
"path"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"os/exec"
"path"
"strings"
"bytes"
"go/format"
)

type golangMetadata struct {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (g *engineGolang) ValidateTools() error {
func (g *engineGolang) AssembleStep() error {
//validate that the chef metadata.rb file exists

if !utils.FileExists(path.Join(g.PipelineData.GitLocalPath, "pkg","version","version.go")) {
if !utils.FileExists(path.Join(g.PipelineData.GitLocalPath, "pkg", "version", "version.go")) {
return errors.EngineBuildPackageInvalid("pkg/version/version.go file is required to process Go library")
}

Expand Down Expand Up @@ -108,10 +108,9 @@ func (g *engineGolang) TestStep() error {
return errors.EngineTestRunnerError(fmt.Sprintf("Lint command (%s) failed. Check log for more details.", lintCmd))
}


if g.Config.GetBool("engine_enable_code_mutation") {
//code formatter
fmtCmd := "go fmt -n $(go list ./... | grep -v /vendor/)"
fmtCmd := "go fmt $(go list ./... | grep -v /vendor/)"

if terr := utils.BashCmdExec(fmtCmd, g.PipelineData.GitLocalPath, ""); terr != nil {
return errors.EngineTestRunnerError(fmt.Sprintf("Format command (%s) failed. Check log for more details.", lintCmd))
Expand Down Expand Up @@ -169,7 +168,7 @@ func (g *engineGolang) DistStep() error {

func (g *engineGolang) retrieveCurrentMetadata(gitLocalPath string) error {

versionContent, rerr := ioutil.ReadFile(path.Join(g.PipelineData.GitLocalPath, "pkg","version","version.go"))
versionContent, rerr := ioutil.ReadFile(path.Join(g.PipelineData.GitLocalPath, "pkg", "version", "version.go"))
if rerr != nil {
return rerr
}
Expand All @@ -184,7 +183,7 @@ func (g *engineGolang) retrieveCurrentMetadata(gitLocalPath string) error {
}

version, verr := g.parseGoVersion(f.Decls)
if(verr != nil){
if verr != nil {
return verr
}

Expand All @@ -204,7 +203,7 @@ func (g *engineGolang) populateNextMetadata() error {
}

func (g *engineGolang) writeNextMetadata(gitLocalPath string) error {
versionPath := path.Join(g.PipelineData.GitLocalPath, "pkg","version","version.go")
versionPath := path.Join(g.PipelineData.GitLocalPath, "pkg", "version", "version.go")
versionContent, rerr := ioutil.ReadFile(versionPath)
if rerr != nil {
return rerr
Expand All @@ -220,7 +219,7 @@ func (g *engineGolang) writeNextMetadata(gitLocalPath string) error {
}

decls, serr := g.setGoVersion(f.Decls, g.NextMetadata.Version)
if(serr != nil){
if serr != nil {
return serr
}
f.Decls = decls
Expand Down Expand Up @@ -267,4 +266,4 @@ func (g *engineGolang) setGoVersion(list []ast.Decl, version string) ([]ast.Decl
}
}
return nil, errors.EngineBuildPackageFailed("Could not set the version in pkg/version/version.go")
}
}
4 changes: 2 additions & 2 deletions pkg/engine/engine_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func (n *engineNode) ValidateTools() error {
return errors.EngineValidateToolError("node binary is missing")
}

if _, kerr := exec.LookPath("eslint"); kerr != nil && !n.Config.GetBool("engine_disable_lint"){
if _, kerr := exec.LookPath("eslint"); kerr != nil && !n.Config.GetBool("engine_disable_lint") {
return errors.EngineValidateToolError("eslint binary is missing")
}

if _, kerr := exec.LookPath("nsp"); kerr != nil && !n.Config.GetBool("engine_disable_security_check"){
if _, kerr := exec.LookPath("nsp"); kerr != nil && !n.Config.GetBool("engine_disable_security_check") {
return errors.EngineValidateToolError("nsp binary is missing")
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/engine_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (g *enginePython) ValidateTools() error {
return errors.EngineValidateToolError("tox binary is missing")
}

if _, kerr := exec.LookPath("pylint"); kerr != nil && !g.Config.GetBool("engine_disable_lint"){
if _, kerr := exec.LookPath("pylint"); kerr != nil && !g.Config.GetBool("engine_disable_lint") {
return errors.EngineValidateToolError("pylint binary is missing")
}

Expand All @@ -52,7 +52,7 @@ func (g *enginePython) ValidateTools() error {
return errors.EngineValidateToolError("twine binary is missing")
}

if _, berr := exec.LookPath("safety"); berr != nil && !g.Config.GetBool("engine_disable_security_check"){
if _, berr := exec.LookPath("safety"); berr != nil && !g.Config.GetBool("engine_disable_security_check") {
return errors.EngineValidateToolError("safety binary is missing")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/engine_ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (g *engineRuby) ValidateTools() error {
return errors.EngineValidateToolError("rake binary is missing")
}

if _, berr := exec.LookPath("rubocop"); berr != nil && !g.Config.GetBool("engine_disable_lint"){
if _, berr := exec.LookPath("rubocop"); berr != nil && !g.Config.GetBool("engine_disable_lint") {
return errors.EngineValidateToolError("rubocop binary is missing")
}

Expand Down
Loading

0 comments on commit 6ac6c28

Please sign in to comment.