-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
103 lines (79 loc) · 2.46 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
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
)
func main() {
cfg := Config{}
flag.StringVar(&cfg.Token, "token", os.Getenv("GITHUB_TOKEN"), "GitHub Token [GITHUB_TOKEN]")
flag.StringVar(&cfg.Source.Owner, "src-owner", "traefik", "Owner of the source repository.")
flag.StringVar(&cfg.Source.RepoName, "src-repo-name", "traefik", "Name of the source repo.")
flag.StringVar(&cfg.Source.DocPath, "src-doc-path", "./docs/site", "Path to the documentation.")
flag.StringVar(&cfg.Target.Owner, "dst-owner", "traefik", "Owner of the targeted doc repo.")
flag.StringVar(&cfg.Target.RepoName, "dst-repo-name", "doc", "Name of the targeted doc repo.")
flag.StringVar(&cfg.Target.DocPath, "dst-doc-path", "./traefik", "Path to put the documentation.")
flag.StringVar(&cfg.Git.UserName, "git-user-name", os.Getenv("GIT_USER_NAME"), "UserName used to commit the documentation. [GIT_USER_NAME]")
flag.StringVar(&cfg.Git.UserEmail, "git-user-email", os.Getenv("GIT_USER_EMAIL"), "Email used to commit the documentation. [GIT_USER_EMAIL]")
flag.BoolVar(&cfg.Debug, "debug", false, "Debug mode")
version := flag.Bool("v", false, "Show version.")
help := flag.Bool("h", false, "Show this help.")
flag.Usage = usage
flag.Parse()
if *help {
usage()
}
if *version {
displayVersion()
return
}
if flag.NArg() > 0 {
usage()
}
err := validate(cfg)
if err != nil {
flag.PrintDefaults()
log.Fatal(err)
}
err = run(cfg)
if err != nil {
log.Fatalf("error: %v", err)
}
}
func usage() {
_, _ = os.Stderr.WriteString(fmt.Sprintf("Lasius Mixtus (%s)\n\nFlags:\n", version))
flag.PrintDefaults()
os.Exit(2)
}
func validate(cfg Config) error {
if cfg.Token == "" {
return errors.New("token is required")
}
err := validateRepoInfo(cfg.Source)
if err != nil {
return fmt.Errorf("source: %w", err)
}
err = validateRepoInfo(cfg.Target)
if err != nil {
return fmt.Errorf("target: %w", err)
}
if (cfg.Git.UserName != "" && cfg.Git.UserEmail == "") || (cfg.Git.UserName == "" && cfg.Git.UserEmail != "") {
return fmt.Errorf("both options (Git user's email and username) must be set together or not set at all: %s - %s",
cfg.Git.UserName, cfg.Git.UserEmail)
}
return nil
}
func validateRepoInfo(info RepoInfo) error {
if info.Owner == "" {
return errors.New("owner is required")
}
if info.RepoName == "" {
return errors.New("repository name is required")
}
if info.DocPath == "" {
return errors.New("documentation path is required")
}
return nil
}