github.com/arsham/gitrelease@v0.3.2-0.20221207124258-6867180b2c2d/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  
     9  	"github.com/arsham/gitrelease/commit"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  	"github.com/spf13/viper"
    13  )
    14  
    15  var (
    16  	tag        string
    17  	printMode  bool
    18  	remote     string
    19  	version    = "development"
    20  	currentSha = "N/A"
    21  
    22  	rootCmd = &cobra.Command{
    23  		Use:   "gitrelease",
    24  		Short: "Release commit information of a tag to github",
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			if len(args) > 0 && args[0] == "version" {
    27  				fmt.Printf("gitrelease version %s (%s)\n", version, currentSha)
    28  				return nil
    29  			}
    30  
    31  			ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
    32  			defer cancel()
    33  			token := os.Getenv("GITHUB_TOKEN")
    34  			if token == "" {
    35  				return errors.New("please export GITHUB_TOKEN")
    36  			}
    37  			g := &commit.Git{
    38  				Remote: remote,
    39  			}
    40  
    41  			user, repo, err := g.RepoInfo(ctx)
    42  			if err != nil {
    43  				return errors.Wrap(err, "can't get repo name")
    44  			}
    45  
    46  			tag1, err := g.PreviousTag(ctx, tag)
    47  			if err != nil {
    48  				return errors.Wrap(err, "getting previous tag")
    49  			}
    50  
    51  			logs, err := g.Commits(ctx, tag1, tag)
    52  			if err != nil {
    53  				return err
    54  			}
    55  			desc := commit.ParseGroups(logs)
    56  			if tag == "@" {
    57  				tag, err = g.LatestTag(ctx)
    58  				if err != nil {
    59  					return err
    60  				}
    61  			}
    62  
    63  			if printMode {
    64  				_, err := fmt.Println(desc)
    65  				return err
    66  			}
    67  
    68  			return g.Release(ctx, token, user, repo, tag, desc)
    69  		},
    70  	}
    71  )
    72  
    73  func main() {
    74  	cobra.CheckErr(rootCmd.Execute())
    75  }
    76  
    77  func init() {
    78  	cobra.OnInitialize(viper.AutomaticEnv)
    79  	rootCmd.PersistentFlags().StringVarP(&tag, "tag", "t", "@", "tag to produce the logs for. Leave empty for current tag.")
    80  	rootCmd.PersistentFlags().BoolVarP(&printMode, "print", "p", false, "only print, do not release!")
    81  	rootCmd.PersistentFlags().StringVarP(&remote, "remote", "r", "origin", "use a different remote")
    82  
    83  	rootCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
    84    {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
    85    {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
    86  
    87  Aliases:
    88    {{.NameAndAliases}}{{end}}{{if .HasExample}}
    89  
    90  Examples:
    91  {{.Example}}{{end}}{{if .HasAvailableSubCommands}}
    92  
    93  Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
    94    {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
    95  
    96  Available Commands:
    97    help        Help about any command
    98    version     Print binary version information
    99  
   100  Flags:
   101  {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
   102  
   103  Global Flags:
   104  {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
   105  
   106  Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
   107    {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
   108  
   109  Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
   110  `)
   111  }