-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (69 loc) · 1.91 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
package main
import (
"errors"
"fmt"
"os"
"regexp"
"github.com/spf13/cobra"
)
// GlobalOptions collects all configuration for a single run.
type GlobalOptions struct {
Verbose bool
Debug bool
Config string
DisableChecks []string
Version string
}
func main() {
var (
gopts GlobalOptions
cfg Config
)
var cmd = &cobra.Command{
Use: "pondi [flags]",
Short: "pondi",
Long: "pondi builds release assets (binaries, source code archive) and creates a new release on GitHub ",
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
if gopts.Version != "" {
matched, err := regexp.MatchString(`^\d+\.\d+\.\d+$`, gopts.Version)
if err != nil {
panic(err)
}
if !matched {
return fmt.Errorf("version %q is invalid (format: 1.2.3)", gopts.Version)
}
}
cfg, err = LoadConfig(gopts.Config)
// if the config file was not explicitly passed and it does not
// exist, just use the default config.
if !cmd.Flags().Changed("config") && errors.Is(err, os.ErrNotExist) {
cfg = DefaultConfig
err = nil
}
if err != nil {
return err
}
return nil
},
}
addCommandCheck(cmd, &gopts, &cfg)
addCommandHooks(cmd, &gopts, &cfg)
flags := cmd.PersistentFlags()
flags.BoolVar(&gopts.Verbose, "verbose", false, "be verbose")
flags.BoolVar(&gopts.Debug, "debug", false, "print debug messages")
flags.StringVar(&gopts.Config, "config", ".pondi.yml", "load configuration from `file`")
flags.StringSliceVar(&gopts.DisableChecks, "disable-checks", nil, "disable checks `name1,name2,[...]`")
flags.StringVar(&gopts.Version, "version", "", "release version (format: `1.2.3`)")
err := cmd.MarkPersistentFlagRequired("version")
if err != nil {
panic(err)
}
err = cmd.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}