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

feat: unit test #9

Merged
merged 2 commits into from
Sep 3, 2021
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gazelle(
args = [
"-from_file=go.mod",
"-to_macro=go.bzl%deps",
"-prune",
],
command = "update-repos",
)
Expand Down
12 changes: 0 additions & 12 deletions bazel/BUILD.bazel

This file was deleted.

7 changes: 6 additions & 1 deletion buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/*
Copyright © 2021 Aspect Build Systems Inc

Not licensed for re-use.
*/

// Variables in this file will be replaced by the linker when Bazel is run with --stamp
// The time should be in format '2018-12-12 12:30:00 UTC'
// The GitStatus should be either "clean" or "dirty"
// Release will be a comma-separated string representation of any tags.

package buildinfo

// BuildTime is a string representation of when this binary was built.
Expand Down
20 changes: 0 additions & 20 deletions cmd/BUILD.bazel

This file was deleted.

15 changes: 13 additions & 2 deletions cmd/aspect/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "aspect_lib",
srcs = ["main.go"],
srcs = [
"main.go",
"root.go",
],
importpath = "aspect.build/cli/cmd/aspect",
visibility = ["//visibility:private"],
deps = ["//cmd"],
deps = [
"//cmd/aspect/version",
"//pkg/ioutils",
"@com_github_fatih_color//:color",
"@com_github_mattn_go_isatty//:go-isatty",
"@com_github_mitchellh_go_homedir//:go-homedir",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_viper//:viper",
],
)

go_binary(
Expand Down
11 changes: 9 additions & 2 deletions cmd/aspect/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*
Copyright © 2021 Aspect Build Systems Inc

Not licensed for re-use.
*/

package main

import "aspect.build/cli/cmd"
import "github.com/spf13/cobra"

func main() {
// Detect whether we are being run as a tools/bazel wrapper (look for BAZEL_REAL in the environment)
Expand All @@ -14,5 +20,6 @@ func main() {
// ask the user if they want to install for all users of the workspace, if so
// - tools/bazel file and put our bootstrap code in there
//
cmd.Execute()
cmd := NewDefaultRootCmd()
cobra.CheckErr(cmd.Execute())
}
63 changes: 63 additions & 0 deletions cmd/aspect/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright © 2021 Aspect Build Systems Inc

Not licensed for re-use.
*/

package main

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"aspect.build/cli/cmd/aspect/version"
"aspect.build/cli/pkg/ioutils"
)

func NewDefaultRootCmd() *cobra.Command {
defaultInteractive := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
return NewRootCmd(ioutils.DefaultStreams, defaultInteractive)
}

func NewRootCmd(streams ioutils.Streams, defaultInteractive bool) *cobra.Command {
cmd := &cobra.Command{
Use: "aspect",
Short: "Aspect.build bazel wrapper",
Long: color.New(color.FgBlue).SprintFunc()(`Aspect CLI`) + ` is a better frontend for running bazel`,
}

// ### Flags
var cfgFile string
var interactive bool
cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.aspect.yaml)")
cmd.PersistentFlags().BoolVar(&interactive, "interactive", defaultInteractive, "Interactive mode (e.g. prompts for user input)")

// ### Viper
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)

// Search config in home directory with name ".aspect" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".aspect")
}
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(streams.Stderr, "Using config file:", viper.ConfigFileUsed())
}

// ### Child commands
cmd.AddCommand(version.NewDefaultVersionCmd())

return cmd
}
14 changes: 14 additions & 0 deletions cmd/aspect/version/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "version",
srcs = ["version.go"],
importpath = "aspect.build/cli/cmd/aspect/version",
visibility = ["//visibility:public"],
deps = [
"//buildinfo",
"//pkg/aspect/version",
"//pkg/ioutils",
"@com_github_spf13_cobra//:cobra",
],
)
37 changes: 37 additions & 0 deletions cmd/aspect/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright © 2021 Aspect Build Systems

Not licensed for re-use
*/

package version

import (
"github.com/spf13/cobra"

"aspect.build/cli/buildinfo"
"aspect.build/cli/pkg/aspect/version"
"aspect.build/cli/pkg/ioutils"
)

func NewDefaultVersionCmd() *cobra.Command {
return NewVersionCmd(ioutils.DefaultStreams)
}

func NewVersionCmd(streams ioutils.Streams) *cobra.Command {
v := version.New(streams)

v.BuildinfoRelease = buildinfo.Release
v.BuildinfoGitStatus = buildinfo.GitStatus

cmd := &cobra.Command{
Use: "version",
Short: "Print the version of aspect CLI as well as tools it invokes",
Long: `Prints version info on colon-separated lines, just like bazel does`,
RunE: v.Run,
}

cmd.PersistentFlags().BoolVarP(&v.GNUFormat, "gnu_format", "", false, "format help following GNU convention")

return cmd
}
75 changes: 0 additions & 75 deletions cmd/root.go

This file was deleted.

36 changes: 0 additions & 36 deletions cmd/version.go

This file was deleted.

Loading