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

Add version command #12

Merged
merged 8 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# astro changelog

## 0.5.0 (UNRELEASED, 2018)

## 0.5.0 (UNRELEASED, 2019)

* Add `version` command
* Propagate SIGINT and SIGTERM to terraform processes (#49)
* Support detach flag with terraform 0.12 (#45)
* Fix plan output for terraform 0.12 (#41)
* Fix bug in initialization of allowed values (#43)
* Don't pass varialbes to modules that don't delcare them (#40)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put this in a separate PR? They have nothing to do with this one.

(Also, typo: "variables")

* Adopt options pattern for `astro.NewProject` constructor (#26)
* Refactor and improve integration tests to invoke them directly using cli
rather than `os.exec` (#26)
Expand Down Expand Up @@ -34,6 +40,7 @@
`astro.NewProject(conf)` should be changed to:
`astro.NewProject(astro.WithConfig(conf))`


## 0.4.1 (October 3, 2018)

* Output policy changes in unified diff format (#2)
Expand Down
23 changes: 15 additions & 8 deletions astro/cli/astro/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ type AstroCLI struct {
}

commands struct {
root *cobra.Command
plan *cobra.Command
apply *cobra.Command
root *cobra.Command
plan *cobra.Command
apply *cobra.Command
version *cobra.Command
}
}

Expand All @@ -85,10 +86,12 @@ func NewAstroCLI(opts ...Option) (*AstroCLI, error) {
cli.createRootCommand()
cli.createPlanCmd()
cli.createApplyCmd()
cli.createVersionCmd()

cli.commands.root.AddCommand(
cli.commands.plan,
cli.commands.apply,
cli.commands.version,
)

// Set trace. Note, this will turn tracing on for all instances of astro
Expand Down Expand Up @@ -159,11 +162,10 @@ func (cli *AstroCLI) configureDynamicUserFlags() {

func (cli *AstroCLI) createRootCommand() {
rootCmd := &cobra.Command{
Use: "astro",
Short: "A tool for managing multiple Terraform modules.",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: cli.preRun,
Use: "astro",
Short: "A tool for managing multiple Terraform modules.",
SilenceUsage: true,
SilenceErrors: true,
}

rootCmd.PersistentFlags().BoolVarP(&cli.flags.verbose, "verbose", "v", false, "verbose output")
Expand All @@ -178,6 +180,7 @@ func (cli *AstroCLI) createApplyCmd() {
Use: "apply [flags] [-- [Terraform argument]...]",
DisableFlagsInUseLine: true,
Short: "Run Terraform apply on all modules",
PersistentPreRunE: cli.preRun,
RunE: cli.runApply,
}

Expand All @@ -191,6 +194,7 @@ func (cli *AstroCLI) createPlanCmd() {
Use: "plan [flags] [-- [Terraform argument]...]",
DisableFlagsInUseLine: true,
Short: "Generate execution plans for modules",
PersistentPreRunE: cli.preRun,
RunE: cli.runPlan,
}

Expand All @@ -203,6 +207,9 @@ func (cli *AstroCLI) createPlanCmd() {
func (cli *AstroCLI) preRun(cmd *cobra.Command, args []string) error {
logger.Trace.Println("cli: in preRun")

if cli.config == nil {
return fmt.Errorf("unable to find config file")
}
// Load astro from config
project, err := astro.NewProject(astro.WithConfig(*cli.config))
if err != nil {
Expand Down
62 changes: 62 additions & 0 deletions astro/cli/astro/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2018 Uber Technologies, Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2019

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Package cmd contains the source for the `astro` command line tool
// that operators use to interact with the project.

package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"
)

var (
// When a release happens, the value of this variable will be overwritten
// by the linker to match the release version.
version = "dev"
commit = ""
date = ""
)

func (cli *AstroCLI) createVersionCmd() {
versionCmd := &cobra.Command{
Use: "version",
DisableFlagsInUseLine: true,
Short: "Print astro version",
RunE: func(cmd *cobra.Command, args []string) error {
versionString := []string{
"astro version",
version,
}

if commit != "" {
versionString = append(versionString, fmt.Sprintf("(%s)", commit))
}

if date != "" {
versionString = append(versionString, fmt.Sprintf("built %s", date))
}

fmt.Fprintln(cli.stdout, strings.Join(versionString, " "))

return nil
},
}
cli.commands.version = versionCmd
}
68 changes: 65 additions & 3 deletions astro/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ func stringVersionMatches(v string, versionConstraint string) bool {
}

// compiles the astro binary and returns the path to it.
func compileAstro(dir string) (string, error) {
func compileAstro(dir string, ldflags []string) (string, error) {
Copy link
Contributor Author

@dansimau dansimau Oct 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just make this buildFlags []string and allow people to pass arbitrary flags.

astroPath := filepath.Join(dir, "astro")
packageName := "github.com/uber/astro/astro/cli/astro"
out, err := exec.Command("go", "build", "-o", astroPath, packageName).CombinedOutput()
compileArgs := []string{"build"}
if len(ldflags) > 0 {
compileArgs = append(compileArgs, "-ldflags")
for _, ldflag := range ldflags {
compileArgs = append(compileArgs, ldflag)
}
}
compileArgs = append(compileArgs, "-o", astroPath, packageName)
out, err := exec.Command("go", compileArgs...).CombinedOutput()
if err != nil {
return "", errors.New(string(out))
}
Expand All @@ -95,7 +103,7 @@ func TestPlanInterrupted(t *testing.T) {
defer os.RemoveAll(tmpdir)
require.NoError(t, err)

astroBinary, err := compileAstro(tmpdir)
astroBinary, err := compileAstro(tmpdir, []string{})
require.NoError(t, err)
command := exec.Command(astroBinary, "plan")

Expand Down Expand Up @@ -219,3 +227,57 @@ func TestProjectPlanDetachSuccess(t *testing.T) {
})
}
}

func TestVersionDev(t *testing.T) {
dir, err := ioutil.TempDir("/tmp", "astro-tests")
if err != nil {
panic(err)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tests just do:

require.NoError(t, err)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

defer os.RemoveAll(dir)
result := RunTest(t, []string{"version"}, "/tmp/astro-tests", "")
assert.Equal(t, "", result.Stderr.String())
assert.Equal(t, "astro version dev\n", result.Stdout.String())
assert.Equal(t, 0, result.ExitCode)
}

func TestVersionWithLdflags(t *testing.T) {
dir, err := ioutil.TempDir("/tmp", "astro-tests")
if err != nil {
panic(err)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require.NoError(t, err)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks

defer os.RemoveAll(dir)

stdoutBytes := &bytes.Buffer{}
stderrBytes := &bytes.Buffer{}

astroBinary, err := compileAstro(dir, []string{
"-X github.com/uber/astro/astro/cli/astro/cmd.version=1.2.3 " +
"-X github.com/uber/astro/astro/cli/astro/cmd.commit=ab123 " +
"-X github.com/uber/astro/astro/cli/astro/cmd.date=2019-01-01T10:00:00",
})
require.NoError(t, err)
command := exec.Command(astroBinary, "version")
command.Dir = dir

command.Stdout = stdoutBytes
command.Stderr = stderrBytes

err = command.Run()

require.NoError(t, err)

assert.Equal(t, "", stderrBytes.String())
assert.Equal(t, "astro version 1.2.3 (ab123) built 2019-01-01T10:00:00\n", stdoutBytes.String())
}

func TestPlanErorrsWithoutConfig(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test apply too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test below.

dir, err := ioutil.TempDir("/tmp", "astro-tests")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
dansimau marked this conversation as resolved.
Show resolved Hide resolved
result := RunTest(t, []string{"plan"}, "/tmp/astro-tests", "")
assert.Equal(t, "unable to find config file\n", result.Stderr.String())
assert.Equal(t, "", result.Stdout.String())
assert.Equal(t, 1, result.ExitCode)
}