-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
111 lines (90 loc) · 2.87 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
110
111
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/arsham/gitrelease/commit"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
tag string
printMode bool
remote string
version = "development"
currentSha = "N/A"
rootCmd = &cobra.Command{
Use: "gitrelease",
Short: "Release commit information of a tag to github",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 && args[0] == "version" {
fmt.Printf("gitrelease version %s (%s)\n", version, currentSha)
return nil
}
ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
defer cancel()
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return errors.New("please export GITHUB_TOKEN")
}
g := &commit.Git{
Remote: remote,
}
user, repo, err := g.RepoInfo(ctx)
if err != nil {
return errors.Wrap(err, "can't get repo name")
}
tag1, err := g.PreviousTag(ctx, tag)
if err != nil {
return errors.Wrap(err, "getting previous tag")
}
logs, err := g.Commits(ctx, tag1, tag)
if err != nil {
return err
}
desc := commit.ParseGroups(logs)
if tag == "@" {
tag, err = g.LatestTag(ctx)
if err != nil {
return err
}
}
if printMode {
_, err := fmt.Println(desc)
return err
}
return g.Release(ctx, token, user, repo, tag, desc)
},
}
)
func main() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
cobra.OnInitialize(viper.AutomaticEnv)
rootCmd.PersistentFlags().StringVarP(&tag, "tag", "t", "@", "tag to produce the logs for. Leave empty for current tag.")
rootCmd.PersistentFlags().BoolVarP(&printMode, "print", "p", false, "only print, do not release!")
rootCmd.PersistentFlags().StringVarP(&remote, "remote", "r", "origin", "use a different remote")
rootCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Available Commands:
help Help about any command
version Print binary version information
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`)
}