Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/flag quiet #185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ you can use one of them, both or neither.
1build unset before after
```

### Using `--quiet` or `-q` flag
Sometimes you choose to not see all logs/output of your command and just see success or failure as the outcome.
So using `--quiet` or `-q` flag to 1build command execution will result in just executing the command
but not showing the entire output of it, only shows SUCCESS/FAILURE as result of command execution.
```console
1build lint test --quiet
```
OR
```console
1build lint test -q
```

See `1build --help` for command usages.

## Contributing
Expand Down
25 changes: 18 additions & 7 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gopinath-langote/1build/cmd/config"
"github.com/gopinath-langote/1build/cmd/models"
"github.com/gopinath-langote/1build/cmd/utils"
"github.com/spf13/viper"
patil-ashutosh marked this conversation as resolved.
Show resolved Hide resolved
)

// ExecutePlan executes the Execution plan
Expand Down Expand Up @@ -43,14 +44,24 @@ func ExecutePlan(commands ...string) {

func executeAndStopIfFailed(command *models.CommandContext, executeStart time.Time) {
command.PrintPhaseBanner()
err := command.CommandSession.Run()
if err != nil {
exitCode := (err.Error())[12:]
text := "\nExecution failed in phase '" + command.Name + "' – exit code: " + exitCode
utils.CPrintln(text, utils.Style{Color: utils.RED})
printResultsBanner(false, executeStart)
utils.ExitWithCode(exitCode)
if !viper.GetBool("quiet") {
err := command.CommandSession.Run()
if err != nil {
exitCode := (err.Error())[12:]
text := "\nExecution failed in phase '" + command.Name + "' – exit code: " + exitCode
utils.CPrintln(text, utils.Style{Color: utils.RED})
printResultsBanner(false, executeStart)
utils.ExitWithCode(exitCode)
}
patil-ashutosh marked this conversation as resolved.
Show resolved Hide resolved
} else {
_, err := command.CommandSession.CombinedOutput()
if err != nil {
exitCode := (err.Error())[12:]
printResultsBanner(false, executeStart)
utils.ExitWithCode(exitCode)
}
}

}

func buildExecutionPlan(onebuildConfig config.OneBuildConfiguration, commands ...string) models.OneBuildExecutionPlan {
Expand Down
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"fmt"

parse "github.com/gopinath-langote/1build/cmd/config"
"github.com/gopinath-langote/1build/cmd/exec"
"github.com/gopinath-langote/1build/cmd/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -39,4 +41,7 @@ func Execute() {

func init() {
rootCmd.SetHelpCommand(&cobra.Command{Use: "no-help", Hidden: true})
rootCmd.PersistentFlags().BoolP("quiet", "q", false,
"Hide output log of command & only show SUCCESS/FAILURE result")
_ = viper.BindPFlags(rootCmd.PersistentFlags())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.12
require (
github.com/codegangsta/inject v0.0.0-20140425184007-37d7f8432a3e // indirect
github.com/codeskyblue/go-sh v0.0.0-20170112005953-b097669b1569
github.com/kr/pretty v0.1.0 // indirect
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b
github.com/spf13/cobra v0.0.6
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand All @@ -44,6 +45,7 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand All @@ -60,12 +62,15 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b h1:PMbSa9CgaiQR9NLlUTwKi+7aeLl3GG5JX5ERJxfQ3IE=
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -86,16 +91,20 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v0.0.6 h1:breEStsVwemnKh2/s6gMvSdMEkwW0sK8vGStnlVBMCs=
github.com/spf13/cobra v0.0.6/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -131,7 +140,9 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
5 changes: 5 additions & 0 deletions testing/fixtures/execute_cmd_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ func assertFailureMessage(t *testing.T, actualOutput string, phase string, exitC
return utils.AssertContains(t, actualOutput, errorText)
}

func assertFailureMessageNone(t *testing.T, actualOutput string, phase string, exitCode string) bool {
errorText := "\nExecution failed in phase '" + phase + "' – exit code: " + exitCode
return utils.AssertNotContains(t, actualOutput, errorText)
}

func assertFailureBanner(t *testing.T, actualOutput string) bool {
return utils.AssertContains(t, actualOutput, "FAILURE - Total Time")
}
1 change: 1 addition & 0 deletions testing/fixtures/fixures.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetFixtures() []Test {
featureUnsetTestsData(),

featureFlagVersionTestData(),
featureFlagTestData(),
featureDeleteTestData(),
}

Expand Down
224 changes: 224 additions & 0 deletions testing/fixtures/flag_fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package fixtures

import (
"testing"

"github.com/gopinath-langote/1build/testing/utils"
)

func featureFlagTestData() []Test {
feature := "flag"
return []Test{
shouldExecuteCommandWithquietFlag(feature),
shouldExecuteBeforeCommandWithquietFlag(feature),
shouldExecuteAfterCommandWithquietFlag(feature),
shouldExecuteBeforeAndAfterCommandWithquietFlag(feature),
shouldStopExecutionIfBeforeCommandFailedWithquietFlag(feature),
shouldStopExecutionIfCommandFailedWithquietFlag(feature),
}
}

func shouldExecuteCommandWithquietFlag(feature string) Test {
fileContent := `
project: Sample Project
commands:
- build: echo building project
`
expectedOutput := `
----- ---------------------
Phase Command
----- ---------------------
build echo building project


