-
Notifications
You must be signed in to change notification settings - Fork 221
/
main.go
84 lines (64 loc) · 1.7 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
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/tools/version/relmeta"
)
// TODO[md]: remove this when we're done with the semver to calver migration
const stableChannelStillOnSemver = true
var (
gitDir string
)
func main() {
rootCmd := &cobra.Command{
Use: "version",
Short: "Tool for working with flyctl version numbers",
}
rootCmd.PersistentFlags().StringVar(&gitDir, "git-dir", "", "path to git directory. defaults to current directory.")
showCmd := &cobra.Command{
Use: "show",
Short: "Show version information as a JSON object",
SilenceUsage: true,
RunE: runShow,
}
nextCmd := &cobra.Command{
Use: "next",
Short: "show the next version number for the current channel",
SilenceUsage: true,
RunE: runNext,
}
rootCmd.AddCommand(showCmd, nextCmd)
if err := rootCmd.Execute(); err != nil {
log.Fatalln(err)
}
}
func runShow(cmd *cobra.Command, args []string) error {
cmd.PrintErrln("Git dir:", gitDir)
if err := relmeta.RefreshTags(gitDir); err != nil {
return err
}
meta, err := relmeta.GenerateReleaseMeta(gitDir, stableChannelStillOnSemver)
if err != nil {
return err
}
if meta.Version == nil {
return fmt.Errorf("no version tag for commit %s", meta.Commit)
}
enc := json.NewEncoder(cmd.OutOrStdout())
enc.Encode(meta)
return nil
}
func runNext(cmd *cobra.Command, args []string) error {
cmd.PrintErrln("Git dir:", gitDir)
if err := relmeta.RefreshTags(gitDir); err != nil {
return err
}
ver, err := relmeta.NextVersion(gitDir, stableChannelStillOnSemver)
if err != nil {
return err
}
fmt.Fprint(cmd.OutOrStdout(), ver.String())
return nil
}