Merge remote-tracking branch 'origin/master' into completion

This commit is contained in:
Mislav Marohnić 2019-10-31 23:28:06 +01:00
commit 55e9c18840
4 changed files with 31 additions and 21 deletions

View file

@ -1,10 +1,12 @@
BUILD_FILES = $(shell go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}}\
{{end}}' ./...)
# export GOFLAGS := -mod=vendor $GOFLAGS
GH_VERSION = $(shell go describe --tags 2>/dev/null || git rev-parse --short HEAD)
LDFLAGS := -X github.com/github/gh-cli/command.Version=$(GH_VERSION) $(LDFLAGS)
LDFLAGS := -X github.com/github/gh-cli/command.BuildDate=$(shell date +%Y-%m-%d) $(LDFLAGS)
bin/gh: $(BUILD_FILES)
go build -o "$@"
@go build -ldflags "$(LDFLAGS)" -o "$@"
test:
go test ./...

View file

@ -6,16 +6,31 @@ import (
"github.com/github/gh-cli/api"
"github.com/github/gh-cli/context"
"github.com/github/gh-cli/version"
"github.com/spf13/cobra"
)
// Version is dynamically set at build time in the Makefile
var Version = "DEV"
// BuildDate is dynamically set at build time in the Makefile
var BuildDate = "YYYY-MM-DD"
func init() {
RootCmd.Version = fmt.Sprintf("%s (%s)", Version, BuildDate)
RootCmd.PersistentFlags().StringP("repo", "R", "", "current GitHub repository")
RootCmd.PersistentFlags().StringP("current-branch", "B", "", "current git branch")
// TODO:
// RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output")
RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
return FlagError{err}
})
}
// FlagError is the kind of error raised in flag processing
type FlagError struct {
error
}
// RootCmd is the entry point of command-line execution
@ -23,10 +38,9 @@ var RootCmd = &cobra.Command{
Use: "gh",
Short: "GitHub CLI",
Long: `Do things with GitHub from your terminal`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("root")
},
SilenceErrors: true,
SilenceUsage: true,
}
// overriden in tests
@ -57,7 +71,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
}
opts := []api.ClientOption{
api.AddHeader("Authorization", fmt.Sprintf("token %s", token)),
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", version.Version)),
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)),
}
if verbose := os.Getenv("DEBUG"); verbose != "" {
opts = append(opts, api.VerboseLog(os.Stderr))

View file

@ -3,13 +3,18 @@ package main
import (
"fmt"
"os"
"strings"
"github.com/github/gh-cli/command"
)
func main() {
if err := command.RootCmd.Execute(); err != nil {
fmt.Println(err)
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
fmt.Fprintln(os.Stderr, err)
_, isFlagError := err.(command.FlagError)
if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") {
fmt.Fprintln(os.Stderr, cmd.UsageString())
}
os.Exit(1)
}
}

View file

@ -1,11 +0,0 @@
package version
import (
"fmt"
)
var Version = "0.0.0"
func FullVersion() (string, error) {
return fmt.Sprintf("gh version %s", Version), nil
}