-------------------------------[ ` + "build" + ` ]--------------------------------

`
return Test{
Feature: feature,
Name: "shouldExecuteCommandWithquietFlag",
CmdArgs: []string{"build", "--quiet"},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}

func shouldExecuteBeforeCommandWithquietFlag(feature string) Test {
fileContent := `
project: Sample Project
before: echo running pre-command
commands:
- build: echo building project
`
expectedOutput := `
------ ------------------------
Phase Command
------ ------------------------
before echo running pre-command
build echo building project


-------------------------------[ ` + "before" + ` ]-------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------

`
return Test{
Feature: feature,
Name: "shouldExecuteBeforeCommandWithquietFlag",
CmdArgs: []string{"build", "--quiet"},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}

func shouldExecuteAfterCommandWithquietFlag(feature string) Test {
fileContent := `
project: Sample Project
after: echo running post-command
commands:
- build: echo building project
`
expectedOutput := `
----- -------------------------
Phase Command
----- -------------------------
build echo building project
after echo running post-command


-------------------------------[ ` + "build" + ` ]--------------------------------
-------------------------------[ ` + "after" + ` ]--------------------------------

`
return Test{
Feature: feature,
Name: "shouldExecuteAfterCommandWithquietFlag",
CmdArgs: []string{"build", "--quiet"},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}

func shouldExecuteBeforeAndAfterCommandWithquietFlag(feature string) Test {
fileContent := `
project: Sample Project
before: echo running pre-command
after: echo running post-command
commands:
- build: echo building project
`
expectedOutput := `
------ -------------------------
Phase Command
------ -------------------------
before echo running pre-command
build echo building project
after echo running post-command


-------------------------------[ ` + "before" + ` ]-------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------
-------------------------------[ ` + "after" + ` ]--------------------------------

`
return Test{
Feature: feature,
Name: "shouldExecuteBeforeAndAfterCommandWithquietFlag",
CmdArgs: []string{"build", "--quiet"},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {

return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertSuccessBanner(t, actualOutput)
},
}
}

func shouldStopExecutionIfBeforeCommandFailedWithquietFlag(feature string) Test {
fileContent := `
project: Sample Project
before: exit 10
after: echo running post-command
commands:
- build: echo building project
`
expectedOutput := `
------ -------------------------
Phase Command
------ -------------------------
before exit 10
build echo building project
after echo running post-command


-------------------------------[ ` + "before" + ` ]-------------------------------

`
return Test{
Feature: feature,
Name: "shouldStopExecutionIfBeforeCommandFailedWithquietFlag",
CmdArgs: []string{"build", "--quiet"},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertFailureMessageNone(t, actualOutput, "before", "10") &&
assertFailureBanner(t, actualOutput)

},
}
}

func shouldStopExecutionIfCommandFailedWithquietFlag(feature string) Test {
fileContent := `
project: Sample Project
before: echo running pre-command
after: echo running post-command
commands:
- build: invalid_command
`
expectedOutput := `
------ -------------------------
Phase Command
------ -------------------------
before echo running pre-command
build invalid_command
after echo running post-command


-------------------------------[ ` + "before" + ` ]-------------------------------
-------------------------------[ ` + "build" + ` ]--------------------------------

`
return Test{
Feature: feature,
Name: "shouldStopExecutionIfCommandFailedWithquietFlag",
CmdArgs: []string{"build", "--quiet"},
Setup: func(dir string) error {
return utils.CreateConfigFile(dir, fileContent)
},
Assertion: func(dir string, actualOutput string, t *testing.T) bool {
return utils.AssertContains(t, actualOutput, expectedOutput) &&
assertFailureMessageNone(t, actualOutput, "build", "127") &&
assertFailureBanner(t, actualOutput)
},
}
}
Loading