-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (92 loc) · 3.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2021-2022 Ambassador Labs
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/spf13/cobra"
)
// FlagErrorFunc is a function to be passed to (*cobra.Command).SetFlagErrorFunc that establishes
// GNU-ish behavior for invalid flag usage.
//
// If there is an error, FlagErrorFunc calls os.Exit; it does NOT return. This means that all
// errors returned from (*cobra.Command).Execute will be execution errors, not usage errors.
func FlagErrorFunc(cmd *cobra.Command, err error) error {
// Copyright note: This code was originally written by LukeShu for Telepresence.
// https://github.com/telepresenceio/telepresence/commit/3b63073ceafae6b548c664a83f7ac90497eab2ae
if err == nil {
return nil
}
// If the error is multiple lines, include an extra blank line before the "See --help" line.
errStr := strings.TrimRight(err.Error(), "\n")
if strings.Contains(errStr, "\n") {
errStr += "\n"
}
fmt.Fprintf(cmd.ErrOrStderr(), "%s: %s\nSee '%s --help' for more information.\n", cmd.CommandPath(), errStr, cmd.CommandPath())
os.Exit(2)
return nil
}
func main() {
ctx := context.Background()
argparser := &cobra.Command{
Use: "goversion [flags] [COMMITISH]",
Short: "Like `git describe`, but emits Go pseudo-versions",
Args: cobra.RangeArgs(0, 1),
SilenceErrors: true, // main() will handle this after .ExecuteContext() returns
SilenceUsage: true, // our FlagErrorFunc will handle it
}
var argDirPrefix string
argparser.Flags().StringVar(&argDirPrefix, "dir-prefix", "",
"Consider the Go module `${COMMITISH}:${dir_prefix}/go.mod` instead of `${COMMITISH}:go.mod`")
var argAll bool
argparser.Flags().BoolVar(&argAll, "all", false,
"Print all possible version strings that identify `${COMMITISH}`, not just the highest one")
argparser.SetFlagErrorFunc(FlagErrorFunc)
argparser.RunE = func(cmd *cobra.Command, args []string) error {
commitish := "HEAD"
if len(args) == 1 {
commitish = args[0]
}
dirtyMarker := ""
if commitish == "HEAD" {
dirtyMarker = fmt.Sprintf("-dirty.%d", time.Now().Unix())
}
maxDescs := 1
if argAll {
maxDescs = 0
}
descs, err := Describe(cmd.Context(), commitish, argDirPrefix, dirtyMarker, maxDescs)
if err != nil {
return err
}
if dirtyMarker != "" && strings.HasPrefix(descs[0], dirtyMarker) && os.Getenv("CI") != "" {
fmt.Fprintln(os.Stderr, "error: this should not happen in CI: the tree should not be dirty")
// Don't bother checking for errors from .Run(), since these are
// just informative error messages.
cmd := exec.CommandContext(ctx, "git", "add", ".")
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
_ = cmd.Run()
cmd = exec.CommandContext(ctx, "git", "diff", "--cached")
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(),
"PAGER=")
_ = cmd.Run()
os.Exit(1)
}
for _, desc := range descs {
fmt.Println(desc)
}
return nil
}
if err := argparser.ExecuteContext(ctx); err != nil {
fmt.Fprintf(argparser.ErrOrStderr(), "%s: error: %v\n", argparser.CommandPath(), err)
os.Exit(1)
}
}