-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from 3 commits
9e25432
54821f5
ec8314c
48e43fa
6ed0065
e08ea9b
3020357
259d374
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2018 Uber Technologies, Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just make this |
||
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)) | ||
} | ||
|
@@ -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") | ||
|
||
|
@@ -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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In tests just do:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we test There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
There was a problem hiding this comment.
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")